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.

771 lines
17KB

  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. /* This class handles all journaling. All journaled objects must
  19. inherit from Loggable as well as define a few special methods (via
  20. macros), get and set methods, and have contructors and destructors
  21. that call log_create() and log_destroy() in the appropriate
  22. order. Any action that might affect multiple loggable objects
  23. *must* be braced by calls to Loggable::block_start() and
  24. Loggable::block_end() in order for Undo to work properly. */
  25. #include "Loggable.H"
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "util/file.h"
  30. // #include "const.h"
  31. #include "util/debug.h"
  32. #include <algorithm>
  33. using std::min;
  34. using std::max;
  35. FILE *Loggable::_fp;
  36. unsigned int Loggable::_log_id = 0;
  37. int Loggable::_level = 0;
  38. off_t Loggable::_undo_offset = 0;
  39. std::map <unsigned int, Loggable::log_pair > Loggable::_loggables;
  40. std::map <std::string, create_func*> Loggable::_class_map;
  41. std::queue <char *> Loggable::_transaction;
  42. progress_func *Loggable::_progress_callback = NULL;
  43. void *Loggable::_progress_callback_arg = NULL;
  44. snapshot_func *Loggable::_snapshot_callback = NULL;
  45. void *Loggable::_snapshot_callback_arg = NULL;
  46. Loggable::~Loggable ( )
  47. {
  48. _loggables[ _id ].loggable = NULL;
  49. }
  50. void
  51. Loggable::block_start ( void )
  52. {
  53. ++Loggable::_level;
  54. }
  55. void
  56. Loggable::block_end ( void )
  57. {
  58. --Loggable::_level;
  59. ASSERT( Loggable::_level >= 0, "Programming error" );
  60. if ( Loggable::_level == 0 )
  61. flush();
  62. }
  63. Loggable *
  64. Loggable::find ( unsigned int id )
  65. {
  66. return _loggables[ id ].loggable;
  67. }
  68. /** Open the journal /filename/ and replay it, bringing the end state back into RAM */
  69. bool
  70. Loggable::open ( const char *filename )
  71. {
  72. FILE *fp;
  73. Loggable::_fp = NULL;
  74. if ( ! ( fp = fopen( filename, "a+" ) ) )
  75. {
  76. WARNING( "Could not open log file for writing!" );
  77. return false;
  78. }
  79. load_unjournaled_state();
  80. if ( newer( "snapshot", filename ) )
  81. {
  82. MESSAGE( "Loading snapshot" );
  83. FILE *fp = fopen( "snapshot", "r" );
  84. replay( fp );
  85. fclose( fp );
  86. }
  87. else
  88. {
  89. MESSAGE( "Replaying journal" );
  90. replay( fp );
  91. }
  92. fseek( fp, 0, SEEK_END );
  93. _undo_offset = ftell( fp );
  94. Loggable::_fp = fp;
  95. return true;
  96. }
  97. bool
  98. Loggable::load_unjournaled_state ( void )
  99. {
  100. FILE *fp;
  101. fp = fopen( "unjournaled", "r" );
  102. if ( ! fp )
  103. {
  104. DWARNING( "Could not open unjournaled state file for reading" );
  105. return false;
  106. }
  107. unsigned int id;
  108. char buf[BUFSIZ];
  109. while ( fscanf( fp, "%X set %[^\n]\n", &id, buf ) == 2 )
  110. _loggables[ id ].unjournaled_state = new Log_Entry( buf );
  111. fclose( fp );
  112. return true;
  113. }
  114. #include <sys/stat.h>
  115. #include <unistd.h>
  116. /** replay journal or snapshot */
  117. bool
  118. Loggable::replay ( const char *file )
  119. {
  120. if ( FILE *fp = fopen( file, "r" ) )
  121. {
  122. bool r = replay( fp );
  123. fclose( fp );
  124. return r;
  125. }
  126. else
  127. return false;
  128. }
  129. /** replay journal or snapshot */
  130. bool
  131. Loggable::replay ( FILE *fp )
  132. {
  133. /* FIXME: bogus */
  134. char buf[BUFSIZ];
  135. struct stat st;
  136. fstat( fileno( fp ), &st );
  137. off_t total = st.st_size;
  138. off_t current = 0;
  139. if ( _progress_callback )
  140. _progress_callback( 0, _progress_callback_arg );
  141. while ( fscanf( fp, "%[^\n]\n", buf ) == 1 )
  142. {
  143. if ( ! ( ! strcmp( buf, "{" ) || ! strcmp( buf, "}" ) ) )
  144. {
  145. if ( *buf == '\t' )
  146. do_this( buf + 1, false );
  147. else
  148. do_this( buf, false );
  149. }
  150. current = ftell( fp );
  151. if ( _progress_callback )
  152. _progress_callback( current * 100 / total, _progress_callback_arg );
  153. }
  154. if ( _progress_callback )
  155. _progress_callback( 0, _progress_callback_arg );
  156. return true;
  157. }
  158. /** close journal and delete all loggable objects, returing the systemt to a blank slate */
  159. bool
  160. Loggable::close ( void )
  161. {
  162. DMESSAGE( "closing journal and destroying all journaled objects" );
  163. if ( _fp )
  164. {
  165. fclose( _fp );
  166. _fp = NULL;
  167. }
  168. if ( ! snapshot( "snapshot" ) )
  169. WARNING( "Failed to create snapshot" );
  170. for ( std::map <unsigned int, Loggable::log_pair >::iterator i = _loggables.begin();
  171. i != _loggables.end(); ++i )
  172. if ( i->second.loggable )
  173. delete i->second.loggable;
  174. save_unjournaled_state();
  175. for ( std::map <unsigned int, Loggable::log_pair >::iterator i = _loggables.begin();
  176. i != _loggables.end(); ++i )
  177. if ( i->second.unjournaled_state )
  178. delete i->second.unjournaled_state;
  179. _loggables.clear();
  180. return true;
  181. }
  182. /** save out unjournaled state for all loggables */
  183. bool
  184. Loggable::save_unjournaled_state ( void )
  185. {
  186. FILE *fp;
  187. fp = fopen( "unjournaled", "w" );
  188. if ( ! fp )
  189. {
  190. DWARNING( "Could not open unjournaled state file for writing!" );
  191. return false;
  192. }
  193. for ( std::map <unsigned int, Loggable::log_pair >::iterator i = _loggables.begin();
  194. i != _loggables.end(); ++i )
  195. {
  196. if ( i->second.unjournaled_state )
  197. {
  198. char *s = i->second.unjournaled_state->print();
  199. fprintf( fp, "0x%X set %s\n", i->first, s );
  200. free( s );
  201. }
  202. }
  203. fclose( fp );
  204. return true;
  205. }
  206. /** must be called after construction in create() methods */
  207. void
  208. Loggable::update_id ( unsigned int id )
  209. {
  210. /* make sure we're the last one */
  211. ASSERT( _id == _log_id, "%u != %u", _id, _log_id );
  212. assert( _loggables[ _id ].loggable == this );
  213. _loggables[ _id ].loggable = NULL;
  214. _log_id = max( _log_id, id );
  215. /* return this id number to the system */
  216. // --_log_id;
  217. _id = id;
  218. if ( _loggables[ _id ].loggable )
  219. FATAL( "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 ].loggable->class_name(), class_name() );
  220. _loggables[ _id ].loggable = this;
  221. }
  222. /** return a pointer to a static copy of /s/ with all special characters escaped */
  223. const char *
  224. Loggable::escape ( const char *s )
  225. {
  226. static char r[512];
  227. size_t i = 0;
  228. for ( ; *s && i < sizeof( r ); ++i, ++s )
  229. {
  230. if ( '\n' == *s )
  231. {
  232. r[ i++ ] = '\\';
  233. r[ i ] = 'n';
  234. }
  235. else if ( '"' == *s )
  236. {
  237. r[ i++ ] = '\\';
  238. r[ i ] = '"';
  239. }
  240. else
  241. r[ i ] = *s;
  242. }
  243. r[ i ] = '\0';
  244. return r;
  245. }
  246. /** 'do' a message like "Audio_Region 0xF1 set :r 123" */
  247. bool
  248. Loggable::do_this ( const char *s, bool reverse )
  249. {
  250. unsigned int id = 0;
  251. char classname[40];
  252. char command[40];
  253. char *arguments = NULL;
  254. int found = sscanf( s, "%s %X %s ", classname, &id, command );
  255. ASSERT( 3 == found, "Invalid journal entry format \"%s\"", s );
  256. const char *create, *destroy;
  257. if ( reverse )
  258. {
  259. // sscanf( s, "%s %*X %s %*[^\n<]<< %a[^\n]", classname, command, &arguments );
  260. sscanf( s, "%s %*X %s%*[^\n<]<< %a[^\n]", classname, command, &arguments );
  261. create = "destroy";
  262. destroy = "create";
  263. DMESSAGE( "undoing \"%s\"", s );
  264. }
  265. else
  266. {
  267. sscanf( s, "%s %*X %s %a[^\n<]", classname, command, &arguments );
  268. create = "create";
  269. destroy = "destroy";
  270. }
  271. if ( ! strcmp( command, destroy ) )
  272. {
  273. Loggable *l = find( id );
  274. /* deleting eg. a track, which contains a list of other
  275. widgets, causes destroy messages to be emitted for all those
  276. widgets, but when replaying the journal the destroy message
  277. causes the children to be deleted also... This is a temporary
  278. hack. Would it be better to queue up objects for deletion
  279. (when?) */
  280. if ( l )
  281. delete l;
  282. }
  283. else if ( ! strcmp( command, "set" ) )
  284. {
  285. // printf( "got set command (%s).\n", arguments );
  286. Loggable *l = find( id );
  287. ASSERT( l, "Unable to find object 0x%X referenced by command \"%s\"", id, s );
  288. Log_Entry e( arguments );
  289. l->log_start();
  290. l->set( e );
  291. l->log_end();
  292. }
  293. else if ( ! strcmp( command, create ) )
  294. {
  295. Log_Entry e( arguments );
  296. ASSERT( _class_map[ std::string( classname ) ], "Journal contains an object of class \"%s\", but I don't know how to create such objects.", classname );
  297. {
  298. /* create */
  299. Loggable *l = _class_map[ std::string( classname ) ]( e, id );
  300. l->log_create();
  301. /* we're now creating a loggable. Apply any unjournaled
  302. * state it may have had in the past under this log ID */
  303. Log_Entry *e = _loggables[ id ].unjournaled_state;
  304. if ( e )
  305. l->set( *e );
  306. }
  307. }
  308. if ( arguments )
  309. free( arguments );
  310. return true;
  311. }
  312. /** Reverse the last journal transaction */
  313. void
  314. Loggable::undo ( void )
  315. {
  316. const int bufsiz = 1024;
  317. char buf[bufsiz];
  318. block_start();
  319. long here = ftell( _fp );
  320. fseek( _fp, _undo_offset, SEEK_SET );
  321. backwards_fgets( buf, bufsiz, _fp );
  322. if ( ! strcmp( buf, "}\n" ) )
  323. {
  324. DMESSAGE( "undoing block" );
  325. for ( ;; )
  326. {
  327. backwards_fgets( buf, bufsiz, _fp );
  328. char *s = buf;
  329. if ( *s != '\t' )
  330. break;
  331. else
  332. ++s;
  333. do_this( s, true );
  334. }
  335. }
  336. else
  337. do_this( buf, true );
  338. off_t uo = ftell( _fp );
  339. ASSERT( _undo_offset <= here, "WTF?" );
  340. block_end();
  341. _undo_offset = uo;
  342. }
  343. /** write a snapshot of the current state of all loggable objects to
  344. * file handle /fp/ */
  345. bool
  346. Loggable::snapshot ( FILE *fp )
  347. {
  348. FILE *ofp = _fp;
  349. if ( ! Loggable::_snapshot_callback )
  350. {
  351. DWARNING( "No snapshot callback defined" );
  352. return false;
  353. }
  354. if ( ! ( _fp = fp ) )
  355. {
  356. _fp = ofp;
  357. return false;
  358. }
  359. block_start();
  360. Loggable::_snapshot_callback( _snapshot_callback_arg );
  361. block_end();
  362. _fp = ofp;
  363. return true;
  364. }
  365. /** write a snapshot of the current state of all loggable objects to
  366. * file /name/ */
  367. bool
  368. Loggable::snapshot ( const char *name )
  369. {
  370. FILE *fp;
  371. if ( ! ( fp = fopen( name, "w" ) ))
  372. return false;
  373. bool r = snapshot( fp );
  374. fclose( fp );
  375. return r;
  376. }
  377. /** Replace the journal with a snapshot of the current state */
  378. void
  379. Loggable::compact ( void )
  380. {
  381. fseek( _fp, 0, SEEK_SET );
  382. ftruncate( fileno( _fp ), 0 );
  383. if ( ! snapshot( _fp ) )
  384. FATAL( "Could not write snapshot!" );
  385. fseek( _fp, 0, SEEK_END );
  386. }
  387. #include <stdarg.h>
  388. /** Writes (part of) a line to the journal. Each separate line will be
  389. * stored separately in _transaction until transaction is closed.
  390. */
  391. void
  392. Loggable::log ( const char *fmt, ... )
  393. {
  394. static char * buf = NULL;
  395. static size_t i = 0;
  396. static size_t buf_size = 0;
  397. if ( ! _fp )
  398. return;
  399. if ( NULL == buf )
  400. {
  401. buf_size = 1024;
  402. buf = (char*)malloc( buf_size );
  403. }
  404. va_list args;
  405. if ( fmt )
  406. {
  407. va_start( args, fmt );
  408. for ( ;; )
  409. {
  410. size_t l = vsnprintf( buf + i, buf_size - i, fmt, args );
  411. if ( l >= buf_size - i )
  412. {
  413. buf = (char*)realloc( buf, buf_size += (l + 1) + buf_size );
  414. }
  415. else
  416. {
  417. i += l;
  418. break;
  419. }
  420. }
  421. va_end( args );
  422. }
  423. if ( '\n' == buf[i-1] )
  424. {
  425. _transaction.push( strdup( buf ) );
  426. i = 0;
  427. }
  428. }
  429. /** End the current transaction and commit it to the journal */
  430. void
  431. Loggable::flush ( void )
  432. {
  433. if ( ! _fp )
  434. {
  435. // printf( "error: no log file open!\n" );
  436. while ( ! _transaction.empty() )
  437. {
  438. free( _transaction.front() );
  439. _transaction.pop();
  440. }
  441. return;
  442. }
  443. int n = _transaction.size();
  444. if ( n > 1 )
  445. fprintf( _fp, "{\n" );
  446. while ( ! _transaction.empty() )
  447. {
  448. char *s = _transaction.front();
  449. _transaction.pop();
  450. if ( n > 1 )
  451. fprintf( _fp, "\t" );
  452. fprintf( _fp, "%s", s );
  453. free( s );
  454. }
  455. if ( n > 1 )
  456. fprintf( _fp, "}\n" );
  457. if ( n )
  458. /* something done, reset undo index */
  459. _undo_offset = ftell( _fp );
  460. fflush( _fp );
  461. }
  462. /** Print bidirectional journal entry */
  463. void
  464. Loggable::log_print( const Log_Entry *o, const Log_Entry *n ) const
  465. {
  466. if ( ! _fp )
  467. return;
  468. if ( n )
  469. for ( int i = 0; i < n->size(); ++i )
  470. {
  471. const char *s, *v;
  472. n->get( i, &s, &v );
  473. log( "%s %s%s", s, v, n->size() == i + 1 ? "" : " " );
  474. }
  475. if ( o && o->size() )
  476. {
  477. if ( n ) log( " << " );
  478. for ( int i = 0; i < o->size(); ++i )
  479. {
  480. const char *s, *v;
  481. o->get( i, &s, &v );
  482. log( "%s %s%s", s, v, o->size() == i + 1 ? "" : " " );
  483. }
  484. }
  485. log( "\n" );
  486. }
  487. /** Remember current object state for later comparison. *Must* be
  488. * called before any user action that might change one of the object's
  489. * journaled properties. */
  490. void
  491. Loggable::log_start ( void )
  492. {
  493. if ( ! _old_state )
  494. {
  495. _old_state = new Log_Entry;
  496. get( *_old_state );
  497. }
  498. ++_nest;
  499. }
  500. /** Log any change to the object's state since log_start(). */
  501. void
  502. Loggable::log_end ( void )
  503. {
  504. ASSERT( _old_state, "Programming error: log_end() called before log_start()" );
  505. if ( --_nest > 0 )
  506. return;
  507. Log_Entry *new_state;
  508. new_state = new Log_Entry;
  509. get( *new_state );
  510. if ( Log_Entry::diff( _old_state, new_state ) )
  511. {
  512. log( "%s 0x%X set ", class_name(), _id );
  513. log_print( _old_state, new_state );
  514. }
  515. delete new_state;
  516. delete _old_state;
  517. _old_state = NULL;
  518. if ( Loggable::_level == 0 )
  519. Loggable::flush();
  520. }
  521. /** Log object creation. *Must* be called at the end of all public
  522. * constructors for leaf classes */
  523. void
  524. Loggable::log_create ( void ) const
  525. {
  526. if ( ! _fp )
  527. /* replaying, don't bother */
  528. return;
  529. log( "%s 0x%X create ", class_name(), _id );
  530. Log_Entry e;
  531. get( e );
  532. if ( e.size() )
  533. log_print( NULL, &e );
  534. else
  535. log( "\n" );
  536. if ( Loggable::_level == 0 )
  537. Loggable::flush();
  538. }
  539. /** record this loggable's unjournaled state in memory */
  540. void
  541. Loggable::record_unjournaled ( void ) const
  542. {
  543. Log_Entry *e = new Log_Entry();
  544. get_unjournaled( *e );
  545. Log_Entry *le = _loggables[ _id ].unjournaled_state;
  546. if ( le )
  547. delete le;
  548. if ( e->size() )
  549. _loggables[ _id ].unjournaled_state = e;
  550. else
  551. {
  552. /* don't waste space on loggables with no unjournaled properties */
  553. _loggables[ _id ].unjournaled_state = NULL;
  554. delete e;
  555. }
  556. }
  557. /** Log object destruction. *Must* be called at the beginning of the
  558. * destructors of leaf classes */
  559. void
  560. Loggable::log_destroy ( void ) const
  561. {
  562. /* the unjournaled state may have changed: make a note of it. */
  563. record_unjournaled();
  564. if ( ! _fp )
  565. /* tearing down... don't bother */
  566. return;
  567. log( "%s 0x%X destroy << ", class_name(), _id );
  568. Log_Entry e;
  569. get( e );
  570. log_print( NULL, &e );
  571. if ( Loggable::_level == 0 )
  572. Loggable::flush();
  573. }