-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Starting on logging module * Fixing some things on Windows * Refactoring * Adding log file start message * Update README and CHANGELOG * Apply clang-format * Update for CI * Apply clang-format * More CI fixing * More CI fixing * Fixing logging test * Add maybe_unused --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
5f52b05
commit ecc530e
Showing
197 changed files
with
32,164 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
modules/common/chowdsp_logging/Loggers/chowdsp_BaseLogger.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
namespace chowdsp | ||
{ | ||
struct BaseLogger : juce::Logger | ||
{ | ||
spdlog::logger internal_logger { "chowdsp_log" }; | ||
spdlog::sink_ptr console_sink {}; | ||
|
||
BaseLogger() | ||
{ | ||
console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>(); | ||
internal_logger.sinks().push_back (console_sink); | ||
|
||
#if JUCE_DEBUG && JUCE_WINDOWS | ||
internal_logger.sinks().push_back (std::make_shared<spdlog::sinks::msvc_sink_mt>()); | ||
#endif | ||
} | ||
|
||
void logMessage (const juce::String& message) override | ||
{ | ||
internal_logger.info (message.toStdString()); | ||
} | ||
}; | ||
} // namespace chowdsp |
67 changes: 67 additions & 0 deletions
67
modules/common/chowdsp_logging/Loggers/chowdsp_CrashLogHelpers.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "chowdsp_CrashLogHelpers.h" | ||
|
||
namespace chowdsp::CrashLogHelpers | ||
{ | ||
void defaultCrashLogAnalyzer (const juce::File& logFile) | ||
{ | ||
#if JUCE_MODULE_AVAILABLE_juce_gui_basics | ||
auto alertOptions = | ||
juce::MessageBoxOptions() | ||
.withTitle ("Crash detected!") | ||
.withMessage ( | ||
"A previous instance of this plugin has crashed! Would you like to view the logs?") | ||
#if JUCE_IOS | ||
.withButton ("Copy Logs") | ||
#else | ||
.withButton ("Show Log File") | ||
#endif | ||
.withButton ("Cancel"); | ||
|
||
juce::AlertWindow::showAsync ( | ||
alertOptions, | ||
[logFile] (int result) | ||
{ | ||
if (result == 1) | ||
{ | ||
#if JUCE_IOS | ||
juce::SystemClipboard::copyTextToClipboard (logFile.loadFileAsString()); | ||
#else | ||
logFile.startAsProcess(); | ||
#endif | ||
} | ||
}); | ||
#else | ||
jassertfalse; // Implement your own! | ||
#endif | ||
} | ||
|
||
constexpr std::string_view crashString = "Plugin crashing!!!"; | ||
constexpr std::string_view crashExaminedString = "The crash in this log file is now being examined!"; | ||
|
||
void checkLogFilesForCrashes (const LogFileHelpers::FileArray& logFiles, | ||
const CrashLogAnalysisCallback& callback) | ||
{ | ||
for (auto& logFile : logFiles) | ||
{ | ||
const auto& logString = logFile.loadFileAsString(); | ||
|
||
if (! logString.contains (toString (crashString))) | ||
continue; | ||
|
||
if (logString.contains (toString (crashExaminedString))) | ||
continue; | ||
|
||
callback (logFile); | ||
logFile.appendText (toString (crashExaminedString)); | ||
} | ||
} | ||
|
||
void signalHandler (void*) // NOSONAR (void* is needed here) | ||
{ | ||
juce::Logger::writeToLog ("Interrupt signal received!"); | ||
juce::Logger::writeToLog ("Stack Trace:"); | ||
juce::Logger::writeToLog (juce::SystemStats::getStackBacktrace()); | ||
|
||
LogFileHelpers::shutdownLogger (1); | ||
} | ||
} // namespace chowdsp::CrashLogHelpers |
18 changes: 18 additions & 0 deletions
18
modules/common/chowdsp_logging/Loggers/chowdsp_CrashLogHelpers.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
namespace chowdsp | ||
{ | ||
#ifndef DOXYGEN | ||
namespace CrashLogHelpers | ||
{ | ||
using CrashLogAnalysisCallback = std::function<void (const juce::File&)>; | ||
|
||
void defaultCrashLogAnalyzer (const juce::File& logFile); | ||
|
||
void checkLogFilesForCrashes (const LogFileHelpers::FileArray& logFiles, | ||
const CrashLogAnalysisCallback& callback); | ||
|
||
void signalHandler (void*); // NOSONAR (void* is needed here) | ||
} // namespace CrashLogHelpers | ||
#endif | ||
} // namespace chowdsp |
69 changes: 69 additions & 0 deletions
69
modules/common/chowdsp_logging/Loggers/chowdsp_LogFileHelpers.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "chowdsp_LogFileHelpers.h" | ||
|
||
#if JUCE_MODULE_AVAILABLE_juce_gui_basics | ||
#include <juce_gui_basics/juce_gui_basics.h> | ||
#endif | ||
|
||
namespace chowdsp | ||
{ | ||
namespace LogFileHelpers | ||
{ | ||
struct LogFileComparator | ||
{ | ||
static int compareElements (juce::File first, juce::File second) // NOLINT(performance-unnecessary-value-param): JUCE sort needs copies for some reason | ||
{ | ||
const auto firstTime = first.getLastModificationTime().toMilliseconds(); | ||
const auto secondTime = second.getLastModificationTime().toMilliseconds(); | ||
|
||
return firstTime < secondTime; | ||
} | ||
}; | ||
|
||
FileArray getLogFilesSorted (const LogFileParams& params) | ||
{ | ||
const juce::String logFileWildcard = "*" + params.logFileExtension; | ||
FileArray logFiles; | ||
|
||
auto logFilesDir = juce::FileLogger::getSystemLogFileFolder().getChildFile (params.logFileSubDir); | ||
if (! logFilesDir.isDirectory()) | ||
return logFiles; | ||
|
||
const auto numLogFiles = logFilesDir.getNumberOfChildFiles (juce::File::findFiles, logFileWildcard); | ||
logFiles.reserve (numLogFiles); | ||
|
||
for (const auto& entry : juce::RangedDirectoryIterator (logFilesDir, false, logFileWildcard)) | ||
{ | ||
VectorHelpers::insert_sorted (logFiles, | ||
entry.getFile(), | ||
[] (const auto& lhs, const auto& rhs) | ||
{ | ||
const auto lhsTime = lhs.getLastModificationTime().toMilliseconds(); | ||
const auto rhsTime = rhs.getLastModificationTime().toMilliseconds(); | ||
return lhsTime < rhsTime; | ||
}); | ||
} | ||
|
||
return logFiles; | ||
} | ||
|
||
// delete old log files to keep the total number of log files below the max! | ||
void pruneOldLogFiles (FileArray& logFiles, const LogFileParams& params) | ||
{ | ||
if (logFiles.size() <= params.maxNumLogFiles) | ||
return; | ||
|
||
while (logFiles.size() > params.maxNumLogFiles) | ||
{ | ||
const auto& lastFile = logFiles.back(); | ||
lastFile.deleteFile(); | ||
logFiles.erase (logFiles.end() - 1); | ||
} | ||
} | ||
|
||
void shutdownLogger (int signal) | ||
{ | ||
juce::Logger::writeToLog (toString (signal == 0 ? exitString : crashString)); | ||
juce::Logger::setCurrentLogger (nullptr); | ||
} | ||
} // namespace LogFileHelpers | ||
} // namespace chowdsp |
33 changes: 33 additions & 0 deletions
33
modules/common/chowdsp_logging/Loggers/chowdsp_LogFileHelpers.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <utility> | ||
|
||
namespace chowdsp | ||
{ | ||
/** Parameters for log files */ | ||
struct LogFileParams | ||
{ | ||
juce::String logFileSubDir {}; | ||
juce::String logFileNameRoot {}; | ||
juce::String logFileExtension = ".log"; | ||
size_t maxNumLogFiles = 50; | ||
}; | ||
|
||
#ifndef DOXYGEN | ||
namespace LogFileHelpers | ||
{ | ||
constexpr std::string_view openString = "This log file is currently being written to..."; | ||
constexpr std::string_view exitString = "Exiting gracefully..."; | ||
constexpr std::string_view crashString = "Plugin crashing!!!"; | ||
constexpr std::string_view crashExaminedString = "The crash in this log file is now being examined!"; | ||
|
||
using FileArray = std::vector<juce::File>; | ||
FileArray getLogFilesSorted (const LogFileParams& params); | ||
|
||
// delete old log files to keep the total number of log files below the max! | ||
void pruneOldLogFiles (FileArray& logFiles, const LogFileParams& params); | ||
|
||
void shutdownLogger (int signal = 0); | ||
} // namespace LogFileHelpers | ||
#endif // DOXYGEN | ||
} // namespace chowdsp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "chowdsp_Logger.h" | ||
|
||
namespace chowdsp | ||
{ | ||
Logger::Logger (const juce::String& logFileSubDir, const juce::String& logFileNameRoot) | ||
: Logger (LogFileParams { logFileSubDir, logFileNameRoot }) | ||
{ | ||
} | ||
|
||
static juce::File getSystemLogFileFolder() | ||
{ | ||
#if JUCE_MAC | ||
return juce::File ("~/Library/Logs"); | ||
#else | ||
return juce::File::getSpecialLocation (juce::File::userApplicationDataDirectory); | ||
#endif | ||
} | ||
|
||
Logger::Logger (const LogFileParams& loggerParams) : params (loggerParams) | ||
{ | ||
using namespace LogFileHelpers; | ||
using namespace CrashLogHelpers; | ||
|
||
auto&& pastLogFiles = getLogFilesSorted (params); | ||
pruneOldLogFiles (pastLogFiles, params); | ||
checkLogFilesForCrashes (pastLogFiles, crashLogAnalysisCallback); | ||
|
||
log_file = getSystemLogFileFolder() | ||
.getChildFile (params.logFileSubDir) | ||
.getChildFile (params.logFileNameRoot | ||
+ juce::Time::getCurrentTime() | ||
.formatted ("%Y-%m-%d_%H-%M-%S")) | ||
.withFileExtension (params.logFileExtension) | ||
.getNonexistentSibling(); | ||
log_file.create(); | ||
|
||
file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt> (log_file.getFullPathName().toStdString(), | ||
false); | ||
logger.internal_logger.sinks().push_back (file_sink); | ||
logger.internal_logger.info ("Starting log file: " + log_file.getFullPathName().toStdString()); | ||
|
||
juce::Logger::setCurrentLogger (&logger); | ||
|
||
juce::SystemStats::setApplicationCrashHandler (signalHandler); | ||
} | ||
|
||
Logger::~Logger() | ||
{ | ||
try | ||
{ | ||
logger.internal_logger.flush(); | ||
} | ||
catch ([[maybe_unused]] const spdlog::spdlog_ex& ex) | ||
{ | ||
jassertfalse; | ||
} | ||
LogFileHelpers::shutdownLogger(); | ||
} | ||
} // namespace chowdsp |
Oops, something went wrong.