2828#include < errno.h>
2929#include < glib.h>
3030#include < glib/gstdio.h>
31- #include < string.h>
31+ #include < string>
32+ #include < fstream>
3233
3334#include " Account.h"
3435#include " Transaction.h"
@@ -85,9 +86,9 @@ static QofLogModule log_module = "gnc.translog";
8586
8687
8788static int gen_logs = 1 ;
88- static FILE * trans_log = nullptr ; /* *< current log file handle */
89- static char * trans_log_name = nullptr ; /* *< current log file name */
90- static char * log_base_name = nullptr ;
89+ static std::ofstream trans_log_stream ; /* *< current log file handle */
90+ static std::string trans_log_name; /* *< current log file name */
91+ static std::string log_base_name;
9192
9293/* *******************************************************************\
9394\********************************************************************/
@@ -107,7 +108,7 @@ void xaccLogEnable (void)
107108void
108109xaccReopenLog (void )
109110{
110- if (trans_log )
111+ if (trans_log_stream. is_open () )
111112 {
112113 xaccCloseLog ();
113114 xaccOpenLog ();
@@ -120,10 +121,9 @@ xaccLogSetBaseName (const char *basepath)
120121{
121122 if (!basepath) return ;
122123
123- g_free (log_base_name);
124- log_base_name = g_strdup (basepath);
124+ log_base_name = basepath;
125125
126- if (trans_log )
126+ if (trans_log_stream. is_open () )
127127 {
128128 xaccCloseLog ();
129129 xaccOpenLog ();
@@ -141,13 +141,12 @@ gboolean
141141xaccFileIsCurrentLog (const gchar *name)
142142{
143143 gchar *base;
144- gint result;
145144
146- if (!name || ! trans_log_name)
145+ if (!name || trans_log_name. empty () )
147146 return FALSE ;
148147
149148 base = g_path_get_basename (name);
150- result = ( strcmp ( base, trans_log_name ) == 0 ) ;
149+ bool result = trans_log_name. compare ( base) == 0 ;
151150 g_free (base);
152151 return result;
153152}
@@ -166,17 +165,18 @@ xaccOpenLog (void)
166165 PINFO (" Attempt to open disabled transaction log" );
167166 return ;
168167 }
169- if (trans_log ) return ;
168+ if (trans_log_stream. is_open () ) return ;
170169
171- if (!log_base_name) log_base_name = g_strdup (" translog" );
170+ if (log_base_name.empty ())
171+ log_base_name = " translog" ;
172172
173173 /* tag each filename with a timestamp */
174174 timestamp = gnc_date_timestamp ();
175175
176- filename = g_strconcat (log_base_name, " ." , timestamp, " .log" , nullptr );
176+ filename = g_strconcat (log_base_name. c_str () , " ." , timestamp, " .log" , nullptr );
177177
178- trans_log = g_fopen (filename, " a " );
179- if (!trans_log )
178+ trans_log_stream. open (filename, std::ios::app );
179+ if (!trans_log_stream. is_open () )
180180 {
181181 int norr = errno;
182182 printf (" Error: xaccOpenLog(): cannot open journal\n "
@@ -188,20 +188,20 @@ xaccOpenLog (void)
188188 }
189189
190190 /* Save the log file name */
191- if (trans_log_name)
192- g_free (trans_log_name) ;
193- trans_log_name = g_path_get_basename (filename );
191+ auto tmpstr = g_path_get_basename (filename);
192+ trans_log_name = tmpstr ;
193+ g_free (tmpstr );
194194
195195 g_free (filename);
196196 g_free (timestamp);
197197
198198 /* Note: this must match src/import-export/log-replay/gnc-log-replay.c */
199- fprintf (trans_log, " mod\t trans_guid\t split_guid\t time_now\t "
200- " date_entered\t date_posted\t "
201- " acc_guid\t acc_name\t num\t description\t "
202- " notes\t memo\t action\t reconciled\t "
203- " amount\t value\t date_reconciled\n " );
204- fprintf (trans_log, " -----------------\n " ) ;
199+ trans_log_stream << " mod\t trans_guid\t split_guid\t time_now\t "
200+ << " date_entered\t date_posted\t "
201+ << " acc_guid\t acc_name\t num\t description\t "
202+ << " notes\t memo\t action\t reconciled\t "
203+ << " amount\t value\t date_reconciled\n "
204+ << " -----------------\n " ;
205205}
206206
207207/* *******************************************************************\
@@ -210,10 +210,9 @@ xaccOpenLog (void)
210210void
211211xaccCloseLog (void )
212212{
213- if (!trans_log) return ;
214- fflush (trans_log);
215- fclose (trans_log);
216- trans_log = nullptr ;
213+ if (!trans_log_stream.is_open ()) return ;
214+ trans_log_stream.flush ();
215+ trans_log_stream.close ();
217216}
218217
219218/* *******************************************************************\
@@ -233,14 +232,14 @@ xaccTransWriteLog (Transaction *trans, char flag)
233232 PINFO (" Attempt to write disabled transaction log" );
234233 return ;
235234 }
236- if (!trans_log ) return ;
235+ if (!trans_log_stream. is_open () ) return ;
237236
238237 gnc_time64_to_iso8601_buff (gnc_time (nullptr ), dnow);
239238 gnc_time64_to_iso8601_buff (trans->date_entered , dent);
240239 gnc_time64_to_iso8601_buff (trans->date_posted , dpost);
241240 guid_to_string_buff (xaccTransGetGUID (trans), trans_guid_str);
242241 trans_notes = xaccTransGetNotes (trans);
243- fprintf (trans_log, " ===== START\n " ) ;
242+ trans_log_stream << " ===== START\n " ;
244243
245244 for (node = trans->splits ; node; node = node->next )
246245 {
@@ -266,37 +265,26 @@ xaccTransWriteLog (Transaction *trans, char flag)
266265 amt = xaccSplitGetAmount (split);
267266 val = xaccSplitGetValue (split);
268267
269- /* use tab-separated fields */
270- fprintf (trans_log,
271- " %c\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t "
272- " %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 " ,
273- flag,
274- trans_guid_str, split_guid_str, /* trans+split make up unique id */
275- /* Note that the next three strings always exist,
276- * so we don't need to test them. */
277- dnow,
278- dent,
279- dpost,
280- acc_guid_str,
281- accname ? accname : " " ,
282- trans->num ? trans->num : " " ,
283- trans->description ? trans->description : " " ,
284- trans_notes ? trans_notes : " " ,
285- split->memo ? split->memo : " " ,
286- split->action ? split->action : " " ,
287- split->reconciled ,
288- gnc_numeric_num (amt),
289- gnc_numeric_denom (amt),
290- gnc_numeric_num (val),
291- gnc_numeric_denom (val),
292- /* The next string always exists. No need to test it. */
293- drecn);
268+ trans_log_stream << flag << ' \t '
269+ << trans_guid_str << ' \t '
270+ << split_guid_str << ' \t '
271+ << dnow << ' \t '
272+ << dent << ' \t '
273+ << dpost << ' \t '
274+ << acc_guid_str << ' \t '
275+ << (accname ? accname : " " ) << ' \t '
276+ << (trans->num ? trans->num : " " ) << ' \t '
277+ << (trans->description ? trans->description : " " ) << ' \t '
278+ << (trans_notes ? trans_notes : " " ) << ' \t '
279+ << (split->memo ? split->memo : " " ) << ' \t '
280+ << (split->action ? split->action : " " ) << ' \t '
281+ << split->reconciled << ' \t '
282+ << gnc_numeric_num (amt) << ' /' << gnc_numeric_denom (amt) << ' \t '
283+ << gnc_numeric_num (val) << ' /' << gnc_numeric_denom (val) << ' \t '
284+ << drecn << ' \n ' ;
294285 }
295286
296- fprintf (trans_log, " ===== END\n " );
297-
298- /* get data out to the disk */
299- fflush (trans_log);
287+ trans_log_stream << " ===== END" << std::endl;
300288}
301289
302290/* *********************** END OF ************************************\
0 commit comments