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.

215 lines
4.9KB

  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. class Loggable;
  30. typedef Loggable *(create_func)(char **);
  31. class Logger;
  32. class Loggable
  33. {
  34. static FILE *_fp;
  35. static int _log_id;
  36. static int _level;
  37. static int _undo_index;
  38. static vector <Loggable *> _loggables;
  39. static map <string, create_func*> _class_map;
  40. static queue <char *> _transaction;
  41. private:
  42. int _id;
  43. char **_old_state;
  44. char **_new_state;
  45. int _nest;
  46. void log_print( char **o, char **n );
  47. static void log ( const char *fmt, ... );
  48. static void flush ( void );
  49. static
  50. void indent ( void )
  51. {
  52. int n = Loggable::_level;
  53. while ( n-- )
  54. log( "\t" );
  55. }
  56. public:
  57. static bool open ( const char *filename );
  58. static void undo ( void );
  59. static int undo_index ( void ) { return _undo_index; }
  60. static bool snapshot( const char *file );
  61. static
  62. void
  63. block_start ( void )
  64. {
  65. // indent();
  66. // log( "{\n" );
  67. ++Loggable::_level;
  68. }
  69. static
  70. void
  71. block_end ( void )
  72. {
  73. assert( --Loggable::_level >= 0 );
  74. // indent();
  75. // log( "}\n" );
  76. }
  77. static
  78. Loggable *
  79. find ( int id )
  80. {
  81. if ( id > _log_id )
  82. return NULL;
  83. return _loggables[ id - 1 ];
  84. }
  85. Loggable ( )
  86. {
  87. _id = ++_log_id;
  88. _old_state = NULL;
  89. _nest = 0;
  90. _loggables.push_back( this );
  91. }
  92. /** must be called after construction in create() methods */
  93. void
  94. update_id ( int id )
  95. {
  96. assert( _id == _log_id );
  97. _loggables[ _id - 1 ] = NULL;
  98. --_log_id;
  99. _id = id;
  100. assert( ! _loggables[ _id - 1 ] );
  101. _loggables[ _id - 1 ] = this;
  102. }
  103. virtual ~Loggable ( )
  104. {
  105. _loggables[ _id - 1 ] = NULL;
  106. }
  107. static
  108. void
  109. register_create ( const char *name, create_func *func )
  110. {
  111. printf( "registering %s to %p\n", name, func );
  112. _class_map[ string( name ) ] = func;
  113. }
  114. /* log messages for journal */
  115. virtual const char *class_name ( void ) = 0;
  116. virtual char ** get ( void ) = 0;
  117. /* this method must parse an array of name/value pair strings and make the appropriate changes too
  118. the object state */
  119. virtual void set ( char **sa ) = 0;
  120. // void log ( const char *module, const char *action, const char *fmt, ... );
  121. protected:
  122. void log_start ( void );
  123. void log_end ( void );
  124. void log_create ( void );
  125. void log_destroy ( void );
  126. public:
  127. int id ( void ) { return _id; }
  128. friend class Logger;
  129. };
  130. class Logger
  131. {
  132. Loggable *_this;
  133. Logger ( ) {}
  134. public:
  135. Logger ( Loggable *l ) : _this( l )
  136. {
  137. _this->log_start();
  138. }
  139. ~Logger ( )
  140. {
  141. _this->log_end();
  142. }
  143. void hold ( void )
  144. {
  145. printf( "hold\n" );
  146. _this->_nest++;
  147. }
  148. void release ( void )
  149. {
  150. printf( "release\n" );
  151. _this->_nest--;
  152. assert( _this->_nest );
  153. }
  154. };
  155. /* #ifndef _LOGGABLE_C */
  156. /* #define log( act, fmt, args... ) log( __CLASS__, act, fmt, ## args ) */
  157. /* #endif */
  158. /* #define LOG_START Logger _logger( this ) */
  159. /* #define LOG_END _logger.print( this ) */