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.

195 lines
4.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. using std::vector;
  26. #include <map>
  27. using std::map;
  28. #include <string>
  29. using std::string;
  30. class Loggable;
  31. typedef Loggable *(create_func)(char **);
  32. class Logger;
  33. class Loggable
  34. {
  35. static FILE *_fp;
  36. static int _log_id;
  37. static int _level;
  38. static int _undo_index;
  39. static vector <Loggable *> _loggables;
  40. static map <string, create_func*> _class_map;
  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
  49. void indent ( void )
  50. {
  51. int n = Loggable::_level;
  52. while ( n-- )
  53. log( "\t" );
  54. }
  55. public:
  56. static bool open ( const char *filename );
  57. static void undo ( void );
  58. static int undo_index ( void ) { return _undo_index; }
  59. static
  60. void
  61. block_start ( void )
  62. {
  63. indent();
  64. // log( "{\n" );
  65. ++Loggable::_level;
  66. }
  67. static
  68. void
  69. block_end ( void )
  70. {
  71. assert( --Loggable::_level >= 0 );
  72. indent();
  73. // log( "}\n" );
  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. virtual ~Loggable ( )
  91. {
  92. }
  93. static
  94. void
  95. register_create ( const char *name, create_func *func )
  96. {
  97. printf( "registering %s to %p\n", name, func );
  98. _class_map[ string( name ) ] = func;
  99. }
  100. /* log messages for journal */
  101. virtual const char *class_name ( void ) = 0;
  102. virtual char ** log_dump ( void ) = 0;
  103. /* this method must parse an array of name/value pair strings and make the appropriate changes too
  104. the object state */
  105. virtual void set ( char **sa ) = 0;
  106. // void log ( const char *module, const char *action, const char *fmt, ... );
  107. protected:
  108. void log_start ( void );
  109. void log_end ( void );
  110. void log_create ( void );
  111. void log_destroy ( void );
  112. public:
  113. int id ( void ) { return _id; }
  114. friend class Logger;
  115. };
  116. class Logger
  117. {
  118. Loggable *_this;
  119. Logger ( ) {}
  120. public:
  121. Logger ( Loggable *l ) : _this( l )
  122. {
  123. _this->log_start();
  124. }
  125. ~Logger ( )
  126. {
  127. _this->log_end();
  128. }
  129. void hold ( void )
  130. {
  131. printf( "hold\n" );
  132. _this->_nest++;
  133. }
  134. void release ( void )
  135. {
  136. printf( "release\n" );
  137. _this->_nest--;
  138. assert( _this->_nest );
  139. }
  140. };
  141. /* #ifndef _LOGGABLE_C */
  142. /* #define log( act, fmt, args... ) log( __CLASS__, act, fmt, ## args ) */
  143. /* #endif */
  144. /* #define LOG_START Logger _logger( this ) */
  145. /* #define LOG_END _logger.print( this ) */