diff --git a/src/engine/TransLog.c b/src/engine/TransLog.c index 8f3f428eb9..411cb2316b 100644 --- a/src/engine/TransLog.c +++ b/src/engine/TransLog.c @@ -83,6 +83,7 @@ static int gen_logs = 1; static FILE * trans_log = 0x0; +static char * log_base_name = 0x0; /********************************************************************\ \********************************************************************/ @@ -93,23 +94,54 @@ void xaccLogEnable (void) { gen_logs = 1; } /********************************************************************\ \********************************************************************/ +void +xaccLogSetBaseName (const char *basepath) +{ + if (!basepath) return; + + if (log_base_name) free (log_base_name); + log_base_name = strdup (basepath); + + if (trans_log) { + xaccCloseLog(); + xaccOpenLog(); + } +} + +/********************************************************************\ +\********************************************************************/ + void xaccOpenLog (void) { - char filename[1000]; + char * filename; char * timestamp; if (!gen_logs) return; if (trans_log) return; + if (!log_base_name) log_base_name = strdup ("translog"); + /* tag each filename with a timestamp */ timestamp = xaccDateUtilGetStampNow (); - strcpy (filename, "translog."); + filename = (char *) malloc (strlen (log_base_name) + 50); + strcpy (filename, log_base_name); + strcat (filename, "."); strcat (filename, timestamp); strcat (filename, ".log"); trans_log = fopen (filename, "a"); + if (!trans_log) { + int norr = errno; + printf ("Error: xaccOpenLog(): cannot open journal \n" + "\t %d %s\n", norr, strerror (norr)); + free (filename); + free (timestamp); + return; + } + free (filename); + free (timestamp); /* use tab-separated fields */ fprintf (trans_log, "mod id time_now " \ @@ -119,7 +151,18 @@ xaccOpenLog (void) "amount price date_reconciled\n"); fprintf (trans_log, "-----------------\n"); - free (timestamp); +} + +/********************************************************************\ +\********************************************************************/ + +void +xaccCloseLog (void) +{ + if (!trans_log) return; + fflush (trans_log); + fclose (trans_log); + trans_log = 0x0; } /********************************************************************\ diff --git a/src/engine/TransLog.h b/src/engine/TransLog.h index b1b658483d..9f4852d618 100644 --- a/src/engine/TransLog.h +++ b/src/engine/TransLog.h @@ -27,10 +27,18 @@ #include "Transaction.h" void xaccOpenLog (void); +void xaccCloseLog (void); void xaccTransWriteLog (Transaction *, char); void xaccLogEnable (void); void xaccLogDisable (void); +/* The xaccLogSetBaseName() method sets the base filepath and the + * root part of the journal file name. If the journal file is + * already open, it will close it and reopen it with the new + * base name. + */ +void xaccLogSetBaseName (const char *); + /* returned strings will have been allocated with malloc, free with free() */ char *xaccSplitAsString(Split *s, const char prefix[]); char *xaccTransAsString(Transaction *t, const char prefix[]);