2008-02-22 11:22:22 +01:00
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
/* Copyright (C) 2008 Jonathan Moore Liles */
|
|
|
|
/* */
|
|
|
|
/* This program is free software; you can redistribute it and/or modify it */
|
|
|
|
/* under the terms of the GNU General Public License as published by the */
|
|
|
|
/* Free Software Foundation; either version 2 of the License, or (at your */
|
|
|
|
/* option) any later version. */
|
|
|
|
/* */
|
|
|
|
/* This program is distributed in the hope that it will be useful, but WITHOUT */
|
|
|
|
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
|
|
|
|
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
|
|
|
|
/* more details. */
|
|
|
|
/* */
|
|
|
|
/* You should have received a copy of the GNU General Public License along */
|
|
|
|
/* with This program; see the file COPYING. If not,write to the Free Software */
|
|
|
|
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
2008-05-22 17:32:26 +02:00
|
|
|
|
2008-02-22 11:22:22 +01:00
|
|
|
/* Master class for journaling. */
|
|
|
|
|
2008-02-22 12:44:35 +01:00
|
|
|
#pragma once
|
|
|
|
|
2008-02-22 21:20:44 +01:00
|
|
|
#include <stdio.h>
|
2008-02-24 08:42:41 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-02-24 11:04:14 +01:00
|
|
|
#include <assert.h>
|
2008-02-24 08:42:41 +01:00
|
|
|
|
2008-02-27 21:04:17 +01:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2008-03-03 17:13:17 +01:00
|
|
|
#include <queue>
|
|
|
|
|
2009-12-25 01:57:34 +01:00
|
|
|
// #include "types.h"
|
2008-02-27 21:04:17 +01:00
|
|
|
|
2008-06-05 06:27:55 +02:00
|
|
|
typedef void (progress_func)( int, void * );
|
2008-08-04 05:54:22 +02:00
|
|
|
typedef void (snapshot_func)( void * );
|
2008-06-05 06:27:55 +02:00
|
|
|
|
2008-04-20 04:15:54 +02:00
|
|
|
class Log_Entry;
|
2008-02-27 21:04:17 +01:00
|
|
|
class Loggable;
|
2009-01-11 22:38:29 +01:00
|
|
|
typedef Loggable *(create_func)(Log_Entry &, unsigned int id);
|
2008-04-20 04:15:54 +02:00
|
|
|
|
2008-04-21 02:28:59 +02:00
|
|
|
#define LOG_REGISTER_CREATE( class ) \
|
|
|
|
Loggable::register_create( #class, & class ::create );
|
2008-04-20 04:15:54 +02:00
|
|
|
|
2008-04-25 08:34:08 +02:00
|
|
|
#define LOG_NAME_FUNC( class ) \
|
|
|
|
virtual const char *class_name ( void ) const { return #class ; }
|
|
|
|
|
2010-01-14 04:20:40 +01:00
|
|
|
#define LOG_CREATE_FUNC( class ) \
|
|
|
|
static Loggable * \
|
|
|
|
create ( Log_Entry &e, unsigned int id ) \
|
|
|
|
{ \
|
|
|
|
class *r = new class; \
|
|
|
|
r->update_id( id ); \
|
|
|
|
r->set( e ); \
|
|
|
|
return (Loggable *)r; \
|
|
|
|
} \
|
|
|
|
LOG_NAME_FUNC( class );
|
2008-04-20 04:15:54 +02:00
|
|
|
|
2008-02-22 12:44:35 +01:00
|
|
|
|
2008-05-05 07:29:39 +02:00
|
|
|
#define LOG_NOT_LOGGABLE_FUNC( class ) \
|
2010-01-14 04:20:40 +01:00
|
|
|
virtual const char *class_name ( void ) const { return #class ; }
|
2008-05-05 07:29:39 +02:00
|
|
|
|
2008-02-24 14:28:55 +01:00
|
|
|
class Logger;
|
2008-05-05 07:29:39 +02:00
|
|
|
class Loggable
|
2008-02-22 11:22:22 +01:00
|
|
|
{
|
2009-01-12 01:37:38 +01:00
|
|
|
struct log_pair {
|
|
|
|
Loggable * loggable;
|
|
|
|
Log_Entry * unjournaled_state;
|
|
|
|
};
|
2008-02-22 11:22:22 +01:00
|
|
|
|
2008-02-22 21:20:44 +01:00
|
|
|
static FILE *_fp;
|
2009-01-11 22:38:29 +01:00
|
|
|
static unsigned int _log_id;
|
2008-02-24 11:04:14 +01:00
|
|
|
static int _level;
|
2008-06-09 00:42:26 +02:00
|
|
|
|
|
|
|
static off_t _undo_offset;
|
2008-02-22 12:44:35 +01:00
|
|
|
|
2009-01-12 01:37:38 +01:00
|
|
|
static std::map <unsigned int, Loggable::log_pair > _loggables;
|
2008-02-24 08:42:41 +01:00
|
|
|
|
2008-05-22 22:58:36 +02:00
|
|
|
static std::map <std::string, create_func*> _class_map;
|
2008-02-27 21:04:17 +01:00
|
|
|
|
2008-05-22 22:58:36 +02:00
|
|
|
static std::queue <char *> _transaction;
|
2008-03-03 17:13:17 +01:00
|
|
|
|
2008-06-05 06:27:55 +02:00
|
|
|
static progress_func *_progress_callback;
|
|
|
|
static void *_progress_callback_arg;
|
|
|
|
|
2008-08-04 05:54:22 +02:00
|
|
|
static snapshot_func *_snapshot_callback;
|
|
|
|
static void *_snapshot_callback_arg;
|
|
|
|
|
2008-02-22 12:44:35 +01:00
|
|
|
private:
|
2008-04-24 02:33:44 +02:00
|
|
|
|
2010-01-31 19:51:55 +01:00
|
|
|
static unsigned int _relative_id;
|
|
|
|
|
2009-01-11 22:38:29 +01:00
|
|
|
unsigned int _id;
|
2008-02-22 12:44:35 +01:00
|
|
|
|
2008-06-04 05:47:26 +02:00
|
|
|
Log_Entry *_old_state;
|
2008-02-24 08:42:41 +01:00
|
|
|
|
2008-02-24 14:28:55 +01:00
|
|
|
int _nest;
|
2008-02-24 11:04:14 +01:00
|
|
|
|
2010-02-01 04:14:56 +01:00
|
|
|
static int _dirty; /* count of changes */
|
|
|
|
|
2008-06-11 03:55:39 +02:00
|
|
|
static void ensure_size ( size_t n );
|
2008-05-05 20:35:04 +02:00
|
|
|
|
2009-01-11 07:35:20 +01:00
|
|
|
void log_print ( const Log_Entry *o, const Log_Entry *n ) const;
|
2008-02-25 05:14:19 +01:00
|
|
|
static void log ( const char *fmt, ... );
|
|
|
|
|
2008-03-03 17:13:17 +01:00
|
|
|
static void flush ( void );
|
2008-02-24 11:04:14 +01:00
|
|
|
|
2008-02-25 05:14:19 +01:00
|
|
|
|
2008-05-08 02:25:12 +02:00
|
|
|
void init ( bool loggable=true )
|
|
|
|
{
|
2008-06-04 05:47:26 +02:00
|
|
|
// _new_state
|
|
|
|
_old_state = NULL;
|
2008-05-08 03:05:49 +02:00
|
|
|
_nest = 0;
|
|
|
|
|
2008-05-08 02:25:12 +02:00
|
|
|
if ( loggable )
|
|
|
|
{
|
|
|
|
_id = ++_log_id;
|
|
|
|
|
2009-01-12 01:37:38 +01:00
|
|
|
_loggables[ _id ].loggable = this;
|
2008-05-08 02:25:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
_id = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-05-08 03:05:49 +02:00
|
|
|
/* not implemented */
|
|
|
|
const Loggable & operator= ( const Loggable &rhs );
|
|
|
|
|
2009-01-11 07:35:20 +01:00
|
|
|
void record_unjournaled ( void ) const;
|
|
|
|
static bool load_unjournaled_state ( void );
|
2008-06-05 06:27:55 +02:00
|
|
|
|
2009-12-25 01:57:34 +01:00
|
|
|
static bool replay ( FILE *fp );
|
|
|
|
|
2008-02-22 11:22:22 +01:00
|
|
|
public:
|
|
|
|
|
2009-12-28 06:25:28 +01:00
|
|
|
static bool replay ( const char *name );
|
2009-12-25 01:57:34 +01:00
|
|
|
|
|
|
|
static bool snapshot( FILE * fp );
|
|
|
|
static bool snapshot( const char *name );
|
|
|
|
|
2008-08-04 05:54:22 +02:00
|
|
|
static void snapshot_callback ( snapshot_func *p, void *arg ) { _snapshot_callback = p; _snapshot_callback_arg = arg; }
|
2008-06-05 06:27:55 +02:00
|
|
|
static void progress_callback ( progress_func *p, void *arg ) { _progress_callback = p; _progress_callback_arg = arg;}
|
2008-05-05 20:35:04 +02:00
|
|
|
static const char *escape ( const char *s );
|
|
|
|
|
2009-01-11 22:38:29 +01:00
|
|
|
unsigned int id ( void ) const { return _id; }
|
2008-05-05 07:29:39 +02:00
|
|
|
|
2009-01-20 05:45:39 +01:00
|
|
|
static bool save_unjournaled_state ( void );
|
2008-02-22 21:20:44 +01:00
|
|
|
static bool open ( const char *filename );
|
2008-05-04 00:50:32 +02:00
|
|
|
static bool close ( void );
|
2008-02-25 05:14:19 +01:00
|
|
|
static void undo ( void );
|
2008-06-09 00:42:26 +02:00
|
|
|
|
2008-04-24 02:33:44 +02:00
|
|
|
static void compact ( void );
|
2008-02-22 21:20:44 +01:00
|
|
|
|
2008-07-30 04:30:45 +02:00
|
|
|
static void block_start ( void );
|
|
|
|
static void block_end ( void );
|
2008-02-24 08:42:41 +01:00
|
|
|
|
2009-01-11 22:38:29 +01:00
|
|
|
static Loggable * find ( unsigned int id );
|
2008-02-24 08:42:41 +01:00
|
|
|
|
2008-05-05 07:29:39 +02:00
|
|
|
Loggable ( bool loggable=true )
|
2008-02-22 12:44:35 +01:00
|
|
|
{
|
2008-05-08 02:25:12 +02:00
|
|
|
init( loggable );
|
2008-02-22 12:44:35 +01:00
|
|
|
}
|
|
|
|
|
2009-01-11 22:38:29 +01:00
|
|
|
void update_id ( unsigned int id );
|
2008-03-01 08:31:06 +01:00
|
|
|
|
2009-01-11 07:35:20 +01:00
|
|
|
virtual ~Loggable ( );
|
2008-02-27 21:04:17 +01:00
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
register_create ( const char *name, create_func *func )
|
|
|
|
{
|
2008-05-22 22:58:36 +02:00
|
|
|
_class_map[ std::string( name ) ] = func;
|
2008-02-27 21:04:17 +01:00
|
|
|
}
|
2008-02-24 08:42:41 +01:00
|
|
|
|
2008-02-22 11:22:22 +01:00
|
|
|
/* log messages for journal */
|
2008-04-25 08:34:08 +02:00
|
|
|
virtual void get ( Log_Entry &e ) const = 0;
|
2009-01-11 07:35:20 +01:00
|
|
|
virtual void get_unjournaled ( Log_Entry & ) const
|
|
|
|
{
|
|
|
|
/* implementation optional */
|
|
|
|
}
|
2008-04-20 04:15:54 +02:00
|
|
|
virtual void set ( Log_Entry &e ) = 0;
|
2008-03-27 06:58:31 +01:00
|
|
|
|
2008-05-05 07:29:39 +02:00
|
|
|
virtual const char *class_name ( void ) const = 0;
|
|
|
|
|
2008-08-04 05:54:22 +02:00
|
|
|
virtual void log_children ( void ) const { return; }
|
2008-05-05 07:29:39 +02:00
|
|
|
|
2010-01-31 19:51:55 +01:00
|
|
|
static void begin_relative_id_mode ( void );
|
|
|
|
|
|
|
|
static void end_relative_id_mode ( void );
|
|
|
|
|
2008-03-27 06:58:31 +01:00
|
|
|
static bool do_this ( const char *s, bool reverse );
|
|
|
|
|
2010-02-01 04:14:56 +01:00
|
|
|
static int dirty ( void ) { return _dirty; }
|
|
|
|
static void clear_dirty ( void ) { _dirty = 0; }
|
|
|
|
|
2008-08-04 05:54:22 +02:00
|
|
|
void log_create ( void ) const;
|
2008-05-08 03:05:49 +02:00
|
|
|
|
2008-02-24 08:42:41 +01:00
|
|
|
protected:
|
2008-02-22 12:44:35 +01:00
|
|
|
|
2008-02-24 08:42:41 +01:00
|
|
|
void log_start ( void );
|
|
|
|
void log_end ( void );
|
2008-05-22 22:34:21 +02:00
|
|
|
|
2008-04-25 08:34:08 +02:00
|
|
|
void log_destroy ( void ) const;
|
2008-02-22 12:44:35 +01:00
|
|
|
|
2008-05-08 02:04:47 +02:00
|
|
|
/* leaf subclasses *must* call log_create() at the end of their copy contructors */
|
2008-05-19 05:01:09 +02:00
|
|
|
Loggable ( const Loggable & )
|
2008-05-08 02:04:47 +02:00
|
|
|
{
|
2008-05-08 02:25:12 +02:00
|
|
|
init( true );
|
2008-05-08 02:04:47 +02:00
|
|
|
}
|
|
|
|
|
2008-02-24 11:58:16 +01:00
|
|
|
public:
|
|
|
|
|
2008-02-24 14:28:55 +01:00
|
|
|
friend class Logger;
|
2008-02-22 11:22:22 +01:00
|
|
|
};
|
2008-02-22 12:44:35 +01:00
|
|
|
|
2008-02-24 14:28:55 +01:00
|
|
|
|
|
|
|
class Logger
|
|
|
|
{
|
|
|
|
|
|
|
|
Loggable *_this;
|
|
|
|
Logger ( ) {}
|
|
|
|
|
2008-05-08 03:05:49 +02:00
|
|
|
/* not permitted */
|
|
|
|
Logger ( const Logger &rhs );
|
|
|
|
const Logger & operator= ( const Logger &rhs );
|
|
|
|
|
2008-02-24 14:28:55 +01:00
|
|
|
public:
|
|
|
|
|
|
|
|
Logger ( Loggable *l ) : _this( l )
|
|
|
|
{
|
|
|
|
_this->log_start();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~Logger ( )
|
|
|
|
{
|
|
|
|
_this->log_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void hold ( void )
|
|
|
|
{
|
|
|
|
_this->_nest++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void release ( void )
|
|
|
|
{
|
|
|
|
_this->_nest--;
|
|
|
|
assert( _this->_nest );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-06-04 05:22:13 +02:00
|
|
|
#include "Log_Entry.H"
|