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.

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