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.

379 lines
9.0KB

  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. public:
  100. static const char *escape ( const char *s );
  101. int id ( void ) const { return _id; }
  102. static bool open ( const char *filename );
  103. static bool close ( void );
  104. static void undo ( void );
  105. static int undo_index ( void ) { return _undo_index; }
  106. static void compact ( void );
  107. static
  108. void
  109. block_start ( void )
  110. {
  111. ++Loggable::_level;
  112. }
  113. static
  114. void
  115. block_end ( void )
  116. {
  117. assert( --Loggable::_level >= 0 );
  118. if ( Loggable::_level == 0 )
  119. flush();
  120. }
  121. static
  122. Loggable *
  123. find ( int id )
  124. {
  125. if ( id > _log_id )
  126. return NULL;
  127. return _loggables[ id - 1 ];
  128. }
  129. Loggable ( bool loggable=true )
  130. {
  131. if ( loggable )
  132. {
  133. _id = ++_log_id;
  134. _old_state = NULL;
  135. _nest = 0;
  136. ensure_size( _id );
  137. _loggables[ _id - 1 ] = this;
  138. }
  139. else
  140. _id = 0;
  141. }
  142. void update_id ( int id );
  143. virtual ~Loggable ( )
  144. {
  145. _loggables[ _id - 1 ] = NULL;
  146. }
  147. static
  148. void
  149. register_create ( const char *name, create_func *func )
  150. {
  151. // printf( "registering %s to %p\n", name, func );
  152. _class_map[ string( name ) ] = func;
  153. }
  154. /* log messages for journal */
  155. virtual void get ( Log_Entry &e ) const = 0;
  156. virtual void set ( Log_Entry &e ) = 0;
  157. virtual const char *class_name ( void ) const = 0;
  158. static bool do_this ( const char *s, bool reverse );
  159. protected:
  160. void log_start ( void );
  161. void log_end ( void );
  162. void log_create ( void ) const;
  163. void log_destroy ( void ) const;
  164. public:
  165. // virtual const char *class_name ( void ) const = 0;
  166. friend class Logger;
  167. };
  168. class Logger
  169. {
  170. Loggable *_this;
  171. Logger ( ) {}
  172. public:
  173. Logger ( Loggable *l ) : _this( l )
  174. {
  175. _this->log_start();
  176. }
  177. ~Logger ( )
  178. {
  179. _this->log_end();
  180. }
  181. void hold ( void )
  182. {
  183. printf( "hold\n" );
  184. _this->_nest++;
  185. }
  186. void release ( void )
  187. {
  188. printf( "release\n" );
  189. _this->_nest--;
  190. assert( _this->_nest );
  191. }
  192. };
  193. class Log_Entry
  194. {
  195. // vector <Pair> _sa;
  196. char **_sa;
  197. int _i;
  198. public:
  199. struct Pair
  200. {
  201. const char *name;
  202. const char *value;
  203. };
  204. Log_Entry ( )
  205. {
  206. _sa = (char**)malloc( sizeof( char * ) );
  207. *_sa = NULL;
  208. _i = 0;
  209. }
  210. Log_Entry ( char **sa )
  211. {
  212. _sa = sa;
  213. _i = 0;
  214. if ( _sa )
  215. while ( _sa[ _i ] ) ++_i;
  216. }
  217. ~Log_Entry ( )
  218. {
  219. if ( ! _sa )
  220. return;
  221. for ( _i = 0; _sa[ _i ]; ++_i )
  222. {
  223. free( _sa[ _i ] );
  224. }
  225. free( _sa );
  226. }
  227. /****************/
  228. /* Construction */
  229. /****************/
  230. void grow ( )
  231. {
  232. _sa = (char**)realloc( _sa, sizeof( char * ) * (_i + 2) );
  233. _sa[ _i + 1 ] = NULL;
  234. }
  235. #define ADD( type, format, exp ) \
  236. void add ( const char *name, type v ) \
  237. { \
  238. grow(); \
  239. asprintf( &_sa[ _i ], "%s " format, name, (exp) ); \
  240. strtok( _sa[ _i++ ], " " ); \
  241. } \
  242. /***************/
  243. /* Examination */
  244. /***************/
  245. int size ( void ) const
  246. {
  247. return _i;
  248. }
  249. void get ( int n, const char **name, const char **value )
  250. {
  251. *name = _sa[ n ];
  252. *value = *name + strlen( *name ) + 1;
  253. }
  254. char **sa ( void )
  255. {
  256. char **sa = _sa;
  257. _sa = NULL;
  258. return sa;
  259. }
  260. /* #define ADD ( type, format, exp ) \ */
  261. /* void add ( const char *name, type v ) \ */
  262. /* { \ */
  263. /* char pat[ 256 ]; \ */
  264. /* Pair p; \ */
  265. /* p.name = strdup( name ); \ */
  266. /* snprintf( pat, sizeof( pat ), format, exp ); \ */
  267. /* p.value = strdup( pat ); \ */
  268. /* _sa.push( p ); \ */
  269. /* } \ */
  270. ADD( int, "%d", v );
  271. ADD( nframes_t, "%lu", (unsigned long)v );
  272. ADD( unsigned long, "%lu", v );
  273. ADD( const char *, "\"%s\"", v ? Loggable::escape( v ) : "" );
  274. ADD( Loggable * , "0x%X", v ? v->id() : 0 );
  275. ADD( float, "%f", v );
  276. ADD( double, "%f", v );
  277. #undef ADD
  278. };