Skip to content

Commit

Permalink
Add file logging and ability to generate diagnostic zip file
Browse files Browse the repository at this point in the history
  • Loading branch information
ad3154 committed Dec 22, 2023
1 parent 96ebc69 commit 64ee36d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/LoggerComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion include/ServerMainComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ class ServerMainComponent : public juce::Component
ConfigureLanguageCommand,
ConfigureReportedVersion,
ConfigureReportedHardware,
ConfigureLogging
ConfigureLogging,
GenerateLogPackage
};

class LanguageCommandConfigClosed
Expand Down Expand Up @@ -133,6 +134,8 @@ class ServerMainComponent : public juce::Component
SoundPlayer mSoundPlayer;
AudioDeviceManager mAudioDeviceManager;
std::unique_ptr<AlertWindow> popupMenu;
std::unique_ptr<FileChooser> diagnosticFileChooser;
std::unique_ptr<ZipFile::Builder> diagnosticFileBuilder;
std::uint8_t numberOfPoolsToRender = 0;
std::uint8_t numberPhysicalSoftKeys = 6;
std::uint8_t numberVirtualSoftKeys = 64;
Expand Down
4 changes: 3 additions & 1 deletion src/LoggerComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -82,4 +83,5 @@ void LoggerComponent::sink_CAN_stack_log(LoggingLevel level, const std::string &
}
setSize(bounds.getWidth(), newSize);
repaint();
logMessage(logText);
}
66 changes: 65 additions & 1 deletion src/ServerMainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ void ServerMainComponent::getAllCommands(juce::Array<juce::CommandID> &allComman
allCommands.add(static_cast<int>(CommandIDs::ConfigureReportedVersion));
allCommands.add(static_cast<int>(CommandIDs::ConfigureReportedHardware));
allCommands.add(static_cast<int>(CommandIDs::ConfigureLogging));
allCommands.add(static_cast<int>(CommandIDs::GenerateLogPackage));
}

void ServerMainComponent::getCommandInfo(juce::CommandID commandID, ApplicationCommandInfo &result)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -666,6 +673,57 @@ bool ServerMainComponent::perform(const InvocationInfo &info)
}
break;

case static_cast<int>(CommandIDs::GenerateLogPackage):
{
diagnosticFileBuilder = std::make_unique<ZipFile::Builder>();
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;
}
Expand All @@ -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 &)
Expand All @@ -693,6 +751,12 @@ PopupMenu ServerMainComponent::getMenuForIndex(int index, const juce::String &)
break;

case 1:
{
retVal.addCommandItem(&mCommandManager, static_cast<int>(CommandIDs::GenerateLogPackage));
}
break;

case 2:
{
retVal.addCommandItem(&mCommandManager, static_cast<int>(CommandIDs::About));
}
Expand Down

0 comments on commit 64ee36d

Please sign in to comment.