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.

903 lines
19KB

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