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.

669 lines
14KB

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