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.

335 lines
7.4KB

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