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.

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