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.

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