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.

386 lines
9.2KB

  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 <map>
  25. #include <string>
  26. #include <queue>
  27. #include "types.h"
  28. #include "util/debug.h"
  29. class Log_Entry;
  30. class Loggable;
  31. typedef Loggable *(create_func)(Log_Entry &);
  32. #define LOG_REGISTER_CREATE( class ) \
  33. Loggable::register_create( #class, & class ::create );
  34. #define LOG_NAME_FUNC( class ) \
  35. virtual const char *class_name ( void ) const { return #class ; }
  36. #define LOG_CREATE_FUNC( class ) \
  37. static Loggable * \
  38. create ( Log_Entry &e ) \
  39. { \
  40. class *r = new class; \
  41. r->set( e ); \
  42. return (Loggable *)r; \
  43. } \
  44. LOG_NAME_FUNC( class ); \
  45. #define LOG_NOT_LOGGABLE_FUNC( class ) \
  46. virtual const char *class_name ( void ) const { return #class ; } \
  47. class Logger;
  48. class Loggable
  49. {
  50. static FILE *_fp;
  51. static int _log_id;
  52. static int _level;
  53. static int _undo_index;
  54. static size_t _loggables_size;
  55. static Loggable ** _loggables;
  56. static std::map <std::string, create_func*> _class_map;
  57. static std::queue <char *> _transaction;
  58. private:
  59. int _id;
  60. char **_old_state;
  61. char **_new_state;
  62. int _nest;
  63. static void ensure_size ( size_t n )
  64. {
  65. if ( n > _loggables_size )
  66. {
  67. size_t p = 0;
  68. while ( ( (unsigned)1 << p ) < n ) ++p;
  69. size_t os = _loggables_size;
  70. _loggables_size = 1 << p ;
  71. _loggables = (Loggable**) realloc( _loggables, sizeof( Loggable ** ) * _loggables_size );
  72. memset( _loggables + os, 0, _loggables_size - os );
  73. }
  74. }
  75. void log_print( char **o, char **n ) const;
  76. static void log ( const char *fmt, ... );
  77. static void flush ( void );
  78. static
  79. void indent ( void )
  80. {
  81. int n = Loggable::_level;
  82. while ( n-- )
  83. log( "\t" );
  84. }
  85. static bool snapshot( FILE * fp );
  86. void init ( bool loggable=true )
  87. {
  88. _new_state = _old_state = NULL;
  89. _nest = 0;
  90. if ( loggable )
  91. {
  92. _id = ++_log_id;
  93. ensure_size( _id );
  94. _loggables[ _id - 1 ] = this;
  95. }
  96. else
  97. _id = 0;
  98. }
  99. /* not implemented */
  100. const Loggable & operator= ( const Loggable &rhs );
  101. public:
  102. static const char *escape ( const char *s );
  103. int id ( void ) const { return _id; }
  104. static bool open ( const char *filename );
  105. static bool close ( void );
  106. static void undo ( void );
  107. static int undo_index ( void ) { return _undo_index; }
  108. static void compact ( void );
  109. static
  110. void
  111. block_start ( void )
  112. {
  113. ++Loggable::_level;
  114. }
  115. static
  116. void
  117. block_end ( void )
  118. {
  119. assert( --Loggable::_level >= 0 );
  120. if ( Loggable::_level == 0 )
  121. flush();
  122. }
  123. static
  124. Loggable *
  125. find ( int id )
  126. {
  127. if ( id > _log_id )
  128. return NULL;
  129. return _loggables[ id - 1 ];
  130. }
  131. Loggable ( bool loggable=true )
  132. {
  133. init( loggable );
  134. }
  135. void update_id ( int id );
  136. virtual ~Loggable ( )
  137. {
  138. _loggables[ _id - 1 ] = NULL;
  139. }
  140. static
  141. void
  142. register_create ( const char *name, create_func *func )
  143. {
  144. // printf( "registering %s to %p\n", name, func );
  145. _class_map[ std::string( name ) ] = func;
  146. }
  147. /* log messages for journal */
  148. virtual void get ( Log_Entry &e ) const = 0;
  149. virtual void set ( Log_Entry &e ) = 0;
  150. virtual const char *class_name ( void ) const = 0;
  151. static bool do_this ( const char *s, bool reverse );
  152. protected:
  153. void log_start ( void );
  154. void log_end ( void );
  155. void log_create ( void ) const;
  156. void log_destroy ( void ) const;
  157. /* leaf subclasses *must* call log_create() at the end of their copy contructors */
  158. Loggable ( const Loggable & )
  159. {
  160. init( true );
  161. /* FIXME: get a real id here!!! */
  162. // _id = 0;
  163. }
  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. /* not permitted */
  173. Logger ( const Logger &rhs );
  174. const Logger & operator= ( const Logger &rhs );
  175. public:
  176. Logger ( Loggable *l ) : _this( l )
  177. {
  178. _this->log_start();
  179. }
  180. ~Logger ( )
  181. {
  182. _this->log_end();
  183. }
  184. void hold ( void )
  185. {
  186. printf( "hold\n" );
  187. _this->_nest++;
  188. }
  189. void release ( void )
  190. {
  191. printf( "release\n" );
  192. _this->_nest--;
  193. assert( _this->_nest );
  194. }
  195. };
  196. class Log_Entry
  197. {
  198. // vector <Pair> _sa;
  199. char **_sa;
  200. int _i;
  201. /* not permitted */
  202. Log_Entry ( const Log_Entry &rhs );
  203. Log_Entry & operator= ( const Log_Entry &rhs );
  204. public:
  205. struct Pair
  206. {
  207. const char *name;
  208. const char *value;
  209. };
  210. Log_Entry ( )
  211. {
  212. _sa = (char**)malloc( sizeof( char * ) );
  213. *_sa = NULL;
  214. _i = 0;
  215. }
  216. Log_Entry ( char **sa )
  217. {
  218. _sa = sa;
  219. _i = 0;
  220. if ( _sa )
  221. while ( _sa[ _i ] ) ++_i;
  222. }
  223. ~Log_Entry ( )
  224. {
  225. if ( ! _sa )
  226. return;
  227. for ( _i = 0; _sa[ _i ]; ++_i )
  228. {
  229. free( _sa[ _i ] );
  230. }
  231. free( _sa );
  232. }
  233. /****************/
  234. /* Construction */
  235. /****************/
  236. void grow ( )
  237. {
  238. _sa = (char**)realloc( _sa, sizeof( char * ) * (_i + 2) );
  239. _sa[ _i + 1 ] = NULL;
  240. }
  241. #define ADD( type, format, exp ) \
  242. void add ( const char *name, type v ) \
  243. { \
  244. grow(); \
  245. asprintf( &_sa[ _i ], "%s " format, name, (exp) ); \
  246. strtok( _sa[ _i++ ], " " ); \
  247. } \
  248. /***************/
  249. /* Examination */
  250. /***************/
  251. int size ( void ) const
  252. {
  253. return _i;
  254. }
  255. void get ( int n, const char **name, const char **value )
  256. {
  257. *name = _sa[ n ];
  258. *value = *name + strlen( *name ) + 1;
  259. }
  260. char **sa ( void )
  261. {
  262. char **sa = _sa;
  263. _sa = NULL;
  264. return sa;
  265. }
  266. /* #define ADD ( type, format, exp ) \ */
  267. /* void add ( const char *name, type v ) \ */
  268. /* { \ */
  269. /* char pat[ 256 ]; \ */
  270. /* Pair p; \ */
  271. /* p.name = strdup( name ); \ */
  272. /* snprintf( pat, sizeof( pat ), format, exp ); \ */
  273. /* p.value = strdup( pat ); \ */
  274. /* _sa.push( p ); \ */
  275. /* } \ */
  276. ADD( int, "%d", v );
  277. ADD( nframes_t, "%lu", (unsigned long)v );
  278. ADD( unsigned long, "%lu", v );
  279. ADD( const char *, "\"%s\"", v ? Loggable::escape( v ) : "" );
  280. ADD( Loggable * , "0x%X", v ? v->id() : 0 );
  281. ADD( float, "%f", v );
  282. ADD( double, "%f", v );
  283. #undef ADD
  284. };