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.

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