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.

345 lines
8.1KB

  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. class Log_Entry;
  31. class Loggable;
  32. typedef Loggable *(create_func)(Log_Entry &);
  33. #define LOG_REGISTER_CREATE( class ) \
  34. Loggable::register_create( #class, & class ::create );
  35. #define LOG_NAME_FUNC( class ) \
  36. virtual const char *class_name ( void ) const { return #class ; }
  37. #define LOG_CREATE_FUNC( class ) \
  38. static Loggable * \
  39. create ( Log_Entry &e ) \
  40. { \
  41. class *r = new class; \
  42. r->set( e ); \
  43. return (Loggable *)r; \
  44. } \
  45. LOG_NAME_FUNC( class ); \
  46. class Logger;
  47. class Loggable
  48. {
  49. static FILE *_fp;
  50. static int _log_id;
  51. static int _level;
  52. static int _undo_index;
  53. static vector <Loggable *> _loggables;
  54. static map <string, create_func*> _class_map;
  55. static queue <char *> _transaction;
  56. private:
  57. int _id;
  58. char **_old_state;
  59. char **_new_state;
  60. int _nest;
  61. void log_print( char **o, char **n ) const;
  62. static void log ( const char *fmt, ... );
  63. static void flush ( void );
  64. static
  65. void indent ( void )
  66. {
  67. int n = Loggable::_level;
  68. while ( n-- )
  69. log( "\t" );
  70. }
  71. static bool snapshot( FILE * fp );
  72. public:
  73. static bool open ( const char *filename );
  74. static void undo ( void );
  75. static int undo_index ( void ) { return _undo_index; }
  76. static void compact ( void );
  77. static
  78. void
  79. block_start ( void )
  80. {
  81. ++Loggable::_level;
  82. }
  83. static
  84. void
  85. block_end ( void )
  86. {
  87. assert( --Loggable::_level >= 0 );
  88. if ( Loggable::_level == 0 )
  89. flush();
  90. }
  91. static
  92. Loggable *
  93. find ( int id )
  94. {
  95. if ( id > _log_id )
  96. return NULL;
  97. return _loggables[ id - 1 ];
  98. }
  99. Loggable ( )
  100. {
  101. _id = ++_log_id;
  102. _old_state = NULL;
  103. _nest = 0;
  104. _loggables.push_back( this );
  105. }
  106. /** must be called after construction in create() methods */
  107. void
  108. update_id ( int id )
  109. {
  110. assert( _id == _log_id );
  111. assert( _loggables[ _id - 1 ] == this );
  112. _loggables[ _id - 1 ] = NULL;
  113. // --_log_id;
  114. _id = id;
  115. /* make sure it'll fit */
  116. _loggables.reserve( _id );
  117. assert( ! _loggables[ _id - 1 ] );
  118. _loggables[ _id - 1 ] = this;
  119. }
  120. virtual ~Loggable ( )
  121. {
  122. _loggables[ _id - 1 ] = NULL;
  123. }
  124. static
  125. void
  126. register_create ( const char *name, create_func *func )
  127. {
  128. // printf( "registering %s to %p\n", name, func );
  129. _class_map[ string( name ) ] = func;
  130. }
  131. /* log messages for journal */
  132. virtual void get ( Log_Entry &e ) const = 0;
  133. virtual void set ( Log_Entry &e ) = 0;
  134. static bool do_this ( const char *s, bool reverse );
  135. protected:
  136. void log_start ( void );
  137. void log_end ( void );
  138. void log_create ( void ) const;
  139. void log_destroy ( void ) const;
  140. public:
  141. virtual const char *class_name ( void ) const = 0;
  142. int id ( void ) { return _id; }
  143. friend class Logger;
  144. };
  145. class Logger
  146. {
  147. Loggable *_this;
  148. Logger ( ) {}
  149. public:
  150. Logger ( Loggable *l ) : _this( l )
  151. {
  152. _this->log_start();
  153. }
  154. ~Logger ( )
  155. {
  156. _this->log_end();
  157. }
  158. void hold ( void )
  159. {
  160. printf( "hold\n" );
  161. _this->_nest++;
  162. }
  163. void release ( void )
  164. {
  165. printf( "release\n" );
  166. _this->_nest--;
  167. assert( _this->_nest );
  168. }
  169. };
  170. class Log_Entry
  171. {
  172. // vector <Pair> _sa;
  173. char **_sa;
  174. int _i;
  175. public:
  176. struct Pair
  177. {
  178. const char *name;
  179. const char *value;
  180. };
  181. Log_Entry ( )
  182. {
  183. _sa = (char**)malloc( sizeof( char * ) );
  184. *_sa = NULL;
  185. _i = 0;
  186. }
  187. Log_Entry ( char **sa )
  188. {
  189. _sa = sa;
  190. _i = 0;
  191. if ( _sa )
  192. while ( _sa[ _i ] ) ++_i;
  193. }
  194. ~Log_Entry ( )
  195. {
  196. if ( ! _sa )
  197. return;
  198. for ( _i = 0; _sa[ _i ]; ++_i )
  199. {
  200. free( _sa[ _i ] );
  201. }
  202. free( _sa );
  203. }
  204. /****************/
  205. /* Construction */
  206. /****************/
  207. void grow ( )
  208. {
  209. _sa = (char**)realloc( _sa, sizeof( char * ) * (_i + 2) );
  210. _sa[ _i + 1 ] = NULL;
  211. }
  212. #define ADD( type, format, exp ) \
  213. void add ( const char *name, type v ) \
  214. { \
  215. grow(); \
  216. asprintf( &_sa[ _i ], "%s " format, name, (exp) ); \
  217. strtok( _sa[ _i++ ], " " ); \
  218. } \
  219. /***************/
  220. /* Examination */
  221. /***************/
  222. int size ( void ) const
  223. {
  224. return _i;
  225. }
  226. void get ( int n, const char **name, const char **value )
  227. {
  228. *name = _sa[ n ];
  229. *value = *name + strlen( *name ) + 1;
  230. }
  231. char **sa ( void )
  232. {
  233. char **sa = _sa;
  234. _sa = NULL;
  235. return sa;
  236. }
  237. /* #define ADD ( type, format, exp ) \ */
  238. /* void add ( const char *name, type v ) \ */
  239. /* { \ */
  240. /* char pat[ 256 ]; \ */
  241. /* Pair p; \ */
  242. /* p.name = strdup( name ); \ */
  243. /* snprintf( pat, sizeof( pat ), format, exp ); \ */
  244. /* p.value = strdup( pat ); \ */
  245. /* _sa.push( p ); \ */
  246. /* } \ */
  247. ADD( int, "%d", v );
  248. ADD( nframes_t, "%lu", (unsigned long)v );
  249. ADD( unsigned long, "%lu", v );
  250. ADD( const char *, "\"%s\"", v ? v : "" );
  251. ADD( Loggable *, "0x%X", v ? v->id() : 0 );
  252. ADD( float, "%f", v );
  253. ADD( double, "%f", v );
  254. #undef ADD
  255. };