1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-07-14 07:37:43 +02:00

Separate LogWindow from AudacityLogger, so the latter uses no GUI

This commit is contained in:
Paul Licameli 2021-04-16 23:02:07 -04:00
parent 03f77429a0
commit e3f9b3cbd4
4 changed files with 57 additions and 59 deletions

View File

@ -70,6 +70,11 @@ struct LogWindowUpdater : public PrefsListener
// Unique PrefsListener can't be statically constructed before the application // Unique PrefsListener can't be statically constructed before the application
// object initializes, so use Optional // object initializes, so use Optional
std::optional<LogWindowUpdater> pUpdater; std::optional<LogWindowUpdater> pUpdater;
void OnCloseWindow(wxCloseEvent & e);
void OnClose(wxCommandEvent & e);
void OnClear(wxCommandEvent & e);
void OnSave(wxCommandEvent & e);
} }
AudacityLogger *AudacityLogger::Get() AudacityLogger *AudacityLogger::Get()
@ -156,7 +161,7 @@ bool AudacityLogger::ClearLog()
return true; return true;
} }
void AudacityLogger::Show(bool show) void LogWindow::Show(bool show)
{ {
// Hide the frame if created, otherwise do nothing // Hide the frame if created, otherwise do nothing
if (!show) { if (!show) {
@ -167,9 +172,11 @@ void AudacityLogger::Show(bool show)
} }
// If the frame already exists, refresh its contents and show it // If the frame already exists, refresh its contents and show it
auto pLogger = AudacityLogger::Get();
if (sFrame) { if (sFrame) {
if (!sFrame->IsShown() && sText) { if (!sFrame->IsShown() && sText) {
sText->ChangeValue(mBuffer); if (pLogger)
sText->ChangeValue(pLogger->GetBuffer());
sText->SetInsertionPointEnd(); sText->SetInsertionPointEnd();
sText->ShowPosition(sText->GetLastPosition()); sText->ShowPosition(sText->GetLastPosition());
} }
@ -207,7 +214,7 @@ void AudacityLogger::Show(bool show)
S.StartVerticalLay(true); S.StartVerticalLay(true);
{ {
sText = S.Style(wxTE_MULTILINE | wxHSCROLL | wxTE_READONLY | wxTE_RICH) sText = S.Style(wxTE_MULTILINE | wxHSCROLL | wxTE_READONLY | wxTE_RICH)
.AddTextWindow(mBuffer); .AddTextWindow({}); // Populate this text window below
S.AddSpace(0, 5); S.AddSpace(0, 5);
S.StartHorizontalLay(wxALIGN_CENTER, 0); S.StartHorizontalLay(wxALIGN_CENTER, 0);
@ -231,40 +238,28 @@ void AudacityLogger::Show(bool show)
frame->Layout(); frame->Layout();
// Hook into the frame events // Hook into the frame events
frame->Bind(wxEVT_CLOSE_WINDOW, frame->Bind(wxEVT_CLOSE_WINDOW, OnCloseWindow );
wxCloseEventHandler(AudacityLogger::OnCloseWindow),
this);
frame->Bind( wxEVT_COMMAND_MENU_SELECTED, frame->Bind( wxEVT_COMMAND_MENU_SELECTED, OnSave, LoggerID_Save);
&AudacityLogger::OnSave, frame->Bind( wxEVT_COMMAND_MENU_SELECTED, OnClear, LoggerID_Clear);
this, LoggerID_Save); frame->Bind( wxEVT_COMMAND_MENU_SELECTED, OnClose, LoggerID_Close);
frame->Bind( wxEVT_COMMAND_MENU_SELECTED, frame->Bind( wxEVT_COMMAND_BUTTON_CLICKED, OnSave, LoggerID_Save);
&AudacityLogger::OnClear, frame->Bind( wxEVT_COMMAND_BUTTON_CLICKED, OnClear, LoggerID_Clear);
this, LoggerID_Clear); frame->Bind( wxEVT_COMMAND_BUTTON_CLICKED, OnClose, LoggerID_Close);
frame->Bind( wxEVT_COMMAND_MENU_SELECTED,
&AudacityLogger::OnClose,
this, LoggerID_Close);
frame->Bind( wxEVT_COMMAND_BUTTON_CLICKED,
&AudacityLogger::OnSave,
this, LoggerID_Save);
frame->Bind( wxEVT_COMMAND_BUTTON_CLICKED,
&AudacityLogger::OnClear,
this, LoggerID_Clear);
frame->Bind( wxEVT_COMMAND_BUTTON_CLICKED,
&AudacityLogger::OnClose,
this, LoggerID_Close);
sFrame = std::move( frame ); sFrame = std::move( frame );
sFrame->Show(); sFrame->Show();
Flush(); if (pLogger)
pLogger->Flush();
// Also create the listeners // Also create the listeners
if (!pUpdater) if (!pUpdater)
pUpdater.emplace(); pUpdater.emplace();
SetListener([]{ if (pLogger) {
pLogger->SetListener([]{
if (auto pLogger = AudacityLogger::Get()) { if (auto pLogger = AudacityLogger::Get()) {
if (sFrame && sFrame->IsShown()) { if (sFrame && sFrame->IsShown()) {
if (sText) if (sText)
@ -274,6 +269,10 @@ void AudacityLogger::Show(bool show)
} }
return false; return false;
}); });
// Initial flush populates sText
pLogger->Flush();
}
} }
wxString AudacityLogger::GetLog(int count) wxString AudacityLogger::GetLog(int count)
@ -294,7 +293,8 @@ wxString AudacityLogger::GetLog(int count)
return buffer; return buffer;
} }
void AudacityLogger::OnCloseWindow(wxCloseEvent & WXUNUSED(e)) namespace {
void OnCloseWindow(wxCloseEvent & WXUNUSED(e))
{ {
#if defined(__WXMAC__) #if defined(__WXMAC__)
// On the Mac, destroy the window rather than hiding it since the // On the Mac, destroy the window rather than hiding it since the
@ -302,22 +302,23 @@ void AudacityLogger::OnCloseWindow(wxCloseEvent & WXUNUSED(e))
// project window open. // project window open.
sFrame.reset(); sFrame.reset();
#else #else
Show(false); sFrame->Show(false);
#endif #endif
} }
void AudacityLogger::OnClose(wxCommandEvent & WXUNUSED(e)) void OnClose(wxCommandEvent & WXUNUSED(e))
{ {
wxCloseEvent dummy; wxCloseEvent dummy;
OnCloseWindow(dummy); OnCloseWindow(dummy);
} }
void AudacityLogger::OnClear(wxCommandEvent & WXUNUSED(e)) void OnClear(wxCommandEvent & WXUNUSED(e))
{ {
ClearLog(); if (auto pLogger = AudacityLogger::Get())
pLogger->ClearLog();
} }
void AudacityLogger::OnSave(wxCommandEvent & WXUNUSED(e)) void OnSave(wxCommandEvent & WXUNUSED(e))
{ {
wxString fName = _("log.txt"); wxString fName = _("log.txt");
@ -350,11 +351,12 @@ void LogWindowUpdater::UpdatePrefs()
if (sFrame) { if (sFrame) {
bool shown = sFrame->IsShown(); bool shown = sFrame->IsShown();
if (shown) { if (shown) {
AudacityLogger::Get()->Show(false); LogWindow::Show(false);
} }
sFrame.reset(); sFrame.reset();
if (shown) { if (shown) {
AudacityLogger::Get()->Show(true); LogWindow::Show(true);
} }
} }
} }
}

View File

@ -30,17 +30,16 @@ class AUDACITY_DLL_API AudacityLogger final : public wxEvtHandler,
// Get the singleton instance or null // Get the singleton instance or null
static AudacityLogger *Get(); static AudacityLogger *Get();
void Show(bool show = true);
bool SaveLog(const wxString &fileName) const; bool SaveLog(const wxString &fileName) const;
bool ClearLog(); bool ClearLog();
//! Retrieve all or some of the lines since most recent ClearLog or start of program
/*! If `count == 0` or is more than the number of lines, return all; else return the last `count` lines */
wxString GetLog(int count = 0); wxString GetLog(int count = 0);
//! Get all the accumulated text since program start or last ClearLog() //! Get all the accumulated text since program start or last ClearLog()
const wxString &GetBuffer() const { return mBuffer; } const wxString &GetBuffer() const { return mBuffer; }
protected:
void Flush() override; void Flush() override;
//! Type of function called by Flush //! Type of function called by Flush
@ -57,14 +56,17 @@ protected:
private: private:
AudacityLogger(); AudacityLogger();
void OnCloseWindow(wxCloseEvent & e);
void OnClose(wxCommandEvent & e);
void OnClear(wxCommandEvent & e);
void OnSave(wxCommandEvent & e);
Listener mListener; Listener mListener;
wxString mBuffer; wxString mBuffer;
bool mUpdated; bool mUpdated;
}; };
//! Maintains the unique logging window which displays debug information
class AUDACITY_DLL_API LogWindow
{
public:
//! Show or hide the unique logging window; create it on demand the first time it is shown
static void Show(bool show = true);
};
#endif #endif

View File

@ -343,10 +343,7 @@ void OnMidiDeviceInfo(const CommandContext &context)
void OnShowLog( const CommandContext &context ) void OnShowLog( const CommandContext &context )
{ {
auto logger = AudacityLogger::Get(); LogWindow::Show();
if (logger) {
logger->Show();
}
} }
#if defined(HAS_CRASH_REPORT) #if defined(HAS_CRASH_REPORT)

View File

@ -169,10 +169,7 @@ void MultiDialog::OnOK(wxCommandEvent & WXUNUSED(event))
void MultiDialog::OnShowLog(wxCommandEvent & WXUNUSED(event)) void MultiDialog::OnShowLog(wxCommandEvent & WXUNUSED(event))
{ {
auto logger = AudacityLogger::Get(); LogWindow::Show();
if (logger) {
logger->Show();
}
} }
void MultiDialog::OnHelp(wxCommandEvent & WXUNUSED(event)) void MultiDialog::OnHelp(wxCommandEvent & WXUNUSED(event))