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.

339 lines
7.5KB

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