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.

581 lines
12KB

  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. #include "Loggable.H"
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include <string.h>
  23. #include <algorithm>
  24. using std::min;
  25. using std::max;
  26. FILE *Loggable::_fp;
  27. int Loggable::_log_id = 0;
  28. int Loggable::_level = 0;
  29. int Loggable::_undo_index = 1;
  30. size_t Loggable::_loggables_size = 0;
  31. Loggable ** Loggable::_loggables;
  32. std::map <std::string, create_func*> Loggable::_class_map;
  33. std::queue <char *> Loggable::_transaction;
  34. bool
  35. Loggable::open ( const char *filename )
  36. {
  37. FILE *fp;
  38. if ( ! ( fp = fopen( filename, "a+" ) ) )
  39. {
  40. WARNING( "Could not open log file for writing!" );
  41. return false;
  42. }
  43. /* replay log */
  44. {
  45. char buf[BUFSIZ];
  46. while ( fscanf( fp, "%[^\n]\n", buf ) == 1 )
  47. {
  48. if ( ! ( ! strcmp( buf, "{" ) || ! strcmp( buf, "}" ) ) )
  49. {
  50. if ( *buf == '\t' )
  51. do_this( buf + 1, false );
  52. else
  53. do_this( buf, false );
  54. }
  55. }
  56. }
  57. Loggable::_fp = fp;
  58. return true;
  59. }
  60. /** close journal and delete all loggable objects */
  61. bool
  62. Loggable::close ( void )
  63. {
  64. DMESSAGE( "closing journal and destroying all journaled objects" );
  65. if ( ! _fp )
  66. return true;
  67. fclose( _fp );
  68. _fp = NULL;
  69. for ( int i = 0; i < _log_id - 1; ++i )
  70. {
  71. Loggable ** l = &_loggables[ i ];
  72. if ( *l )
  73. {
  74. delete *l;
  75. *l = NULL;
  76. }
  77. }
  78. _log_id = 0;
  79. return true;
  80. }
  81. /** must be called after construction in create() methods */
  82. void
  83. Loggable::update_id ( int id )
  84. {
  85. /* make sure we're the last one */
  86. assert( _id == _log_id );
  87. assert( _loggables[ _id - 1 ] == this );
  88. _loggables[ _id - 1 ] = NULL;
  89. _log_id = max( _log_id, id );
  90. /* return this id number to the system */
  91. // --_log_id;
  92. _id = id;
  93. /* make sure it'll fit */
  94. ensure_size( _id );
  95. ASSERT( ! _loggables[ _id - 1 ], "Attempt to create object with an ID (0x%X) that already exists. The existing object is of type \"%s\", the new one is \"%s\". Corrupt journal?", _id, _loggables[ _id - 1 ]->class_name(), class_name() );
  96. _loggables[ _id - 1 ] = this;
  97. }
  98. /** return a pointer to a static copy of /s/ with all special characters escaped */
  99. const char *
  100. Loggable::escape ( const char *s )
  101. {
  102. static char r[512];
  103. size_t i = 0;
  104. for ( ; *s && i < sizeof( r ); ++i, ++s )
  105. {
  106. if ( '\n' == *s )
  107. {
  108. r[ i++ ] = '\\';
  109. r[ i ] = 'n';
  110. }
  111. else if ( '"' == *s )
  112. {
  113. r[ i++ ] = '\\';
  114. r[ i ] = '"';
  115. }
  116. else
  117. r[ i ] = *s;
  118. }
  119. r[ i ] = '\0';
  120. return r;
  121. }
  122. /** 'do' a message like "Audio_Region 0xF1 set :r 123" */
  123. bool
  124. Loggable::do_this ( const char *s, bool reverse )
  125. {
  126. int id;
  127. if ( ! ( sscanf( s, "%*s %X ", &id ) > 0 ) )
  128. return false;
  129. Loggable *l = find( id );
  130. // assert( l );
  131. char classname[40];
  132. char command[40];
  133. char *arguments = NULL;
  134. const char *create, *destroy;
  135. if ( reverse )
  136. {
  137. // sscanf( s, "%s %*X %s %*[^\n<]<< %a[^\n]", classname, command, &arguments );
  138. sscanf( s, "%s %*X %s%*[^\n<]<< %a[^\n]", classname, command, &arguments );
  139. create = "destroy";
  140. destroy = "create";
  141. }
  142. else
  143. {
  144. sscanf( s, "%s %*X %s %a[^\n<]", classname, command, &arguments );
  145. create = "create";
  146. destroy = "destroy";
  147. }
  148. if ( ! strcmp( command, destroy ) )
  149. {
  150. /* deleting eg. a track, which contains a list of other
  151. widgets, causes destroy messages to be emitted for all those
  152. widgets, but when replaying the journal the destroy message
  153. causes the children to be deleted also... This is a temporary
  154. hack. Would it be better to queue up objects for deletion
  155. (when?) */
  156. if ( l )
  157. delete l;
  158. }
  159. else if ( ! strcmp( command, "set" ) )
  160. {
  161. // printf( "got set command (%s).\n", arguments );
  162. Log_Entry e( arguments );
  163. l->log_start();
  164. l->set( e );
  165. l->log_end();
  166. }
  167. else if ( ! strcmp( command, create ) )
  168. {
  169. Log_Entry e( arguments );
  170. ASSERT( _class_map[ std::string( classname ) ], "Journal contains an object of class \"%s\", but I don't know how to create such objects.", classname );
  171. {
  172. /* create */
  173. Loggable *l = _class_map[ std::string( classname ) ]( e );
  174. l->update_id( id );
  175. l->log_create();
  176. }
  177. }
  178. if ( arguments )
  179. free( arguments );
  180. return true;
  181. }
  182. void
  183. Loggable::undo ( void )
  184. {
  185. char *buf = new char[ BUFSIZ ];
  186. // fflush( _fp );
  187. /* FIXME: handle more than the first block!!! */
  188. fseek( _fp, 0, SEEK_END );
  189. size_t len = ftell( _fp );
  190. fseek( _fp, 0 - (BUFSIZ > len ? len : BUFSIZ), SEEK_END );
  191. len = fread( buf, 1, BUFSIZ, _fp );
  192. char *s = buf + len - 1;
  193. int i = 1;
  194. /* move back _undo_index items from the end */
  195. for ( int j = _undo_index; j-- ; )
  196. for ( --s; *s && s >= buf; --s, ++i )
  197. {
  198. if ( *s == '\n' )
  199. {
  200. if ( *(s + 1) == '\t' )
  201. continue;
  202. if ( *(s + 1) == '}' )
  203. {
  204. *(s + 1) = NULL;
  205. continue;
  206. }
  207. break;
  208. }
  209. }
  210. s++;
  211. buf[ len ] = NULL;
  212. if ( ! strlen( s ) )
  213. {
  214. WARNING( "corrupt undo file or no undo entries." );
  215. return;
  216. }
  217. char *b = s;
  218. s += strlen( s ) - 1;
  219. if ( strtok( b, "\n" ) == NULL )
  220. FATAL( "empty undo transaction!\n" );
  221. int n = 1;
  222. while ( strtok( NULL, "\n" ) )
  223. ++n;
  224. int ui = _undo_index;
  225. block_start();
  226. if ( strcmp( b, "{" ) )
  227. {
  228. /* It's a single undo, get rid of trailing messages in this block */
  229. n = 1;
  230. s = b + 2;
  231. s += strlen( s ) - 1;
  232. }
  233. while ( n-- )
  234. {
  235. while ( s >= b && *(--s) );
  236. s++;
  237. if ( ! strcmp( s, "{" ) )
  238. break;
  239. if ( *s == '\t' )
  240. s++;
  241. DMESSAGE( "undoing \"%s\"", s );
  242. do_this( s, true );
  243. s -= 2;
  244. }
  245. block_end();
  246. _undo_index = ui + 2;
  247. delete[] buf;
  248. }
  249. /** write a snapshot of the state of all loggable objects, sufficient
  250. * for later reconstruction, to /fp/ */
  251. bool
  252. Loggable::snapshot( FILE *fp )
  253. {
  254. FILE *ofp = _fp;
  255. if ( ! ( _fp = fp ) )
  256. {
  257. _fp = ofp;
  258. return false;
  259. }
  260. /* first, make all ids consecutive */
  261. int id = 0;
  262. for ( int i = 0; i < _log_id; ++i )
  263. if ( _loggables[ i ] )
  264. {
  265. ++id;
  266. if ( _loggables[ id - 1 ] )
  267. continue;
  268. _loggables[ id - 1 ] = _loggables[ i ];
  269. _loggables[ i ] = NULL;
  270. _loggables[ id - 1 ]->_id = id;
  271. }
  272. _log_id = id;
  273. block_start();
  274. for ( int i = 0; i < _log_id; ++i )
  275. {
  276. const Loggable * l = _loggables[ i ];
  277. if ( l && _class_map[ std::string( l->class_name() ) ] )
  278. l->log_create();
  279. }
  280. block_end();
  281. _fp = ofp;
  282. return true;
  283. }
  284. void
  285. Loggable::compact ( void )
  286. {
  287. fseek( _fp, 0, SEEK_SET );
  288. ftruncate( fileno( _fp ), 0 );
  289. snapshot( _fp );
  290. _undo_index = 1;
  291. }
  292. void
  293. Loggable::log ( const char *fmt, ... )
  294. {
  295. /* FIXME: bogus limit */
  296. static char buf[1024];
  297. static int i = 0;
  298. va_list args;
  299. if ( fmt )
  300. {
  301. va_start( args, fmt );
  302. i += vsprintf( buf + i, fmt, args );
  303. va_end( args );
  304. }
  305. if ( rindex( buf, '\n' ) )
  306. {
  307. _transaction.push( strdup( buf ) );
  308. i = 0;
  309. }
  310. }
  311. void
  312. Loggable::flush ( void )
  313. {
  314. if ( ! _fp )
  315. {
  316. // printf( "error: no log file open!\n" );
  317. while ( ! _transaction.empty() )
  318. {
  319. free( _transaction.front() );
  320. _transaction.pop();
  321. }
  322. return;
  323. }
  324. int n = _transaction.size();
  325. if ( n )
  326. /* something done, reset undo index */
  327. _undo_index = 1;
  328. if ( n > 1 )
  329. fprintf( _fp, "{\n" );
  330. while ( ! _transaction.empty() )
  331. {
  332. char *s = _transaction.front();
  333. _transaction.pop();
  334. if ( n > 1 )
  335. fprintf( _fp, "\t" );
  336. fprintf( _fp, "%s", s );
  337. free( s );
  338. }
  339. if ( n > 1 )
  340. fprintf( _fp, "}\n" );
  341. fflush( _fp );
  342. }
  343. void
  344. Loggable::log_print( char **o, char **n ) const
  345. {
  346. if ( n )
  347. for ( ; *n; n++ )
  348. log( "%s %s%s", *n, *n + strlen( *n ) + 1, *(n + 1) ? " " : "" );
  349. if ( o && *o )
  350. {
  351. if ( n ) log( " << " );
  352. for ( ; *o; o++ )
  353. log( "%s %s%s", *o, *o + strlen( *o ) + 1, *(o + 1) ? " " : "" );
  354. }
  355. log( "\n" );
  356. }
  357. void
  358. Loggable::log_start ( void )
  359. {
  360. if ( ! _old_state )
  361. {
  362. _old_state = new Log_Entry;
  363. get( *_old_state );
  364. }
  365. ++_nest;
  366. }
  367. void
  368. Loggable::log_end ( void )
  369. {
  370. if ( --_nest > 0 )
  371. return;
  372. Log_Entry *_new_state;
  373. {
  374. _new_state = new Log_Entry;
  375. get( *_new_state );
  376. }
  377. if ( *_old_state != *_new_state )
  378. {
  379. // indent();
  380. log( "%s 0x%X set ", class_name(), _id );
  381. log_print( _old_state->sa(), _new_state->sa() );
  382. }
  383. if ( _new_state )
  384. delete _new_state;
  385. if ( _old_state )
  386. delete _old_state;
  387. _old_state = NULL;
  388. if ( Loggable::_level == 0 )
  389. Loggable::flush();
  390. }
  391. void
  392. Loggable::log_create ( void ) const
  393. {
  394. // indent();
  395. /* if ( Loggable::_level ) */
  396. /* { */
  397. /* /\* defer until the block ends--this is really only required for capturing... *\/ */
  398. /* } */
  399. log( "%s 0x%X create ", class_name(), _id );
  400. char **sa;
  401. Log_Entry e;
  402. get( e );
  403. sa = e.sa();
  404. if ( sa )
  405. {
  406. log_print( NULL, sa );
  407. }
  408. else
  409. log( "\n" );
  410. if ( Loggable::_level == 0 )
  411. Loggable::flush();
  412. }
  413. void
  414. Loggable::log_destroy ( void ) const
  415. {
  416. if ( ! _fp )
  417. /* tearing down... don't bother */
  418. return;
  419. log( "%s 0x%X destroy << ", class_name(), _id );
  420. Log_Entry e;
  421. get( e );
  422. log_print( NULL, e.sa() );
  423. if ( Loggable::_level == 0 )
  424. Loggable::flush();
  425. }