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.

907 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 %m[^\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, "%m[^\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<]<< %m[^\n]", classname, command, &arguments );
  308. sscanf( s, "%s %*X %s%*[^\n<]<< %m[^\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 %m[^\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. if ( ! _fp || /* journal not open */
  367. 1 == _undo_offset ) /* nothing left to undo */
  368. return;
  369. char *buf;
  370. block_start();
  371. long here = ftell( _fp );
  372. fseek( _fp, _undo_offset, SEEK_SET );
  373. if ( ( buf = backwards_afgets( _fp ) ) )
  374. {
  375. if ( ! strcmp( buf, "}\n" ) )
  376. {
  377. free( buf );
  378. DMESSAGE( "undoing block" );
  379. for ( ;; )
  380. {
  381. if ( ( buf = backwards_afgets( _fp ) ) )
  382. {
  383. char *s = buf;
  384. if ( *s != '\t' )
  385. {
  386. DMESSAGE( "done with block", s );
  387. break;
  388. }
  389. else
  390. ++s;
  391. do_this( s, true );
  392. free( buf );
  393. }
  394. }
  395. }
  396. else
  397. {
  398. do_this( buf, true );
  399. free( buf );
  400. }
  401. }
  402. off_t uo = ftell( _fp );
  403. ASSERT( _undo_offset <= here, "WTF?" );
  404. block_end();
  405. _undo_offset = uo;
  406. }
  407. /** write a snapshot of the current state of all loggable objects to
  408. * file handle /fp/ */
  409. bool
  410. Loggable::snapshot ( FILE *fp )
  411. {
  412. FILE *ofp = _fp;
  413. if ( ! Loggable::_snapshot_callback )
  414. {
  415. DWARNING( "No snapshot callback defined" );
  416. return false;
  417. }
  418. if ( ! ( _fp = fp ) )
  419. {
  420. _fp = ofp;
  421. return false;
  422. }
  423. #ifndef NDEBUG
  424. _snapshotting = true;
  425. _snapshot_count++;
  426. #endif
  427. block_start();
  428. Loggable::_snapshot_callback( _snapshot_callback_arg );
  429. block_end();
  430. #ifndef NDEBUG
  431. _snapshotting = false;
  432. #endif
  433. _fp = ofp;
  434. clear_dirty();
  435. return true;
  436. }
  437. /** write a snapshot of the current state of all loggable objects to
  438. * file /name/ */
  439. bool
  440. Loggable::snapshot ( const char *name )
  441. {
  442. FILE *fp;
  443. char *tmpname;
  444. asprintf( &tmpname, ".#%s", name );
  445. if ( ! ( fp = fopen( tmpname, "w" ) ))
  446. return false;
  447. bool r = snapshot( fp );
  448. fclose( fp );
  449. rename( tmpname, name );
  450. free(tmpname);
  451. return r;
  452. }
  453. /** Replace the journal with a snapshot of the current state */
  454. void
  455. Loggable::compact ( void )
  456. {
  457. fseek( _fp, 0, SEEK_SET );
  458. ftruncate( fileno( _fp ), 0 );
  459. if ( ! snapshot( _fp ) )
  460. FATAL( "Could not write snapshot!" );
  461. fseek( _fp, 0, SEEK_END );
  462. }
  463. #include <stdarg.h>
  464. /** Writes (part of) a line to the journal. Each separate line will be
  465. * stored separately in _transaction until transaction is closed.
  466. */
  467. void
  468. Loggable::log ( const char *fmt, ... )
  469. {
  470. Locker lock( _lock );
  471. static char * buf = NULL;
  472. static size_t i = 0;
  473. static size_t buf_size = 0;
  474. if ( ! _fp )
  475. return;
  476. if ( NULL == buf )
  477. {
  478. buf_size = 1024;
  479. buf = (char*)malloc( buf_size );
  480. }
  481. va_list args;
  482. if ( fmt )
  483. {
  484. va_start( args, fmt );
  485. for ( ;; )
  486. {
  487. size_t l = vsnprintf( buf + i, buf_size - i, fmt, args );
  488. if ( l >= buf_size - i )
  489. {
  490. buf = (char*)realloc( buf, buf_size += (l + 1) + buf_size );
  491. }
  492. else
  493. {
  494. i += l;
  495. break;
  496. }
  497. }
  498. va_end( args );
  499. }
  500. if ( '\n' == buf[i-1] )
  501. {
  502. _transaction.push( strdup( buf ) );
  503. i = 0;
  504. }
  505. }
  506. /** End the current transaction and commit it to the journal */
  507. void
  508. Loggable::flush ( void )
  509. {
  510. if ( ! _fp )
  511. {
  512. // printf( "error: no log file open!\n" );
  513. while ( ! _transaction.empty() )
  514. {
  515. free( _transaction.front() );
  516. _transaction.pop();
  517. }
  518. return;
  519. }
  520. int n = _transaction.size();
  521. if ( n > 1 )
  522. fprintf( _fp, "{\n" );
  523. while ( ! _transaction.empty() )
  524. {
  525. char *s = _transaction.front();
  526. _transaction.pop();
  527. if ( n > 1 )
  528. fprintf( _fp, "\t" );
  529. fprintf( _fp, "%s", s );
  530. free( s );
  531. }
  532. if ( n > 1 )
  533. fprintf( _fp, "}\n" );
  534. if ( n )
  535. /* something done, reset undo index */
  536. _undo_offset = ftell( _fp );
  537. fflush( _fp );
  538. }
  539. /** Print bidirectional journal entry */
  540. void
  541. Loggable::log_print( const Log_Entry *o, const Log_Entry *n ) const
  542. {
  543. if ( ! _fp )
  544. return;
  545. if ( n )
  546. for ( int i = 0; i < n->size(); ++i )
  547. {
  548. const char *s, *v;
  549. n->get( i, &s, &v );
  550. log( "%s %s%s", s, v, n->size() == i + 1 ? "" : " " );
  551. }
  552. if ( o && o->size() )
  553. {
  554. if ( n ) log( " << " );
  555. for ( int i = 0; i < o->size(); ++i )
  556. {
  557. const char *s, *v;
  558. o->get( i, &s, &v );
  559. log( "%s %s%s", s, v, o->size() == i + 1 ? "" : " " );
  560. }
  561. }
  562. log( "\n" );
  563. }
  564. /** Remember current object state for later comparison. *Must* be
  565. * called before any user action that might change one of the object's
  566. * journaled properties. */
  567. void
  568. Loggable::log_start ( void )
  569. {
  570. Locker lock( _lock );;
  571. if ( ! _old_state )
  572. {
  573. _old_state = new Log_Entry;
  574. get( *_old_state );
  575. }
  576. ++_nest;
  577. }
  578. /** Log any change to the object's state since log_start(). */
  579. void
  580. Loggable::log_end ( void )
  581. {
  582. Locker lock( _lock );;
  583. ASSERT( _old_state, "Programming error: log_end() called before log_start()" );
  584. if ( --_nest > 0 )
  585. return;
  586. Log_Entry *new_state;
  587. new_state = new Log_Entry;
  588. get( *new_state );
  589. if ( Log_Entry::diff( _old_state, new_state ) )
  590. {
  591. log( "%s 0x%X set ", class_name(), _id );
  592. log_print( _old_state, new_state );
  593. set_dirty();
  594. }
  595. delete new_state;
  596. delete _old_state;
  597. _old_state = NULL;
  598. if ( Loggable::_level == 0 )
  599. Loggable::flush();
  600. }
  601. /** Log object creation. *Must* be called at the end of all public
  602. * constructors for leaf classes */
  603. void
  604. Loggable::log_create ( void ) const
  605. {
  606. Locker lock( _lock );;
  607. set_dirty();
  608. if ( ! _fp )
  609. /* replaying, don't bother */
  610. return;
  611. #ifndef NDEBUG
  612. if ( _snapshotting && _snapshot_count != _num_snapshot )
  613. {
  614. _num_snapshot_creates = 1;
  615. _num_snapshot = _snapshot_count;
  616. }
  617. else if ( _snapshotting && _snapshot_count == _num_snapshot )
  618. {
  619. _num_snapshot_creates++;
  620. ASSERT( _num_snapshot_creates < 2, "Attempt to log creation of same object twice in one snapshot! %s", class_name() );
  621. }
  622. else
  623. {
  624. _num_log_creates++;
  625. ASSERT( _num_log_creates < 2, "Attempt to log creation of same object twice in the journal! %s", class_name() );
  626. }
  627. #endif
  628. log( "%s 0x%X create ", class_name(), _id );
  629. Log_Entry e;
  630. get( e );
  631. if ( e.size() )
  632. log_print( NULL, &e );
  633. else
  634. log( "\n" );
  635. if ( Loggable::_level == 0 )
  636. Loggable::flush();
  637. }
  638. /** record this loggable's unjournaled state in memory */
  639. void
  640. Loggable::record_unjournaled ( void ) const
  641. {
  642. Log_Entry *e = new Log_Entry();
  643. get_unjournaled( *e );
  644. Log_Entry **le = &_loggables[ _id ].unjournaled_state;
  645. if ( *le )
  646. {
  647. delete *le;
  648. *le = NULL;
  649. }
  650. if ( e->size() )
  651. *le = e;
  652. else
  653. delete e;
  654. }
  655. /** Log object destruction. *Must* be called at the beginning of the
  656. * destructors of leaf classes */
  657. void
  658. Loggable::log_destroy ( void ) const
  659. {
  660. Locker lock( _lock );;
  661. set_dirty();
  662. if ( ! _fp )
  663. /* tearing down... don't bother */
  664. return;
  665. /* the unjournaled state may have changed: make a note of it. */
  666. record_unjournaled();
  667. log( "%s 0x%X destroy << ", class_name(), _id );
  668. Log_Entry e;
  669. get( e );
  670. log_print( NULL, &e );
  671. if ( Loggable::_level == 0 )
  672. Loggable::flush();
  673. }