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.

639 lines
12KB

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