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.

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