non/Loggable.H

212 lines
4.9 KiB
C++
Raw Normal View History

/*******************************************************************************/
/* 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. */
/*******************************************************************************/
/* 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>
#include <assert.h>
2008-02-24 08:42:41 +01:00
#include <vector>
using std::vector;
2008-02-27 21:04:17 +01:00
#include <map>
using std::map;
#include <string>
using std::string;
class Loggable;
typedef Loggable *(create_func)(char **);
2008-02-22 12:44:35 +01:00
2008-02-24 14:28:55 +01:00
class Logger;
class Loggable
{
2008-02-22 21:20:44 +01:00
static FILE *_fp;
2008-02-22 12:44:35 +01:00
static int _log_id;
static int _level;
2008-02-25 23:47:49 +01:00
static int _undo_index;
2008-02-22 12:44:35 +01:00
2008-02-24 08:42:41 +01:00
static vector <Loggable *> _loggables;
2008-02-27 21:04:17 +01:00
static map <string, create_func*> _class_map;
2008-02-22 12:44:35 +01:00
private:
int _id;
2008-02-24 08:42:41 +01:00
char **_old_state;
char **_new_state;
2008-02-24 14:28:55 +01:00
int _nest;
2008-02-25 05:14:19 +01:00
void log_print( char **o, char **n );
static void log ( const char *fmt, ... );
static
void indent ( void )
{
int n = Loggable::_level;
while ( n-- )
2008-02-25 05:14:19 +01:00
log( "\t" );
}
2008-02-25 05:14:19 +01:00
public:
2008-02-22 21:20:44 +01:00
static bool open ( const char *filename );
2008-02-25 05:14:19 +01:00
static void undo ( void );
2008-02-27 21:04:17 +01:00
static int undo_index ( void ) { return _undo_index; }
static bool snapshot( const char *file );
2008-02-22 21:20:44 +01:00
static
void
block_start ( void )
{
indent();
2008-02-27 21:04:17 +01:00
// log( "{\n" );
++Loggable::_level;
}
static
void
block_end ( void )
{
assert( --Loggable::_level >= 0 );
indent();
2008-02-27 21:04:17 +01:00
// log( "}\n" );
}
2008-02-24 08:42:41 +01:00
static
Loggable *
find ( int id )
{
if ( id > _log_id )
return NULL;
2008-02-25 05:14:19 +01:00
return _loggables[ id - 1 ];
2008-02-24 08:42:41 +01:00
}
2008-02-22 12:44:35 +01:00
Loggable ( )
{
_id = ++_log_id;
2008-02-24 08:42:41 +01:00
_old_state = NULL;
2008-02-24 14:28:55 +01:00
_nest = 0;
2008-02-24 08:42:41 +01:00
_loggables.push_back( this );
2008-02-22 12:44:35 +01:00
}
/** must be called after construction in create() methods */
void
update_id ( int id )
2008-02-27 21:04:17 +01:00
{
assert( _id == _log_id );
_loggables[ _id - 1 ] = NULL;
2008-02-27 21:04:17 +01:00
--_log_id;
2008-02-27 21:04:17 +01:00
_id = id;
2008-02-27 21:04:17 +01:00
assert( ! _loggables[ _id - 1 ] );
_loggables[ _id - 1 ] = this;
}
virtual ~Loggable ( )
{
_loggables[ _id - 1 ] = NULL;
2008-02-27 21:04:17 +01:00
}
static
void
register_create ( const char *name, create_func *func )
{
printf( "registering %s to %p\n", name, func );
_class_map[ string( name ) ] = func;
}
2008-02-24 08:42:41 +01:00
/* log messages for journal */
2008-02-24 08:42:41 +01:00
virtual const char *class_name ( void ) = 0;
virtual char ** get ( void ) = 0;
2008-02-24 08:42:41 +01:00
/* this method must parse an array of name/value pair strings and make the appropriate changes too
the object state */
virtual void set ( char **sa ) = 0;
// void log ( const char *module, const char *action, const char *fmt, ... );
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 );
void log_create ( void );
void log_destroy ( void );
2008-02-22 12:44:35 +01:00
2008-02-24 11:58:16 +01:00
public:
int id ( void ) { return _id; }
2008-02-24 14:28:55 +01:00
friend class Logger;
};
2008-02-22 12:44:35 +01:00
2008-02-24 14:28:55 +01:00
class Logger
{
Loggable *_this;
Logger ( ) {}
public:
Logger ( Loggable *l ) : _this( l )
{
_this->log_start();
}
~Logger ( )
{
_this->log_end();
}
void hold ( void )
{
printf( "hold\n" );
_this->_nest++;
}
void release ( void )
{
printf( "release\n" );
_this->_nest--;
assert( _this->_nest );
}
};
2008-02-24 11:58:16 +01:00
/* #ifndef _LOGGABLE_C */
/* #define log( act, fmt, args... ) log( __CLASS__, act, fmt, ## args ) */
/* #endif */
2008-02-24 08:42:41 +01:00
/* #define LOG_START Logger _logger( this ) */
/* #define LOG_END _logger.print( this ) */