From 8da6529329e54c8bdba17e8b5b88907f521838e5 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Fri, 26 Apr 2019 10:20:49 -0400 Subject: [PATCH] Remove AudacityApp::GetLogger() & HelpActions::DoShowLog() --- src/AudacityApp.cpp | 16 +++------------- src/AudacityApp.h | 3 --- src/AudacityLogger.cpp | 17 +++++++++++++++++ src/AudacityLogger.h | 6 +++++- src/Menus.h | 1 - src/menus/HelpMenus.cpp | 13 ++++--------- src/widgets/MultiDialog.cpp | 10 +++++----- 7 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/AudacityApp.cpp b/src/AudacityApp.cpp index 82f981486..1bc4c9e47 100644 --- a/src/AudacityApp.cpp +++ b/src/AudacityApp.cpp @@ -831,13 +831,6 @@ locations of the missing files."), missingFileName); } } -AudacityLogger *AudacityApp::GetLogger() -{ - // Use dynamic_cast so that we get a NULL ptr if we haven't yet - // setup our logger. - return dynamic_cast(wxLog::GetActiveTarget()); -} - #if defined(__WXMSW__) #define WL(lang, sublang) (lang), (sublang), #else @@ -944,7 +937,7 @@ void AudacityApp::GenerateCrashReport(wxDebugReport::Context ctx) #endif } - AudacityLogger *logger = GetLogger(); + auto logger = AudacityLogger::Get(); if (logger) { rpt.AddText(wxT("log.txt"), logger->GetLog(), _TS("Audacity Log")); @@ -1051,11 +1044,8 @@ bool AudacityApp::OnInit() // Ensure we have an event loop during initialization wxEventLoopGuarantor eventLoop; - // wxWidgets will clean up the logger for the main thread, so we can say - // safenew. See: - // http://docs.wxwidgets.org/3.0/classwx_log.html#a2525bf54fa3f31dc50e6e3cd8651e71d - std::unique_ptr < wxLog > - { wxLog::SetActiveTarget(safenew AudacityLogger) }; // DELETE old + // cause initialization of wxWidgets' global logger target + (void) AudacityLogger::Get(); #if defined(__WXMAC__) // Disable window animation diff --git a/src/AudacityApp.h b/src/AudacityApp.h index 55b8da308..f05d1877f 100644 --- a/src/AudacityApp.h +++ b/src/AudacityApp.h @@ -36,7 +36,6 @@ class IPCServ; class Importer; class CommandHandler; class AppCommandEvent; -class AudacityLogger; class AudacityProject; class AliasBlockFile; @@ -93,8 +92,6 @@ class AudacityApp final : public wxApp { void AssociateFileTypes(); #endif - AudacityLogger *GetLogger(); - #if defined(EXPERIMENTAL_CRASH_REPORT) void GenerateCrashReport(wxDebugReport::Context ctx); #endif diff --git a/src/AudacityLogger.cpp b/src/AudacityLogger.cpp index d872a0c39..2da109ba3 100644 --- a/src/AudacityLogger.cpp +++ b/src/AudacityLogger.cpp @@ -22,6 +22,7 @@ Provides thread-safe logging based on the wxWidgets log facility. #include "FileNames.h" #include "ShuttleGui.h" +#include #include #include #include @@ -50,6 +51,22 @@ enum LoggerID_Close }; +AudacityLogger *AudacityLogger::Get() +{ + static std::once_flag flag; + std::call_once( flag, []{ + // wxWidgets will clean up the logger for the main thread, so we can say + // safenew. See: + // http://docs.wxwidgets.org/3.0/classwx_log.html#a2525bf54fa3f31dc50e6e3cd8651e71d + std::unique_ptr < wxLog > // DELETE any previous logger + { wxLog::SetActiveTarget(safenew AudacityLogger) }; + } ); + + // Use dynamic_cast so that we get a NULL ptr in case our logger + // is no longer the target. + return dynamic_cast(wxLog::GetActiveTarget()); +} + AudacityLogger::AudacityLogger() : wxEvtHandler(), wxLog() diff --git a/src/AudacityLogger.h b/src/AudacityLogger.h index 2dd7cc98e..e5e3ee822 100644 --- a/src/AudacityLogger.h +++ b/src/AudacityLogger.h @@ -27,7 +27,9 @@ class wxTextCtrl; class AudacityLogger final : public wxEvtHandler, public wxLog { public: - AudacityLogger(); + + // Get the singleton instance or null + static AudacityLogger *Get(); void Show(bool show = true); @@ -40,6 +42,8 @@ class AudacityLogger final : public wxEvtHandler, public wxLog { void DoLogText(const wxString & msg) override; private: + AudacityLogger(); + void OnCloseWindow(wxCloseEvent & e); void OnClose(wxCommandEvent & e); void OnClear(wxCommandEvent & e); diff --git a/src/Menus.h b/src/Menus.h index 6bedcf754..f67ab5930 100644 --- a/src/Menus.h +++ b/src/Menus.h @@ -180,7 +180,6 @@ bool DoAudacityCommand( /// Namespace for functions for Help menu namespace HelpActions { void DoHelpWelcome( AudacityProject & ); -void DoShowLog( AudacityProject& ); } #endif diff --git a/src/menus/HelpMenus.cpp b/src/menus/HelpMenus.cpp index a4780c9d1..8058ee49e 100644 --- a/src/menus/HelpMenus.cpp +++ b/src/menus/HelpMenus.cpp @@ -75,14 +75,6 @@ namespace HelpActions { // exported helper functions -void DoShowLog( AudacityProject & ) -{ - AudacityLogger *logger = wxGetApp().GetLogger(); - if (logger) { - logger->Show(); - } -} - void DoHelpWelcome( AudacityProject &project ) { SplashDialog::Show2( &project ); @@ -135,7 +127,10 @@ void OnMidiDeviceInfo(const CommandContext &context) void OnShowLog( const CommandContext &context ) { - DoShowLog( context.project ); + auto logger = AudacityLogger::Get(); + if (logger) { + logger->Show(); + } } #if defined(EXPERIMENTAL_CRASH_REPORT) diff --git a/src/widgets/MultiDialog.cpp b/src/widgets/MultiDialog.cpp index 587950ef1..efdecb0f3 100644 --- a/src/widgets/MultiDialog.cpp +++ b/src/widgets/MultiDialog.cpp @@ -21,8 +21,6 @@ for each problem encountered, since there can be many orphans. #include "../Audacity.h" #include "MultiDialog.h" -#include "../Project.h" - #include #include #include @@ -34,7 +32,7 @@ for each problem encountered, since there can be many orphans. #include #include -#include "../Menus.h" +#include "../AudacityLogger.h" #include "wxPanelWrapper.h" class MultiDialog final : public wxDialogWrapper @@ -153,8 +151,10 @@ void MultiDialog::OnOK(wxCommandEvent & WXUNUSED(event)) void MultiDialog::OnShowLog(wxCommandEvent & WXUNUSED(event)) { - auto project = GetActiveProject(); - HelpActions::DoShowLog(*project); + auto logger = AudacityLogger::Get(); + if (logger) { + logger->Show(); + } }