From 0a59d4aad36cb7c49f7032bda0e796e28bf87aca Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Thu, 19 Aug 2021 15:50:40 -0400 Subject: [PATCH] Refactor logger. Require setting logger::logPath before initializing logger. --- adapters/standalone.cpp | 8 +++-- include/logger.hpp | 5 ++- src/logger.cpp | 70 ++++++++++++++++++++++------------------- 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/adapters/standalone.cpp b/adapters/standalone.cpp index af35f2c4..658ff2e7 100644 --- a/adapters/standalone.cpp +++ b/adapters/standalone.cpp @@ -108,7 +108,9 @@ int main(int argc, char* argv[]) { // Initialize environment system::init(); asset::init(); - bool loggerWasTruncated = logger::isTruncated(); + if (!settings::devMode) { + logger::logPath = asset::user("log.txt"); + } logger::init(); random::init(); @@ -203,8 +205,8 @@ int main(int argc, char* argv[]) { #endif // Initialize patch - if (loggerWasTruncated && osdialog_message(OSDIALOG_INFO, OSDIALOG_YES_NO, "Rack crashed during the last session, possibly due to a buggy module in your patch. Clear your patch and start over?")) { - // Do nothing + if (logger::wasTruncated() && osdialog_message(OSDIALOG_INFO, OSDIALOG_YES_NO, "Rack crashed during the last session, possibly due to a buggy module in your patch. Clear your patch and start over?")) { + // Do nothing, which leaves a blank patch } else { APP->patch->launch(patchPath); diff --git a/include/logger.hpp b/include/logger.hpp index 0286ed8c..55492ee4 100644 --- a/include/logger.hpp +++ b/include/logger.hpp @@ -40,10 +40,9 @@ Thread-safe, meaning messages cannot overlap each other in the log. */ __attribute__((format(printf, 5, 6))) void log(Level level, const char* filename, int line, const char* func, const char* format, ...); -/** Returns whether the current log file failed to end properly, due to a possible crash. -Must be called *before* init(). +/** Returns whether the last log file failed to end properly, due to a possible crash. */ -bool isTruncated(); +bool wasTruncated(); } // namespace logger diff --git a/src/logger.cpp b/src/logger.cpp index 84d5734d..5454435c 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -14,6 +14,36 @@ namespace logger { std::string logPath; static FILE* outputFile = NULL; static std::mutex mutex; +static bool truncated = false; + + +static bool fileEndsWith(FILE* file, std::string str) { + // Seek to last `len` characters + size_t len = str.size(); + std::fseek(file, -long(len), SEEK_END); + char actual[len]; + if (std::fread(actual, 1, len, file) != len) + return false; + return std::string(actual, len) == str; +} + +static bool isTruncated() { + if (logPath.empty()) + return false; + + // Open existing log file + FILE* file = std::fopen(logPath.c_str(), "r"); + if (!file) + return false; + DEFER({std::fclose(file);}); + + if (fileEndsWith(file, "END")) + return false; + // legacy <=v1 + if (fileEndsWith(file, "Destroying logger\n")) + return false; + return true; +} void init() { @@ -21,11 +51,11 @@ void init() { std::lock_guard lock(mutex); // Don't open a file in development mode. - if (settings::devMode) { + if (logPath.empty()) { outputFile = stderr; } else { - logPath = asset::user("log.txt"); + truncated = isTruncated(); outputFile = std::fopen(logPath.c_str(), "w"); if (!outputFile) { @@ -53,22 +83,22 @@ static const char* const levelLabels[] = { "debug", "info", "warn", - "fatal" + "fatal", }; static const int levelColors[] = { 35, 34, 33, - 31 + 31, }; static void logVa(Level level, const char* filename, int line, const char* func, const char* format, va_list args) { - std::lock_guard lock(mutex); if (!outputFile) return; - double nowTime = system::getTime(); + std::lock_guard lock(mutex); + if (outputFile == stderr) std::fprintf(outputFile, "\x1B[%dm", levelColors[level]); std::fprintf(outputFile, "[%.03f %s %s:%d %s] ", nowTime, levelLabels[level], filename, line, func); @@ -88,32 +118,8 @@ void log(Level level, const char* filename, int line, const char* func, const ch va_end(args); } -static bool fileEndsWith(FILE* file, std::string str) { - // Seek to last `len` characters - size_t len = str.size(); - std::fseek(file, -long(len), SEEK_END); - char actual[len]; - if (std::fread(actual, 1, len, file) != len) - return false; - return std::string(actual, len) == str; -} - -bool isTruncated() { - if (logPath.empty()) - return false; - - // Open existing log file - FILE* file = std::fopen(logPath.c_str(), "r"); - if (!file) - return false; - DEFER({std::fclose(file);}); - - if (fileEndsWith(file, "END")) - return false; - // legacy <=v1 - if (fileEndsWith(file, "Destroying logger\n")) - return false; - return true; +bool wasTruncated() { + return truncated; }