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.

170 lines
4.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. using std::vector;
  26. class Logger;
  27. class Loggable
  28. {
  29. static FILE *_fp;
  30. static int _log_id;
  31. static int _level;
  32. static int _undo_index;
  33. static vector <Loggable *> _loggables;
  34. private:
  35. int _id;
  36. char **_old_state;
  37. char **_new_state;
  38. int _nest;
  39. void log_print( char **o, char **n );
  40. static void log ( const char *fmt, ... );
  41. static
  42. void indent ( void )
  43. {
  44. int n = Loggable::_level;
  45. while ( n-- )
  46. log( "\t" );
  47. }
  48. public:
  49. static bool open ( const char *filename );
  50. static void undo ( void );
  51. static
  52. void
  53. block_start ( void )
  54. {
  55. indent();
  56. log( "{\n" );
  57. ++Loggable::_level;
  58. }
  59. static
  60. void
  61. block_end ( void )
  62. {
  63. assert( --Loggable::_level >= 0 );
  64. indent();
  65. log( "}\n" );
  66. }
  67. static
  68. Loggable *
  69. find ( int id )
  70. {
  71. if ( id > _log_id )
  72. return NULL;
  73. return _loggables[ id - 1 ];
  74. }
  75. Loggable ( )
  76. {
  77. _id = ++_log_id;
  78. _old_state = NULL;
  79. _nest = 0;
  80. _loggables.push_back( this );
  81. }
  82. /* log messages for journal */
  83. virtual const char *class_name ( void ) = 0;
  84. virtual char ** log_dump ( void ) = 0;
  85. /* this method must parse an array of name/value pair strings and make the appropriate changes too
  86. the object state */
  87. virtual void set ( char **sa ) = 0;
  88. // void log ( const char *module, const char *action, const char *fmt, ... );
  89. protected:
  90. void log_start ( void );
  91. void log_end ( void );
  92. void log_create ( void );
  93. void log_destroy ( void );
  94. public:
  95. int id ( void ) { return _id; }
  96. friend class Logger;
  97. };
  98. class Logger
  99. {
  100. Loggable *_this;
  101. Logger ( ) {}
  102. public:
  103. Logger ( Loggable *l ) : _this( l )
  104. {
  105. _this->log_start();
  106. }
  107. ~Logger ( )
  108. {
  109. _this->log_end();
  110. }
  111. void hold ( void )
  112. {
  113. printf( "hold\n" );
  114. _this->_nest++;
  115. }
  116. void release ( void )
  117. {
  118. printf( "release\n" );
  119. _this->_nest--;
  120. assert( _this->_nest );
  121. }
  122. };
  123. /* #ifndef _LOGGABLE_C */
  124. /* #define log( act, fmt, args... ) log( __CLASS__, act, fmt, ## args ) */
  125. /* #endif */
  126. /* #define LOG_START Logger _logger( this ) */
  127. /* #define LOG_END _logger.print( this ) */