Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

405 lines
9.6KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. /* Master class for journaling. */
  19. #pragma once
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <vector>
  25. #include <map>
  26. #include <string>
  27. #include <queue>
  28. using namespace std;
  29. #include "types.h"
  30. #include "debug.h"
  31. /* welcome to C++ */
  32. /* This class is just a dummy to allow base classes with null ids but
  33. * whose children are really loggable. */
  34. /* class Loggable_ID */
  35. /* { */
  36. /* public: */
  37. /* Loggable_ID ( ) { } */
  38. /* virtual ~Loggable_ID ( ) { } */
  39. /* virtual int id ( void ) const = 0; */
  40. /* virtual const char *class_name ( void ) const = 0; */
  41. /* }; */
  42. class Log_Entry;
  43. class Loggable;
  44. typedef Loggable *(create_func)(Log_Entry &);
  45. #define LOG_REGISTER_CREATE( class ) \
  46. Loggable::register_create( #class, & class ::create );
  47. #define LOG_NAME_FUNC( class ) \
  48. virtual const char *class_name ( void ) const { return #class ; }
  49. #define LOG_CREATE_FUNC( class ) \
  50. static Loggable * \
  51. create ( Log_Entry &e ) \
  52. { \
  53. class *r = new class; \
  54. r->set( e ); \
  55. return (Loggable *)r; \
  56. } \
  57. LOG_NAME_FUNC( class ); \
  58. #define LOG_NOT_LOGGABLE_FUNC( class ) \
  59. virtual const char *class_name ( void ) const { return #class ; } \
  60. class Logger;
  61. class Loggable
  62. {
  63. static FILE *_fp;
  64. static int _log_id;
  65. static int _level;
  66. static int _undo_index;
  67. static size_t _loggables_size;
  68. static Loggable ** _loggables;
  69. static map <string, create_func*> _class_map;
  70. static queue <char *> _transaction;
  71. private:
  72. int _id;
  73. char **_old_state;
  74. char **_new_state;
  75. int _nest;
  76. static void ensure_size ( size_t n )
  77. {
  78. if ( n > _loggables_size )
  79. {
  80. size_t p = 0;
  81. while ( ( (unsigned)1 << p ) < n ) ++p;
  82. size_t os = _loggables_size;
  83. _loggables_size = 1 << p ;
  84. _loggables = (Loggable**) realloc( _loggables, sizeof( Loggable ** ) * _loggables_size );
  85. memset( _loggables + os, 0, _loggables_size - os );
  86. }
  87. }
  88. void log_print( char **o, char **n ) const;
  89. static void log ( const char *fmt, ... );
  90. static void flush ( void );
  91. static
  92. void indent ( void )
  93. {
  94. int n = Loggable::_level;
  95. while ( n-- )
  96. log( "\t" );
  97. }
  98. static bool snapshot( FILE * fp );
  99. void init ( bool loggable=true )
  100. {
  101. _new_state = _old_state = NULL;
  102. _nest = 0;
  103. if ( loggable )
  104. {
  105. _id = ++_log_id;
  106. ensure_size( _id );
  107. _loggables[ _id - 1 ] = this;
  108. }
  109. else
  110. _id = 0;
  111. }
  112. /* not implemented */
  113. const Loggable & operator= ( const Loggable &rhs );
  114. public:
  115. static const char *escape ( const char *s );
  116. int id ( void ) const { return _id; }
  117. static bool open ( const char *filename );
  118. static bool close ( void );
  119. static void undo ( void );
  120. static int undo_index ( void ) { return _undo_index; }
  121. static void compact ( void );
  122. static
  123. void
  124. block_start ( void )
  125. {
  126. ++Loggable::_level;
  127. }
  128. static
  129. void
  130. block_end ( void )
  131. {
  132. assert( --Loggable::_level >= 0 );
  133. if ( Loggable::_level == 0 )
  134. flush();
  135. }
  136. static
  137. Loggable *
  138. find ( int id )
  139. {
  140. if ( id > _log_id )
  141. return NULL;
  142. return _loggables[ id - 1 ];
  143. }
  144. Loggable ( bool loggable=true )
  145. {
  146. init( loggable );
  147. }
  148. void update_id ( int id );
  149. virtual ~Loggable ( )
  150. {
  151. _loggables[ _id - 1 ] = NULL;
  152. }
  153. static
  154. void
  155. register_create ( const char *name, create_func *func )
  156. {
  157. // printf( "registering %s to %p\n", name, func );
  158. _class_map[ string( name ) ] = func;
  159. }
  160. /* log messages for journal */
  161. virtual void get ( Log_Entry &e ) const = 0;
  162. virtual void set ( Log_Entry &e ) = 0;
  163. virtual const char *class_name ( void ) const = 0;
  164. static bool do_this ( const char *s, bool reverse );
  165. protected:
  166. void log_start ( void );
  167. void log_end ( void );
  168. void log_create ( void ) const;
  169. void log_destroy ( void ) const;
  170. /* leaf subclasses *must* call log_create() at the end of their copy contructors */
  171. Loggable ( const Loggable &rhs )
  172. {
  173. init( true );
  174. /* FIXME: get a real id here!!! */
  175. // _id = 0;
  176. }
  177. public:
  178. // virtual const char *class_name ( void ) const = 0;
  179. friend class Logger;
  180. };
  181. class Logger
  182. {
  183. Loggable *_this;
  184. Logger ( ) {}
  185. /* not permitted */
  186. Logger ( const Logger &rhs );
  187. const Logger & operator= ( const Logger &rhs );
  188. public:
  189. Logger ( Loggable *l ) : _this( l )
  190. {
  191. _this->log_start();
  192. }
  193. ~Logger ( )
  194. {
  195. _this->log_end();
  196. }
  197. void hold ( void )
  198. {
  199. printf( "hold\n" );
  200. _this->_nest++;
  201. }
  202. void release ( void )
  203. {
  204. printf( "release\n" );
  205. _this->_nest--;
  206. assert( _this->_nest );
  207. }
  208. };
  209. class Log_Entry
  210. {
  211. // vector <Pair> _sa;
  212. char **_sa;
  213. int _i;
  214. /* not permitted */
  215. Log_Entry ( const Log_Entry &rhs );
  216. Log_Entry & operator= ( const Log_Entry &rhs );
  217. public:
  218. struct Pair
  219. {
  220. const char *name;
  221. const char *value;
  222. };
  223. Log_Entry ( )
  224. {
  225. _sa = (char**)malloc( sizeof( char * ) );
  226. *_sa = NULL;
  227. _i = 0;
  228. }
  229. Log_Entry ( char **sa )
  230. {
  231. _sa = sa;
  232. _i = 0;
  233. if ( _sa )
  234. while ( _sa[ _i ] ) ++_i;
  235. }
  236. ~Log_Entry ( )
  237. {
  238. if ( ! _sa )
  239. return;
  240. for ( _i = 0; _sa[ _i ]; ++_i )
  241. {
  242. free( _sa[ _i ] );
  243. }
  244. free( _sa );
  245. }
  246. /****************/
  247. /* Construction */
  248. /****************/
  249. void grow ( )
  250. {
  251. _sa = (char**)realloc( _sa, sizeof( char * ) * (_i + 2) );
  252. _sa[ _i + 1 ] = NULL;
  253. }
  254. #define ADD( type, format, exp ) \
  255. void add ( const char *name, type v ) \
  256. { \
  257. grow(); \
  258. asprintf( &_sa[ _i ], "%s " format, name, (exp) ); \
  259. strtok( _sa[ _i++ ], " " ); \
  260. } \
  261. /***************/
  262. /* Examination */
  263. /***************/
  264. int size ( void ) const
  265. {
  266. return _i;
  267. }
  268. void get ( int n, const char **name, const char **value )
  269. {
  270. *name = _sa[ n ];
  271. *value = *name + strlen( *name ) + 1;
  272. }
  273. char **sa ( void )
  274. {
  275. char **sa = _sa;
  276. _sa = NULL;
  277. return sa;
  278. }
  279. /* #define ADD ( type, format, exp ) \ */
  280. /* void add ( const char *name, type v ) \ */
  281. /* { \ */
  282. /* char pat[ 256 ]; \ */
  283. /* Pair p; \ */
  284. /* p.name = strdup( name ); \ */
  285. /* snprintf( pat, sizeof( pat ), format, exp ); \ */
  286. /* p.value = strdup( pat ); \ */
  287. /* _sa.push( p ); \ */
  288. /* } \ */
  289. ADD( int, "%d", v );
  290. ADD( nframes_t, "%lu", (unsigned long)v );
  291. ADD( unsigned long, "%lu", v );
  292. ADD( const char *, "\"%s\"", v ? Loggable::escape( v ) : "" );
  293. ADD( Loggable * , "0x%X", v ? v->id() : 0 );
  294. ADD( float, "%f", v );
  295. ADD( double, "%f", v );
  296. #undef ADD
  297. };