diff --git a/demo/demo.cpp b/demo/demo.cpp index 3c025289..dc4e3af3 100644 --- a/demo/demo.cpp +++ b/demo/demo.cpp @@ -39,7 +39,7 @@ bool MyApp::OnInit() wxInitAllImageHandlers(); // enable this to route wxLog messages to a file: - // auto logFile = new LogFile; + // auto logFile = new LogFile{ true }; // delete wxLog::SetActiveTarget(logFile); MyFrame* frame = new MyFrame; diff --git a/src/ui/app.cpp b/src/ui/app.cpp index f0b50c2f..3cc404f7 100644 --- a/src/ui/app.cpp +++ b/src/ui/app.cpp @@ -51,8 +51,9 @@ bool Wisteria::UI::BaseApp::OnInit() SET_PROFILER_REPORT_PATH(m_profileReportPath.mb_str()); DUMP_PROFILER_REPORT(); // flush out data in temp file from previous run - // logs will be written to file now, delete the old logging system - m_logBuffer = new LogFile; + // Logs will be written to file now, delete the old logging system. + // (We will append to an existing log file if SHIFT key is held down.) + m_logBuffer = new LogFile{ !wxGetMouseState().ShiftDown() }; delete wxLog::SetActiveTarget(m_logBuffer); // log some system information @@ -503,4 +504,4 @@ wxString Wisteria::UI::BaseApp::FindResourceDirectoryWithAppInfo( if (wxFileName::DirExists(foundFolder)) { return foundFolder; } return wxString{}; - } \ No newline at end of file + } diff --git a/src/util/logfile.cpp b/src/util/logfile.cpp index fe77662d..960c021d 100644 --- a/src/util/logfile.cpp +++ b/src/util/logfile.cpp @@ -9,23 +9,21 @@ #include "logfile.h" //-------------------------------------------------- -LogFile::LogFile() : +LogFile::LogFile(bool clearPreviousLog) : // will be a unique file name on a per day basis m_logFilePath(wxStandardPaths::Get().GetTempDir() + wxFileName::GetPathSeparator() + wxTheApp->GetAppName() + wxDateTime::Now().FormatISODate() + L".log") { - wxFile logFile; - if (!logFile.Create(m_logFilePath, true)) + wxFile logFile(m_logFilePath, clearPreviousLog ? wxFile::write : wxFile::write_append); + if (!logFile.IsOpened()) { wxMessageBox(wxString::Format( _(L"Unable to create log file at '%s'"), m_logFilePath), _(L"Logging Error"), wxOK|wxICON_WARNING); + return; } - else - { - // clear file (from a previous program run) and prepare for appending - logFile.Write(wxEmptyString); - } + // clear file (from a previous program run) or prepare for appending + logFile.Write(wxString{}); } //-------------------------------------------------- diff --git a/src/util/logfile.h b/src/util/logfile.h index ab9f141f..81400154 100644 --- a/src/util/logfile.h +++ b/src/util/logfile.h @@ -57,17 +57,15 @@ class LogFile : public wxLog { public: /// @brief Default constructor. + /// @param clearPreviousLog @c true to clear the contents of the target log file + /// if it exists. /// @details Should be created on the heap and passed to /// @c wxLog::SetActiveTarget(). - LogFile(); + explicit LogFile(bool clearPreviousLog); /// @private LogFile(const LogFile&) = delete; /// @private - LogFile(LogFile&&) = delete; - /// @private LogFile& operator=(const LogFile&) = delete; - /// @private - LogFile& operator=(LogFile&&) = delete; /// @brief Reads (and returns) the content of the log file. /// @returns The content of the log report.