diff --git a/include/asset.hpp b/include/asset.hpp index ce4162a7..9bd9dedd 100644 --- a/include/asset.hpp +++ b/include/asset.hpp @@ -37,13 +37,12 @@ std::string plugin(plugin::Plugin* plugin, std::string filename = ""); // Set these before calling init() to override the default paths extern std::string systemDir; extern std::string userDir; +// Only defined on Mac +extern std::string bundlePath; -extern std::string logPath; extern std::string pluginsPath; extern std::string settingsPath; extern std::string templatePath; -// Only defined on Mac -extern std::string bundlePath; } // namespace asset diff --git a/include/logger.hpp b/include/logger.hpp index 5727da8b..82bf4b1f 100644 --- a/include/logger.hpp +++ b/include/logger.hpp @@ -23,6 +23,9 @@ namespace rack { namespace logger { +extern std::string path; + + enum Level { DEBUG_LEVEL, INFO_LEVEL, diff --git a/src/asset.cpp b/src/asset.cpp index 42c53bb9..fe97966a 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -123,7 +123,6 @@ void init() { templatePath = system::join(userDir, "template.vcv"); } else { - logPath = system::join(userDir, "log.txt"); pluginsPath = system::join(userDir, "plugins-v" + ABI_VERSION); settingsPath = system::join(userDir, "settings-v" + ABI_VERSION + ".json"); templatePath = system::join(userDir, "template-v" + ABI_VERSION + ".vcv"); @@ -150,10 +149,8 @@ std::string plugin(plugin::Plugin* plugin, std::string filename) { std::string systemDir; std::string userDir; -std::string logPath; std::string pluginsPath; std::string settingsPath; -std::string autosavePath; std::string templatePath; std::string bundlePath; diff --git a/src/logger.cpp b/src/logger.cpp index eac12329..2fd0b058 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -3,6 +3,7 @@ #include #include #include +#include // #include // for dup2 @@ -10,24 +11,26 @@ namespace rack { namespace logger { +std::string path; static FILE* outputFile = NULL; -static double startTime = 0.0; -static std::mutex logMutex; +static std::mutex mutex; void init() { - std::lock_guard lock(logMutex); - startTime = system::getTime(); + assert(!outputFile); + std::lock_guard lock(mutex); + // Don't open a file in development mode. - if (asset::logPath.empty()) { + if (settings::devMode) { outputFile = stderr; - return; } + else { + path = asset::user("log.txt"); - assert(!outputFile); - outputFile = std::fopen(asset::logPath.c_str(), "w"); - if (!outputFile) { - std::fprintf(stderr, "Could not open log at %s\n", asset::logPath.c_str()); + outputFile = std::fopen(path.c_str(), "w"); + if (!outputFile) { + std::fprintf(stderr, "Could not open log at %s\n", path.c_str()); + } } // Redirect stdout and stderr to the file @@ -37,7 +40,7 @@ void init() { } void destroy() { - std::lock_guard lock(logMutex); + std::lock_guard lock(mutex); if (outputFile && outputFile != stderr) { // Print end token so we know if the logger exited cleanly. std::fprintf(outputFile, "END"); @@ -61,15 +64,14 @@ static const int levelColors[] = { }; static void logVa(Level level, const char* filename, int line, const char* func, const char* format, va_list args) { - std::lock_guard lock(logMutex); + std::lock_guard lock(mutex); if (!outputFile) return; double nowTime = system::getTime(); - double duration = nowTime - startTime; if (outputFile == stderr) std::fprintf(outputFile, "\x1B[%dm", levelColors[level]); - std::fprintf(outputFile, "[%.03f %s %s:%d %s] ", duration, levelLabels[level], filename, line, func); + std::fprintf(outputFile, "[%.03f %s %s:%d %s] ", nowTime, levelLabels[level], filename, line, func); if (outputFile == stderr) std::fprintf(outputFile, "\x1B[0m"); std::vfprintf(outputFile, format, args); @@ -97,11 +99,11 @@ static bool fileEndsWith(FILE* file, std::string str) { } bool isTruncated() { - if (asset::logPath.empty()) + if (path.empty()) return false; // Open existing log file - FILE* file = std::fopen(asset::logPath.c_str(), "r"); + FILE* file = std::fopen(path.c_str(), "r"); if (!file) return false; DEFER({std::fclose(file);}); diff --git a/standalone/main.cpp b/standalone/main.cpp index b786225f..d4cae580 100644 --- a/standalone/main.cpp +++ b/standalone/main.cpp @@ -49,7 +49,7 @@ static void fatalSignalHandler(int sig) { // This might fail because we might not be in the main thread. // But oh well, we're crashing anyway. - std::string text = APP_NAME + " has crashed. See " + asset::logPath + " for details."; + std::string text = APP_NAME + " has crashed. See " + logger::path + " for details."; osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, text.c_str()); abort();