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.

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