[translog.cpp] use c++ for better cleanup

pull/2140/head
Christopher Lam 6 months ago
parent a3eefc8b12
commit c8348fa4cf

@ -28,7 +28,8 @@
#include <errno.h> #include <errno.h>
#include <glib.h> #include <glib.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include <string.h> #include <string>
#include <fstream>
#include "Account.h" #include "Account.h"
#include "Transaction.h" #include "Transaction.h"
@ -85,9 +86,9 @@ static QofLogModule log_module = "gnc.translog";
static int gen_logs = 1; static int gen_logs = 1;
static FILE * trans_log = nullptr; /**< current log file handle */ static std::ofstream trans_log_stream; /**< current log file handle */
static char * trans_log_name = nullptr; /**< current log file name */ static std::string trans_log_name; /**< current log file name */
static char * log_base_name = nullptr; static std::string log_base_name;
/********************************************************************\ /********************************************************************\
\********************************************************************/ \********************************************************************/
@ -107,7 +108,7 @@ void xaccLogEnable (void)
void void
xaccReopenLog (void) xaccReopenLog (void)
{ {
if (trans_log) if (trans_log_stream.is_open())
{ {
xaccCloseLog(); xaccCloseLog();
xaccOpenLog(); xaccOpenLog();
@ -120,10 +121,9 @@ xaccLogSetBaseName (const char *basepath)
{ {
if (!basepath) return; if (!basepath) return;
g_free (log_base_name); log_base_name = basepath;
log_base_name = g_strdup (basepath);
if (trans_log) if (trans_log_stream.is_open())
{ {
xaccCloseLog(); xaccCloseLog();
xaccOpenLog(); xaccOpenLog();
@ -141,13 +141,12 @@ gboolean
xaccFileIsCurrentLog (const gchar *name) xaccFileIsCurrentLog (const gchar *name)
{ {
gchar *base; gchar *base;
gint result;
if (!name || !trans_log_name) if (!name || trans_log_name.empty())
return FALSE; return FALSE;
base = g_path_get_basename(name); base = g_path_get_basename(name);
result = (strcmp(base, trans_log_name) == 0); bool result = trans_log_name.compare(base) == 0;
g_free(base); g_free(base);
return result; return result;
} }
@ -166,17 +165,18 @@ xaccOpenLog (void)
PINFO ("Attempt to open disabled transaction log"); PINFO ("Attempt to open disabled transaction log");
return; return;
} }
if (trans_log) return; if (trans_log_stream.is_open()) return;
if (!log_base_name) log_base_name = g_strdup ("translog"); if (log_base_name.empty())
log_base_name = "translog";
/* tag each filename with a timestamp */ /* tag each filename with a timestamp */
timestamp = gnc_date_timestamp (); timestamp = gnc_date_timestamp ();
filename = g_strconcat (log_base_name, ".", timestamp, ".log", nullptr); filename = g_strconcat (log_base_name.c_str(), ".", timestamp, ".log", nullptr);
trans_log = g_fopen (filename, "a"); trans_log_stream.open(filename, std::ios::app);
if (!trans_log) if (!trans_log_stream.is_open())
{ {
int norr = errno; int norr = errno;
printf ("Error: xaccOpenLog(): cannot open journal\n" printf ("Error: xaccOpenLog(): cannot open journal\n"
@ -188,20 +188,20 @@ xaccOpenLog (void)
} }
/* Save the log file name */ /* Save the log file name */
if (trans_log_name) auto tmpstr = g_path_get_basename(filename);
g_free (trans_log_name); trans_log_name = tmpstr;
trans_log_name = g_path_get_basename(filename); g_free (tmpstr);
g_free (filename); g_free (filename);
g_free (timestamp); g_free (timestamp);
/* Note: this must match src/import-export/log-replay/gnc-log-replay.c */ /* Note: this must match src/import-export/log-replay/gnc-log-replay.c */
fprintf (trans_log, "mod\ttrans_guid\tsplit_guid\ttime_now\t" trans_log_stream << "mod\ttrans_guid\tsplit_guid\ttime_now\t"
"date_entered\tdate_posted\t" << "date_entered\tdate_posted\t"
"acc_guid\tacc_name\tnum\tdescription\t" << "acc_guid\tacc_name\tnum\tdescription\t"
"notes\tmemo\taction\treconciled\t" << "notes\tmemo\taction\treconciled\t"
"amount\tvalue\tdate_reconciled\n"); << "amount\tvalue\tdate_reconciled\n"
fprintf (trans_log, "-----------------\n"); << "-----------------\n";
} }
/********************************************************************\ /********************************************************************\
@ -210,10 +210,9 @@ xaccOpenLog (void)
void void
xaccCloseLog (void) xaccCloseLog (void)
{ {
if (!trans_log) return; if (!trans_log_stream.is_open()) return;
fflush (trans_log); trans_log_stream.flush();
fclose (trans_log); trans_log_stream.close();
trans_log = nullptr;
} }
/********************************************************************\ /********************************************************************\
@ -233,14 +232,14 @@ xaccTransWriteLog (Transaction *trans, char flag)
PINFO ("Attempt to write disabled transaction log"); PINFO ("Attempt to write disabled transaction log");
return; return;
} }
if (!trans_log) return; if (!trans_log_stream.is_open()) return;
gnc_time64_to_iso8601_buff (gnc_time(nullptr), dnow); gnc_time64_to_iso8601_buff (gnc_time(nullptr), dnow);
gnc_time64_to_iso8601_buff (trans->date_entered, dent); gnc_time64_to_iso8601_buff (trans->date_entered, dent);
gnc_time64_to_iso8601_buff (trans->date_posted, dpost); gnc_time64_to_iso8601_buff (trans->date_posted, dpost);
guid_to_string_buff (xaccTransGetGUID(trans), trans_guid_str); guid_to_string_buff (xaccTransGetGUID(trans), trans_guid_str);
trans_notes = xaccTransGetNotes(trans); trans_notes = xaccTransGetNotes(trans);
fprintf (trans_log, "===== START\n"); trans_log_stream << "===== START\n";
for (node = trans->splits; node; node = node->next) for (node = trans->splits; node; node = node->next)
{ {
@ -266,37 +265,26 @@ xaccTransWriteLog (Transaction *trans, char flag)
amt = xaccSplitGetAmount (split); amt = xaccSplitGetAmount (split);
val = xaccSplitGetValue (split); val = xaccSplitGetValue (split);
/* use tab-separated fields */ trans_log_stream << flag << '\t'
fprintf (trans_log, << trans_guid_str << '\t'
"%c\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t" << split_guid_str << '\t'
"%s\t%s\t%s\t%s\t%c\t%" G_GINT64_FORMAT "/%" G_GINT64_FORMAT "\t%" G_GINT64_FORMAT "/%" G_GINT64_FORMAT "\t%s\n", << dnow << '\t'
flag, << dent << '\t'
trans_guid_str, split_guid_str, /* trans+split make up unique id */ << dpost << '\t'
/* Note that the next three strings always exist, << acc_guid_str << '\t'
* so we don't need to test them. */ << (accname ? accname : "") << '\t'
dnow, << (trans->num ? trans->num : "") << '\t'
dent, << (trans->description ? trans->description : "") << '\t'
dpost, << (trans_notes ? trans_notes : "") << '\t'
acc_guid_str, << (split->memo ? split->memo : "") << '\t'
accname ? accname : "", << (split->action ? split->action : "") << '\t'
trans->num ? trans->num : "", << split->reconciled << '\t'
trans->description ? trans->description : "", << gnc_numeric_num(amt) << '/' << gnc_numeric_denom(amt) << '\t'
trans_notes ? trans_notes : "", << gnc_numeric_num(val) << '/' << gnc_numeric_denom(val) << '\t'
split->memo ? split->memo : "", << drecn << '\n';
split->action ? split->action : "",
split->reconciled,
gnc_numeric_num(amt),
gnc_numeric_denom(amt),
gnc_numeric_num(val),
gnc_numeric_denom(val),
/* The next string always exists. No need to test it. */
drecn);
} }
fprintf (trans_log, "===== END\n"); trans_log_stream << "===== END" << std::endl;
/* get data out to the disk */
fflush (trans_log);
} }
/************************ END OF ************************************\ /************************ END OF ************************************\

Loading…
Cancel
Save