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.

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