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.

218 lines
5.0KB

  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. ++Loggable::_level;
  66. }
  67. static
  68. void
  69. block_end ( void )
  70. {
  71. assert( --Loggable::_level >= 0 );
  72. if ( Loggable::_level == 0 )
  73. flush();
  74. }
  75. static
  76. Loggable *
  77. find ( int id )
  78. {
  79. if ( id > _log_id )
  80. return NULL;
  81. return _loggables[ id - 1 ];
  82. }
  83. Loggable ( )
  84. {
  85. _id = ++_log_id;
  86. _old_state = NULL;
  87. _nest = 0;
  88. _loggables.push_back( this );
  89. }
  90. /** must be called after construction in create() methods */
  91. void
  92. update_id ( int id )
  93. {
  94. assert( _id == _log_id );
  95. _loggables[ _id - 1 ] = NULL;
  96. --_log_id;
  97. _id = id;
  98. assert( ! _loggables[ _id - 1 ] );
  99. _loggables[ _id - 1 ] = this;
  100. }
  101. virtual ~Loggable ( )
  102. {
  103. _loggables[ _id - 1 ] = NULL;
  104. }
  105. static
  106. void
  107. register_create ( const char *name, create_func *func )
  108. {
  109. printf( "registering %s to %p\n", name, func );
  110. _class_map[ string( name ) ] = func;
  111. }
  112. /* log messages for journal */
  113. virtual const char *class_name ( void ) = 0;
  114. virtual char ** get ( void ) = 0;
  115. /* this method must parse an array of name/value pair strings and make the appropriate changes too
  116. the object state */
  117. virtual void set ( char **sa ) = 0;
  118. // void log ( const char *module, const char *action, const char *fmt, ... );
  119. static bool do_this ( const char *s, bool reverse );
  120. protected:
  121. void log_start ( void );
  122. void log_end ( void );
  123. void log_create ( void );
  124. void log_destroy ( void );
  125. public:
  126. int id ( void ) { return _id; }
  127. friend class Logger;
  128. };
  129. class Logger
  130. {
  131. Loggable *_this;
  132. Logger ( ) {}
  133. public:
  134. Logger ( Loggable *l ) : _this( l )
  135. {
  136. _this->log_start();
  137. }
  138. ~Logger ( )
  139. {
  140. _this->log_end();
  141. }
  142. void hold ( void )
  143. {
  144. printf( "hold\n" );
  145. _this->_nest++;
  146. }
  147. void release ( void )
  148. {
  149. printf( "release\n" );
  150. _this->_nest--;
  151. assert( _this->_nest );
  152. }
  153. };
  154. /* #ifndef _LOGGABLE_C */
  155. /* #define log( act, fmt, args... ) log( __CLASS__, act, fmt, ## args ) */
  156. /* #endif */
  157. /* #define LOG_START Logger _logger( this ) */
  158. /* #define LOG_END _logger.print( this ) */