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.

747 lines
15KB

  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. #define _LOGGABLE_C
  19. #include "Loggable.H"
  20. #undef _LOGABLE_C
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25. FILE *Loggable::_fp;
  26. int Loggable::_log_id = 0;
  27. int Loggable::_level = 0;
  28. int Loggable::_undo_index = 1;
  29. size_t Loggable::_loggables_size = 0;
  30. Loggable ** Loggable::_loggables;
  31. map <string, create_func*> Loggable::_class_map;
  32. queue <char *> Loggable::_transaction;
  33. bool
  34. Loggable::open ( const char *filename )
  35. {
  36. FILE *fp;
  37. if ( ! ( fp = fopen( filename, "a+" ) ) )
  38. {
  39. WARNING( "Could not open log file for writing!" );
  40. return false;
  41. }
  42. /* replay log */
  43. {
  44. char buf[BUFSIZ];
  45. while ( fscanf( fp, "%[^\n]\n", buf ) == 1 )
  46. {
  47. if ( ! ( ! strcmp( buf, "{" ) || ! strcmp( buf, "}" ) ) )
  48. {
  49. if ( *buf == '\t' )
  50. do_this( buf + 1, false );
  51. else
  52. do_this( buf, false );
  53. }
  54. }
  55. }
  56. Loggable::_fp = fp;
  57. return true;
  58. }
  59. /** close journal and delete all loggable objects */
  60. bool
  61. Loggable::close ( void )
  62. {
  63. DMESSAGE( "closing journal and destroying all journaled objects" );
  64. if ( ! _fp )
  65. return true;
  66. fclose( _fp );
  67. _fp = NULL;
  68. for ( int i = 0; i < _log_id - 1; ++i )
  69. {
  70. Loggable ** l = &_loggables[ i ];
  71. if ( *l )
  72. {
  73. delete *l;
  74. *l = NULL;
  75. }
  76. }
  77. _log_id = 0;
  78. return true;
  79. }
  80. /** must be called after construction in create() methods */
  81. void
  82. Loggable::update_id ( int id )
  83. {
  84. /* make sure we're the last one */
  85. assert( _id == _log_id );
  86. assert( _loggables[ _id - 1 ] == this );
  87. _loggables[ _id - 1 ] = NULL;
  88. _log_id = max( _log_id, id );
  89. /* return this id number to the system */
  90. // --_log_id;
  91. _id = id;
  92. /* make sure it'll fit */
  93. ensure_size( _id );
  94. 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() );
  95. _loggables[ _id - 1 ] = this;
  96. }
  97. /** return a pointer to a static copy of /s/ with all special characters escaped */
  98. const char *
  99. Loggable::escape ( const char *s )
  100. {
  101. static char r[512];
  102. size_t i = 0;
  103. for ( ; *s && i < sizeof( r ); ++i, ++s )
  104. {
  105. if ( '\n' == *s )
  106. {
  107. r[ i++ ] = '\\';
  108. r[ i ] = 'n';
  109. }
  110. else if ( '"' == *s )
  111. {
  112. r[ i++ ] = '\\';
  113. r[ i ] = '"';
  114. }
  115. else
  116. r[ i ] = *s;
  117. }
  118. r[ i ] = '\0';
  119. return r;
  120. }
  121. /** remove escapes from string /s/ in-place */
  122. static void
  123. unescape ( char *s )
  124. {
  125. char *r = s;
  126. for ( ; *s; s++, r++ )
  127. {
  128. if ( '\\' == *s )
  129. {
  130. switch ( *(++s) )
  131. {
  132. case 'n':
  133. *r = '\n';
  134. break;
  135. case '"':
  136. *r = '"';
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. else
  143. *r = *s;
  144. }
  145. *r = '\0';
  146. }
  147. /** sigh. parse a string of ":name value :name value" pairs into an
  148. * array of strings, one per pair */
  149. // FIXME: doesn't handle the case of :name ":foo bar", nested quotes
  150. // or other things it should.
  151. static
  152. char **
  153. parse_alist( const char *s )
  154. {
  155. // FIXME: bogus over allocation...
  156. int tl = strlen( s );
  157. char **r = (char**)malloc( sizeof( char* ) * tl );
  158. // const char *e = s + tl;
  159. const char *c = NULL;
  160. int i = 0;
  161. for ( ; ; s++ )
  162. {
  163. /* if ( *s == '\n' ) */
  164. /* break; */
  165. // if ( *s == ':' || s == e )
  166. if ( *s == ':' || *s == '\0' )
  167. {
  168. if ( c )
  169. {
  170. int l = s - c;
  171. char *pair = (char*)malloc( l + 1 );
  172. /* remove trailing space */
  173. if ( c[ l - 1 ] == ' ' )
  174. --l;
  175. strncpy( pair, c, l );
  176. pair[ l ] = '\0';
  177. r[ i++ ] = pair;
  178. /* split */
  179. strtok( pair, " " );
  180. /* remove quotes */
  181. char *v = pair + strlen( pair ) + 1;
  182. unescape( v );
  183. if ( *v == '"' )
  184. {
  185. // v++;
  186. if ( v[ strlen( v ) - 1 ] != '"' )
  187. WARNING( "invalid quoting in log entry!" );
  188. else
  189. {
  190. v[ strlen( v ) - 1 ] = '\0';
  191. memmove( v, v + 1, strlen( v ) + 1 );
  192. }
  193. }
  194. }
  195. c = s;
  196. if ( *s == '\0' )
  197. break;
  198. }
  199. }
  200. r[ i ] = NULL;
  201. return r;
  202. }
  203. static
  204. void free_sa ( char **sa )
  205. {
  206. char **a = sa;
  207. for ( ; *a; a++ )
  208. free( *a );
  209. free( sa );
  210. }
  211. /** 'do' a message like "Audio_Region 0xF1 set :r 123" */
  212. bool
  213. Loggable::do_this ( const char *s, bool reverse )
  214. {
  215. int id;
  216. if ( ! ( sscanf( s, "%*s %X ", &id ) > 0 ) )
  217. return false;
  218. Loggable *l = find( id );
  219. // assert( l );
  220. char classname[40];
  221. char command[40];
  222. char *arguments = NULL;
  223. const char *create, *destroy;
  224. if ( reverse )
  225. {
  226. // sscanf( s, "%s %*X %s %*[^\n<]<< %a[^\n]", classname, command, &arguments );
  227. sscanf( s, "%s %*X %s%*[^\n<]<< %a[^\n]", classname, command, &arguments );
  228. create = "destroy";
  229. destroy = "create";
  230. }
  231. else
  232. {
  233. sscanf( s, "%s %*X %s %a[^\n<]", classname, command, &arguments );
  234. create = "create";
  235. destroy = "destroy";
  236. }
  237. if ( ! strcmp( command, destroy ) )
  238. {
  239. /* deleting eg. a track, which contains a list of other
  240. widgets, causes destroy messages to be emitted for all those
  241. widgets, but when replaying the journal the destroy message
  242. causes the children to be deleted also... This is a temporary
  243. hack. Would it be better to queue up objects for deletion
  244. (when?) */
  245. if ( l )
  246. delete l;
  247. }
  248. else if ( ! strcmp( command, "set" ) )
  249. {
  250. // printf( "got set command (%s).\n", arguments );
  251. char **sa = parse_alist( arguments );
  252. Log_Entry e( sa );
  253. l->log_start();
  254. l->set( e );
  255. l->log_end();
  256. }
  257. else if ( ! strcmp( command, create ) )
  258. {
  259. char **sa = NULL;
  260. if ( arguments )
  261. sa = parse_alist( arguments );
  262. Log_Entry e( sa );
  263. ASSERT( _class_map[ string( classname ) ], "Journal contains an object of class \"%s\", but I don't know how to create such objects.", classname );
  264. {
  265. /* create */
  266. Loggable *l = _class_map[ string( classname ) ]( e );
  267. l->update_id( id );
  268. l->log_create();
  269. }
  270. }
  271. if ( arguments )
  272. free( arguments );
  273. return true;
  274. }
  275. void
  276. Loggable::undo ( void )
  277. {
  278. char *buf = new char[ BUFSIZ ];
  279. // fflush( _fp );
  280. /* FIXME: handle more than the first block!!! */
  281. fseek( _fp, 0, SEEK_END );
  282. size_t len = ftell( _fp );
  283. fseek( _fp, 0 - (BUFSIZ > len ? len : BUFSIZ), SEEK_END );
  284. len = fread( buf, 1, BUFSIZ, _fp );
  285. char *s = buf + len - 1;
  286. int i = 1;
  287. /* move back _undo_index items from the end */
  288. for ( int j = _undo_index; j-- ; )
  289. for ( --s; *s && s >= buf; --s, ++i )
  290. {
  291. if ( *s == '\n' )
  292. {
  293. if ( *(s + 1) == '\t' )
  294. continue;
  295. if ( *(s + 1) == '}' )
  296. {
  297. *(s + 1) = NULL;
  298. continue;
  299. }
  300. break;
  301. }
  302. }
  303. s++;
  304. buf[ len ] = NULL;
  305. if ( ! strlen( s ) )
  306. {
  307. WARNING( "corrupt undo file or no undo entries." );
  308. return;
  309. }
  310. char *b = s;
  311. s += strlen( s ) - 1;
  312. if ( strtok( b, "\n" ) == NULL )
  313. FATAL( "empty undo transaction!\n" );
  314. int n = 1;
  315. while ( strtok( NULL, "\n" ) )
  316. ++n;
  317. int ui = _undo_index;
  318. block_start();
  319. if ( strcmp( b, "{" ) )
  320. {
  321. /* It's a single undo, get rid of trailing messages in this block */
  322. n = 1;
  323. s = b + 2;
  324. s += strlen( s ) - 1;
  325. }
  326. while ( n-- )
  327. {
  328. while ( s >= b && *(--s) );
  329. s++;
  330. if ( ! strcmp( s, "{" ) )
  331. break;
  332. if ( *s == '\t' )
  333. s++;
  334. DMESSAGE( "undoing \"%s\"", s );
  335. do_this( s, true );
  336. s -= 2;
  337. }
  338. block_end();
  339. _undo_index = ui + 2;
  340. delete[] buf;
  341. }
  342. /** write a snapshot of the state of all loggable objects, sufficient
  343. * for later reconstruction, to /fp/ */
  344. bool
  345. Loggable::snapshot( FILE *fp )
  346. {
  347. FILE *ofp = _fp;
  348. if ( ! ( _fp = fp ) )
  349. {
  350. _fp = ofp;
  351. return false;
  352. }
  353. /* first, make all ids consecutive */
  354. int id = 0;
  355. for ( int i = 0; i < _log_id; ++i )
  356. if ( _loggables[ i ] )
  357. {
  358. ++id;
  359. if ( _loggables[ id - 1 ] )
  360. continue;
  361. _loggables[ id - 1 ] = _loggables[ i ];
  362. _loggables[ i ] = NULL;
  363. _loggables[ id - 1 ]->_id = id;
  364. }
  365. _log_id = id;
  366. block_start();
  367. for ( int i = 0; i < _log_id; ++i )
  368. {
  369. const Loggable * l = _loggables[ i ];
  370. if ( l && _class_map[ string( l->class_name() ) ] )
  371. l->log_create();
  372. }
  373. block_end();
  374. _fp = ofp;
  375. return true;
  376. }
  377. void
  378. Loggable::compact ( void )
  379. {
  380. fseek( _fp, 0, SEEK_SET );
  381. ftruncate( fileno( _fp ), 0 );
  382. snapshot( _fp );
  383. _undo_index = 1;
  384. }
  385. void
  386. Loggable::log ( const char *fmt, ... )
  387. {
  388. /* FIXME: bogus limit */
  389. static char buf[1024];
  390. static int i = 0;
  391. va_list args;
  392. if ( fmt )
  393. {
  394. va_start( args, fmt );
  395. i += vsprintf( buf + i, fmt, args );
  396. va_end( args );
  397. }
  398. if ( rindex( buf, '\n' ) )
  399. {
  400. _transaction.push( strdup( buf ) );
  401. i = 0;
  402. }
  403. }
  404. void
  405. Loggable::flush ( void )
  406. {
  407. if ( ! _fp )
  408. {
  409. // printf( "error: no log file open!\n" );
  410. while ( ! _transaction.empty() )
  411. {
  412. free( _transaction.front() );
  413. _transaction.pop();
  414. }
  415. return;
  416. }
  417. int n = _transaction.size();
  418. if ( n )
  419. /* something done, reset undo index */
  420. _undo_index = 1;
  421. if ( n > 1 )
  422. fprintf( _fp, "{\n" );
  423. while ( ! _transaction.empty() )
  424. {
  425. char *s = _transaction.front();
  426. _transaction.pop();
  427. if ( n > 1 )
  428. fprintf( _fp, "\t" );
  429. fprintf( _fp, "%s", s );
  430. free( s );
  431. }
  432. if ( n > 1 )
  433. fprintf( _fp, "}\n" );
  434. fflush( _fp );
  435. }
  436. void
  437. Loggable::log_print( char **o, char **n ) const
  438. {
  439. if ( n )
  440. for ( ; *n; n++ )
  441. log( "%s %s%s", *n, *n + strlen( *n ) + 1, *(n + 1) ? " " : "" );
  442. if ( o && *o )
  443. {
  444. if ( n ) log( " << " );
  445. for ( ; *o; o++ )
  446. log( "%s %s%s", *o, *o + strlen( *o ) + 1, *(o + 1) ? " " : "" );
  447. }
  448. log( "\n" );
  449. }
  450. /** compare elements of dumps s1 and s2, removing those elements
  451. of dst which are not changed from src */
  452. static
  453. bool
  454. log_diff ( char **sa1, char **sa2 )
  455. {
  456. if ( ! sa1 )
  457. return true;
  458. int w = 0;
  459. for ( int i = 0; sa1[ i ]; ++i )
  460. {
  461. const char *v1 = sa1[ i ] + strlen( sa1[ i ] ) + 1;
  462. const char *v2 = sa2[ i ] + strlen( sa2[ i ] ) + 1;
  463. if ( ! strcmp( sa1[ i ], sa2[ i ] ) && ! strcmp( v1, v2 ) )
  464. {
  465. free( sa2[ i ] );
  466. free( sa1[ i ] );
  467. }
  468. else
  469. {
  470. sa2[ w ] = sa2[ i ];
  471. sa1[ w ] = sa1[ i ];
  472. w++;
  473. }
  474. }
  475. sa1[ w ] = NULL;
  476. sa2[ w ] = NULL;
  477. return w == 0 ? false : true;
  478. }
  479. void
  480. Loggable::log_start ( void )
  481. {
  482. if ( ! _old_state )
  483. {
  484. Log_Entry e;
  485. get( e );
  486. _old_state = e.sa();
  487. }
  488. ++_nest;
  489. }
  490. void
  491. Loggable::log_end ( void )
  492. {
  493. if ( --_nest > 0 )
  494. return;
  495. char **_new_state;
  496. {
  497. Log_Entry e;
  498. get( e );
  499. _new_state = e.sa();
  500. }
  501. if ( log_diff( _old_state, _new_state ) )
  502. {
  503. // indent();
  504. log( "%s 0x%X set ", class_name(), _id );
  505. log_print( _old_state, _new_state );
  506. }
  507. if ( _new_state )
  508. free_sa( _new_state );
  509. if ( _old_state )
  510. free_sa( _old_state );
  511. _old_state = NULL;
  512. if ( Loggable::_level == 0 )
  513. Loggable::flush();
  514. }
  515. void
  516. Loggable::log_create ( void ) const
  517. {
  518. // indent();
  519. /* if ( Loggable::_level ) */
  520. /* { */
  521. /* /\* defer until the block ends--this is really only required for capturing... *\/ */
  522. /* } */
  523. log( "%s 0x%X create ", class_name(), _id );
  524. char **sa;
  525. Log_Entry e;
  526. get( e );
  527. sa = e.sa();
  528. if ( sa )
  529. {
  530. log_print( NULL, sa );
  531. free_sa( sa );
  532. }
  533. else
  534. log( "\n" );
  535. if ( Loggable::_level == 0 )
  536. Loggable::flush();
  537. }
  538. void
  539. Loggable::log_destroy ( void ) const
  540. {
  541. if ( ! _fp )
  542. /* tearing down... don't bother */
  543. return;
  544. log( "%s 0x%X destroy << ", class_name(), _id );
  545. char **sa;
  546. Log_Entry e;
  547. get( e );
  548. sa = e.sa();
  549. // log_print( sa, NULL );
  550. log_print( NULL, sa );
  551. free_sa( sa );
  552. if ( Loggable::_level == 0 )
  553. Loggable::flush();
  554. }