From b6eaf5623e50b2e7826eba279019fb74c01bc2b7 Mon Sep 17 00:00:00 2001 From: Leland Lucius Date: Tue, 11 Aug 2020 00:14:56 -0500 Subject: [PATCH] Add "tracing" of macro commands At this time it's really just a debugging aid for us. It should make it easier for us to better understand timing of various commands. Tracing is enabled by setting the "/EnableMacroTracing" preference to 1 and disabled by setting it to 0. Since the tracing is written to the log, this also adds a "SaveLog" command that takes a filename. It can be used from within the macro being traced to preserve the log in case the script intended to exit Audacity. --- src/AudacityLogger.cpp | 13 +++++++++++++ src/AudacityLogger.h | 2 ++ src/BatchCommands.cpp | 21 ++++++++++++++++++++- src/commands/OpenSaveCommands.cpp | 28 ++++++++++++++++++++++++++++ src/commands/OpenSaveCommands.h | 21 +++++++++++++++++++++ 5 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/AudacityLogger.cpp b/src/AudacityLogger.cpp index a9417f5d3..14ec64d36 100644 --- a/src/AudacityLogger.cpp +++ b/src/AudacityLogger.cpp @@ -110,6 +110,19 @@ void AudacityLogger::DoLogText(const wxString & str) } } +bool AudacityLogger::SaveLog(const FilePath &fileName) const +{ + wxFFile file(fileName, wxT("w")); + + if (file.IsOpened()) { + file.Write(mBuffer); + file.Close(); + return true; + } + + return false; +} + void AudacityLogger::Show(bool show) { // Hide the frame if created, otherwise do nothing diff --git a/src/AudacityLogger.h b/src/AudacityLogger.h index e5e3ee822..35a4cf71f 100644 --- a/src/AudacityLogger.h +++ b/src/AudacityLogger.h @@ -33,6 +33,8 @@ class AudacityLogger final : public wxEvtHandler, public wxLog { void Show(bool show = true); + bool SaveLog(const FilePath &fileName) const; + #if defined(EXPERIMENTAL_CRASH_REPORT) wxString GetLog(); #endif diff --git a/src/BatchCommands.cpp b/src/BatchCommands.cpp index 7c1ed4377..332ecfdbf 100644 --- a/src/BatchCommands.cpp +++ b/src/BatchCommands.cpp @@ -732,6 +732,9 @@ bool MacroCommands::ApplyMacro( mAbort = false; + bool trace; + gPrefs->Read(wxT("/EnableMacroTracing"), &trace, false); + size_t i = 0; for (; i < mCommandMacro.size(); i++) { const auto &command = mCommandMacro[i]; @@ -742,7 +745,23 @@ bool MacroCommands::ApplyMacro( // in default of any better friendly name Verbatim( command.GET() ) : iter->name.Msgid().Stripped(); - if (!ApplyCommandInBatchMode(friendly, command, mParamsMacro[i]) || mAbort) + + wxTimeSpan before; + if (trace) { + before = wxTimeSpan(0, 0, 0, wxGetUTCTimeMillis()); + } + + bool success = ApplyCommandInBatchMode(friendly, command, mParamsMacro[i]); + + if (trace) { + auto after = wxTimeSpan(0, 0, 0, wxGetUTCTimeMillis()); + wxLogMessage(wxT("Macro line #%ld \"%s\" took %s"), + i + 1, + command.GET(), + (after - before).Format(wxT("%H:%M:%S.%l"))); + } + + if (!success || mAbort) break; } diff --git a/src/commands/OpenSaveCommands.cpp b/src/commands/OpenSaveCommands.cpp index 921090b4c..ca307b1f8 100644 --- a/src/commands/OpenSaveCommands.cpp +++ b/src/commands/OpenSaveCommands.cpp @@ -18,6 +18,7 @@ #include "OpenSaveCommands.h" #include "LoadCommands.h" +#include "../AudacityLogger.h" #include "../Project.h" #include "../ProjectFileIO.h" #include "../ProjectFileManager.h" @@ -132,3 +133,30 @@ bool SaveCopyCommand::Apply(const CommandContext &context) auto &projectFileManager = ProjectFileManager::Get( context.project ); return projectFileManager.SaveCopy(mFileName); } + +const ComponentInterfaceSymbol SaveLogCommand::Symbol +{ XO("Save Log") }; + +namespace{ BuiltinCommandsModule::Registration< SaveLogCommand > reg4; } + +bool SaveLogCommand::DefineParams( ShuttleParams & S ){ + S.Define( mFileName, wxT("Filename"), "log.txt" ); + return true; +} + +void SaveLogCommand::PopulateOrExchange(ShuttleGui & S) +{ + S.AddSpace(0, 5); + + S.StartMultiColumn(2, wxALIGN_CENTER); + { + S.TieTextBox(XXO("File Name:"),mFileName); + } + S.EndMultiColumn(); +} + +bool SaveLogCommand::Apply(const CommandContext &context) +{ + auto logger = AudacityLogger::Get(); + return logger->SaveLog(mFileName); +} diff --git a/src/commands/OpenSaveCommands.h b/src/commands/OpenSaveCommands.h index ee869787a..02ecda8ca 100644 --- a/src/commands/OpenSaveCommands.h +++ b/src/commands/OpenSaveCommands.h @@ -19,6 +19,9 @@ \class SaveCopyCommand \brief Command for saving a copy of currenty project +\class SaveLogCommand +\brief Command for saving the log contents + *//*******************************************************************/ #include "Command.h" @@ -81,3 +84,21 @@ public: public: wxString mFileName; }; + +class SaveLogCommand : public AudacityCommand +{ +public: + static const ComponentInterfaceSymbol Symbol; + + // ComponentInterface overrides + ComponentInterfaceSymbol GetSymbol() override {return Symbol;}; + TranslatableString GetDescription() override {return XO("Saves the log contents.");}; + bool DefineParams( ShuttleParams & S ) override; + void PopulateOrExchange(ShuttleGui & S) override; + bool Apply(const CommandContext & context) override; + + // AudacityCommand overrides + wxString ManualPage() override {return wxT("Extra_Menu:_Scriptables_II#save_log");}; +public: + wxString mFileName; +};