From 64ee36deb98d347c7fa3363a72249e406017578e Mon Sep 17 00:00:00 2001 From: Adrian Del Grosso <10929341+ad3154@users.noreply.github.com> Date: Thu, 21 Dec 2023 16:30:59 -0700 Subject: [PATCH] Add file logging and ability to generate diagnostic zip file --- include/LoggerComponent.hpp | 1 + include/ServerMainComponent.hpp | 5 ++- src/LoggerComponent.cpp | 4 +- src/ServerMainComponent.cpp | 66 ++++++++++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/include/LoggerComponent.hpp b/include/LoggerComponent.hpp index 91c8d84..d8a6305 100644 --- a/include/LoggerComponent.hpp +++ b/include/LoggerComponent.hpp @@ -17,6 +17,7 @@ /// @brief Defines a GUI component that will draw log info sunk from the stack class LoggerComponent : public Component + , public FileLogger , public isobus::CANStackLogger { public: diff --git a/include/ServerMainComponent.hpp b/include/ServerMainComponent.hpp index e0405af..d38b8cf 100644 --- a/include/ServerMainComponent.hpp +++ b/include/ServerMainComponent.hpp @@ -98,7 +98,8 @@ class ServerMainComponent : public juce::Component ConfigureLanguageCommand, ConfigureReportedVersion, ConfigureReportedHardware, - ConfigureLogging + ConfigureLogging, + GenerateLogPackage }; class LanguageCommandConfigClosed @@ -133,6 +134,8 @@ class ServerMainComponent : public juce::Component SoundPlayer mSoundPlayer; AudioDeviceManager mAudioDeviceManager; std::unique_ptr popupMenu; + std::unique_ptr diagnosticFileChooser; + std::unique_ptr diagnosticFileBuilder; std::uint8_t numberOfPoolsToRender = 0; std::uint8_t numberPhysicalSoftKeys = 6; std::uint8_t numberVirtualSoftKeys = 64; diff --git a/src/LoggerComponent.cpp b/src/LoggerComponent.cpp index 35499ba..48dc5df 100644 --- a/src/LoggerComponent.cpp +++ b/src/LoggerComponent.cpp @@ -8,7 +8,8 @@ //================================================================================================ #include "LoggerComponent.hpp" -LoggerComponent::LoggerComponent() +LoggerComponent::LoggerComponent() : + FileLogger(File(File::getSpecialLocation(File::userApplicationDataDirectory).getFullPathName() + "/Open-Agriculture/AgISOVirtualTerminalLog.txt"), "Starting AgIsoVirtualTerminal", 1024000) { auto bounds = getLocalBounds(); setBounds(10, 10, bounds.getWidth() - 10, bounds.getHeight() - 10); @@ -82,4 +83,5 @@ void LoggerComponent::sink_CAN_stack_log(LoggingLevel level, const std::string & } setSize(bounds.getWidth(), newSize); repaint(); + logMessage(logText); } diff --git a/src/ServerMainComponent.cpp b/src/ServerMainComponent.cpp index af508f9..9d1f652 100644 --- a/src/ServerMainComponent.cpp +++ b/src/ServerMainComponent.cpp @@ -527,6 +527,7 @@ void ServerMainComponent::getAllCommands(juce::Array &allComman allCommands.add(static_cast(CommandIDs::ConfigureReportedVersion)); allCommands.add(static_cast(CommandIDs::ConfigureReportedHardware)); allCommands.add(static_cast(CommandIDs::ConfigureLogging)); + allCommands.add(static_cast(CommandIDs::GenerateLogPackage)); } void ServerMainComponent::getCommandInfo(juce::CommandID commandID, ApplicationCommandInfo &result) @@ -563,6 +564,12 @@ void ServerMainComponent::getCommandInfo(juce::CommandID commandID, ApplicationC } break; + case CommandIDs::GenerateLogPackage: + { + result.setInfo("Generate Diagnostic Package", "Creates a zip file of diagnostic information", "Troubleshooting", 0); + } + break; + case CommandIDs::NoCommand: default: break; @@ -666,6 +673,57 @@ bool ServerMainComponent::perform(const InvocationInfo &info) } break; + case static_cast(CommandIDs::GenerateLogPackage): + { + diagnosticFileBuilder = std::make_unique(); + bool anyFilesAdded = false; + + if (File(File::getSpecialLocation(File::userApplicationDataDirectory).getFullPathName() + File::getSeparatorString() + "Open-Agriculture" + File::getSeparatorString() + "AgISOVirtualTerminalLog.txt").existsAsFile()) + { + diagnosticFileBuilder->addFile(File::getSpecialLocation(File::userApplicationDataDirectory).getFullPathName() + File::getSeparatorString() + "Open-Agriculture" + File::getSeparatorString() + "AgISOVirtualTerminalLog.txt", 9); + anyFilesAdded = true; + } + if (File(File::getSpecialLocation(File::userApplicationDataDirectory).getFullPathName() + File::getSeparatorString() + "Open-Agriculture" + File::getSeparatorString() + "vt_settings.xml").existsAsFile()) + { + diagnosticFileBuilder->addFile(File::getSpecialLocation(File::userApplicationDataDirectory).getFullPathName() + File::getSeparatorString() + "Open-Agriculture" + File::getSeparatorString() + "vt_settings.xml", 9); + anyFilesAdded = true; + } + + auto executablesFolder = File(File::getSpecialLocation(File::hostApplicationPath).getParentDirectory().getFullPathName() + File::getSeparatorString() + "iso_data"); + auto childDirectories = executablesFolder.findChildFiles(File::TypesOfFileToFind::findDirectories, false, "*"); + + for (auto &directory : childDirectories) + { + auto childFiles = directory.findChildFiles(File::TypesOfFileToFind::findFiles, false, "*.iop"); + for (auto &file : childFiles) + { + diagnosticFileBuilder->addFile(file, 9); + anyFilesAdded = true; + } + } + + if (anyFilesAdded) + { + auto currentTime = Time::getCurrentTime().toString(true, true, true, false); + currentTime = currentTime.replaceCharacter(' ', '_'); + currentTime = currentTime.replaceCharacter(':', '_'); + const String fileName = File::getSpecialLocation(File::userApplicationDataDirectory).getFullPathName() + File::getSeparatorString() + "Open-Agriculture" + File::getSeparatorString() + "AgISOVirtualTerminalLogs_" + currentTime + ".zip"; + auto output = File(fileName).createOutputStream(); + diagnosticFileBuilder->writeToStream(*output.get(), nullptr); + File(fileName).revealToUser(); + } + else + { + AlertWindow::showAsync(MessageBoxOptions() + .withIconType(MessageBoxIconType::WarningIcon) + .withTitle("Export Failed") + .withButton("OK"), + nullptr); + } + retVal = true; + } + break; + default: break; } @@ -674,7 +732,7 @@ bool ServerMainComponent::perform(const InvocationInfo &info) StringArray ServerMainComponent::getMenuBarNames() { - return juce::StringArray("Configure", "About"); + return juce::StringArray("Configure", "Troubleshooting", "About"); } PopupMenu ServerMainComponent::getMenuForIndex(int index, const juce::String &) @@ -693,6 +751,12 @@ PopupMenu ServerMainComponent::getMenuForIndex(int index, const juce::String &) break; case 1: + { + retVal.addCommandItem(&mCommandManager, static_cast(CommandIDs::GenerateLogPackage)); + } + break; + + case 2: { retVal.addCommandItem(&mCommandManager, static_cast(CommandIDs::About)); }