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>
|
|
|
|
|
2008-04-20 04:15:54 +02:00
|
|
|
#include "types.h"
|
2008-02-27 21:04:17 +01:00
|
|
|
|
2008-05-22 09:05:49 +02:00
|
|
|
#include "util/debug.h"
|
2008-05-03 18:46:48 +02:00
|
|
|
|
2008-04-20 04:15:54 +02:00
|
|
|
class Log_Entry;
|
2008-02-27 21:04:17 +01:00
|
|
|
class Loggable;
|
2008-04-20 04:15:54 +02:00
|
|
|
typedef Loggable *(create_func)(Log_Entry &);
|
|
|
|
|
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 ; }
|
|
|
|
|
|
|
|
#define LOG_CREATE_FUNC( class ) \
|
|
|
|
static Loggable * \
|
|
|
|
create ( Log_Entry &e ) \
|
|
|
|
{ \
|
|
|
|
class *r = new class; \
|
|
|
|
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 ) \
|
|
|
|
virtual const char *class_name ( void ) const { return #class ; } \
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
2008-02-22 21:20:44 +01:00
|
|
|
static FILE *_fp;
|
2008-02-22 12:44:35 +01:00
|
|
|
static int _log_id;
|
2008-02-24 11:04:14 +01:00
|
|
|
static int _level;
|
2008-02-25 23:47:49 +01:00
|
|
|
static int _undo_index;
|
2008-02-22 12:44:35 +01:00
|
|
|
|
2008-05-06 01:24:21 +02:00
|
|
|
static size_t _loggables_size;
|
|
|
|
static Loggable ** _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-02-22 12:44:35 +01:00
|
|
|
private:
|
2008-04-24 02:33:44 +02:00
|
|
|
|
2008-05-05 07:29:39 +02:00
|
|
|
int _id;
|
2008-02-22 12:44:35 +01:00
|
|
|
|
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-24 11:04:14 +01:00
|
|
|
|
2008-05-06 01:24:21 +02:00
|
|
|
static void ensure_size ( size_t n )
|
|
|
|
{
|
|
|
|
if ( n > _loggables_size )
|
|
|
|
{
|
|
|
|
size_t p = 0;
|
|
|
|
while ( ( (unsigned)1 << p ) < n ) ++p;
|
|
|
|
|
|
|
|
size_t os = _loggables_size;
|
|
|
|
_loggables_size = 1 << p ;
|
|
|
|
|
|
|
|
_loggables = (Loggable**) realloc( _loggables, sizeof( Loggable ** ) * _loggables_size );
|
|
|
|
|
|
|
|
memset( _loggables + os, 0, _loggables_size - os );
|
|
|
|
}
|
|
|
|
}
|
2008-05-05 20:35:04 +02:00
|
|
|
|
2008-04-25 08:34:08 +02:00
|
|
|
void log_print( char **o, char **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
|
|
|
static
|
|
|
|
void indent ( void )
|
|
|
|
{
|
|
|
|
int n = Loggable::_level;
|
|
|
|
|
|
|
|
while ( n-- )
|
2008-02-25 05:14:19 +01:00
|
|
|
log( "\t" );
|
2008-02-24 11:04:14 +01:00
|
|
|
}
|
|
|
|
|
2008-04-24 02:33:44 +02:00
|
|
|
static bool snapshot( FILE * fp );
|
2008-02-25 05:14:19 +01:00
|
|
|
|
2008-05-08 02:25:12 +02:00
|
|
|
void init ( bool loggable=true )
|
|
|
|
{
|
2008-05-08 03:05:49 +02:00
|
|
|
_new_state = _old_state = NULL;
|
|
|
|
_nest = 0;
|
|
|
|
|
2008-05-08 02:25:12 +02:00
|
|
|
if ( loggable )
|
|
|
|
{
|
|
|
|
_id = ++_log_id;
|
|
|
|
|
|
|
|
ensure_size( _id );
|
|
|
|
|
|
|
|
_loggables[ _id - 1 ] = this;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_id = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-05-08 03:05:49 +02:00
|
|
|
/* not implemented */
|
|
|
|
const Loggable & operator= ( const Loggable &rhs );
|
|
|
|
|
2008-02-22 11:22:22 +01:00
|
|
|
public:
|
|
|
|
|
2008-05-05 20:35:04 +02:00
|
|
|
static const char *escape ( const char *s );
|
|
|
|
|
2008-05-05 07:29:39 +02:00
|
|
|
int id ( void ) const { return _id; }
|
|
|
|
|
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-02-27 21:04:17 +01:00
|
|
|
static int undo_index ( void ) { return _undo_index; }
|
2008-04-24 02:33:44 +02:00
|
|
|
static void compact ( void );
|
2008-02-22 21:20:44 +01:00
|
|
|
|
2008-02-24 11:04:14 +01:00
|
|
|
static
|
|
|
|
void
|
|
|
|
block_start ( void )
|
|
|
|
{
|
|
|
|
++Loggable::_level;
|
|
|
|
}
|
2008-03-05 18:03:17 +01:00
|
|
|
|
2008-02-24 11:04:14 +01:00
|
|
|
static
|
|
|
|
void
|
|
|
|
block_end ( void )
|
|
|
|
{
|
|
|
|
assert( --Loggable::_level >= 0 );
|
2008-03-05 18:03:17 +01:00
|
|
|
|
|
|
|
if ( Loggable::_level == 0 )
|
|
|
|
flush();
|
2008-02-24 11:04:14 +01:00
|
|
|
}
|
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-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
|
|
|
}
|
|
|
|
|
2008-05-06 01:24:21 +02:00
|
|
|
void update_id ( int id );
|
2008-03-01 08:31:06 +01:00
|
|
|
|
|
|
|
virtual ~Loggable ( )
|
|
|
|
{
|
|
|
|
_loggables[ _id - 1 ] = NULL;
|
2008-02-27 21:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
register_create ( const char *name, create_func *func )
|
|
|
|
{
|
2008-05-03 06:44:48 +02:00
|
|
|
// printf( "registering %s to %p\n", name, func );
|
2008-02-27 21:04:17 +01:00
|
|
|
|
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;
|
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-03-27 06:58:31 +01:00
|
|
|
static bool do_this ( const char *s, bool reverse );
|
|
|
|
|
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_create ( void ) const;
|
|
|
|
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
|
|
|
/* FIXME: get a real id here!!! */
|
2008-05-08 02:25:12 +02:00
|
|
|
// _id = 0;
|
2008-05-08 02:04:47 +02:00
|
|
|
}
|
|
|
|
|
2008-02-24 11:58:16 +01:00
|
|
|
public:
|
|
|
|
|
2008-05-04 01:25:59 +02:00
|
|
|
// virtual const char *class_name ( void ) const = 0;
|
2008-02-24 11:58:16 +01:00
|
|
|
|
2008-03-01 08:31:06 +01:00
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
printf( "hold\n" );
|
|
|
|
_this->_nest++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void release ( void )
|
|
|
|
{
|
|
|
|
printf( "release\n" );
|
|
|
|
_this->_nest--;
|
|
|
|
assert( _this->_nest );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-02-24 08:42:41 +01:00
|
|
|
|
2008-04-20 04:15:54 +02:00
|
|
|
class Log_Entry
|
|
|
|
{
|
|
|
|
// vector <Pair> _sa;
|
|
|
|
char **_sa;
|
|
|
|
int _i;
|
|
|
|
|
2008-05-08 03:05:49 +02:00
|
|
|
/* not permitted */
|
|
|
|
Log_Entry ( const Log_Entry &rhs );
|
|
|
|
Log_Entry & operator= ( const Log_Entry &rhs );
|
|
|
|
|
2008-04-20 04:15:54 +02:00
|
|
|
public:
|
|
|
|
|
2008-04-20 18:42:26 +02:00
|
|
|
struct Pair
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
const char *value;
|
|
|
|
};
|
|
|
|
|
2008-04-20 04:15:54 +02:00
|
|
|
Log_Entry ( )
|
|
|
|
{
|
|
|
|
_sa = (char**)malloc( sizeof( char * ) );
|
|
|
|
*_sa = NULL;
|
|
|
|
_i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log_Entry ( char **sa )
|
|
|
|
{
|
2008-04-20 18:42:26 +02:00
|
|
|
_sa = sa;
|
|
|
|
_i = 0;
|
2008-04-20 04:15:54 +02:00
|
|
|
|
2008-04-20 18:42:26 +02:00
|
|
|
if ( _sa )
|
|
|
|
while ( _sa[ _i ] ) ++_i;
|
2008-04-20 23:46:47 +02:00
|
|
|
|
2008-04-20 04:15:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~Log_Entry ( )
|
|
|
|
{
|
|
|
|
if ( ! _sa )
|
|
|
|
return;
|
|
|
|
|
|
|
|
for ( _i = 0; _sa[ _i ]; ++_i )
|
|
|
|
{
|
|
|
|
free( _sa[ _i ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
free( _sa );
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************/
|
|
|
|
/* Construction */
|
|
|
|
/****************/
|
|
|
|
|
|
|
|
void grow ( )
|
|
|
|
{
|
|
|
|
_sa = (char**)realloc( _sa, sizeof( char * ) * (_i + 2) );
|
2008-04-20 18:42:26 +02:00
|
|
|
_sa[ _i + 1 ] = NULL;
|
2008-04-20 04:15:54 +02:00
|
|
|
}
|
|
|
|
|
2008-04-20 18:42:26 +02:00
|
|
|
#define ADD( type, format, exp ) \
|
2008-04-20 04:15:54 +02:00
|
|
|
void add ( const char *name, type v ) \
|
|
|
|
{ \
|
|
|
|
grow(); \
|
2008-04-20 18:42:26 +02:00
|
|
|
asprintf( &_sa[ _i ], "%s " format, name, (exp) ); \
|
|
|
|
strtok( _sa[ _i++ ], " " ); \
|
2008-04-20 04:15:54 +02:00
|
|
|
} \
|
|
|
|
|
|
|
|
/***************/
|
|
|
|
/* Examination */
|
|
|
|
/***************/
|
|
|
|
|
|
|
|
int size ( void ) const
|
|
|
|
{
|
|
|
|
return _i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void get ( int n, const char **name, const char **value )
|
|
|
|
{
|
|
|
|
*name = _sa[ n ];
|
|
|
|
*value = *name + strlen( *name ) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char **sa ( void )
|
|
|
|
{
|
|
|
|
char **sa = _sa;
|
|
|
|
|
|
|
|
_sa = NULL;
|
|
|
|
|
|
|
|
return sa;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* #define ADD ( type, format, exp ) \ */
|
|
|
|
/* void add ( const char *name, type v ) \ */
|
|
|
|
/* { \ */
|
|
|
|
/* char pat[ 256 ]; \ */
|
|
|
|
/* Pair p; \ */
|
|
|
|
/* p.name = strdup( name ); \ */
|
|
|
|
/* snprintf( pat, sizeof( pat ), format, exp ); \ */
|
|
|
|
/* p.value = strdup( pat ); \ */
|
|
|
|
/* _sa.push( p ); \ */
|
|
|
|
/* } \ */
|
|
|
|
|
|
|
|
ADD( int, "%d", v );
|
2008-04-20 18:42:26 +02:00
|
|
|
ADD( nframes_t, "%lu", (unsigned long)v );
|
2008-04-26 11:01:54 +02:00
|
|
|
ADD( unsigned long, "%lu", v );
|
2008-05-05 20:35:04 +02:00
|
|
|
ADD( const char *, "\"%s\"", v ? Loggable::escape( v ) : "" );
|
2008-05-05 07:29:39 +02:00
|
|
|
ADD( Loggable * , "0x%X", v ? v->id() : 0 );
|
2008-04-20 04:15:54 +02:00
|
|
|
ADD( float, "%f", v );
|
|
|
|
ADD( double, "%f", v );
|
|
|
|
|
|
|
|
#undef ADD
|
|
|
|
|
|
|
|
};
|