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.

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