From 1a34a80c210cb42d2009d43599a85e0919a2965d Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Fri, 23 Jul 2021 15:41:38 +0200 Subject: [PATCH] Add a special exception logger We'll need this to make sure that we can redirect caught exceptions printed in `src/common/` to the correct file if `YABRIDGE_DEBUG_FILE` is set. --- src/common/logging/common.cpp | 8 ++++++++ src/common/logging/common.h | 11 +++++++++++ src/plugin/vst2-plugin.cpp | 2 +- src/plugin/vst3-plugin.cpp | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/common/logging/common.cpp b/src/common/logging/common.cpp index b30471f8..e874c2e8 100644 --- a/src/common/logging/common.cpp +++ b/src/common/logging/common.cpp @@ -116,6 +116,14 @@ Logger Logger::create_wine_stderr() { "", std::shared_ptr(&std::cerr, [](auto*) {}), false); } +Logger Logger::create_exception_logger() { +#ifdef __WINE__ + return Logger::create_wine_stderr(); +#else + return Logger::create_from_environment("[error] "); +#endif +} + void Logger::log(const std::string& message) { std::ostringstream formatted_message; diff --git a/src/common/logging/common.h b/src/common/logging/common.h index 24054f34..72f00362 100644 --- a/src/common/logging/common.h +++ b/src/common/logging/common.h @@ -137,6 +137,17 @@ class Logger { */ static Logger create_wine_stderr(); + /** + * Create a special logger instance for printing caught exceptions. This + * simply calls `Logger::create_from_environment()` on the plugin side, and + * `Logger::create_wine_stderr()` on the Wine side. Printing directly to + * STDERR on the Wine side is fine, but on the plugin side that means that + * we cannot redirect the output with `YABRIDGE_DEBUG_FILE`. So this should + * also be used instead of writing to `std::cerr` when catching exceptions + * in `src/common/`. + */ + static Logger create_exception_logger(); + /** * Write a message to the log, prefixing it with a timestamp and this * logger's prefix string. diff --git a/src/plugin/vst2-plugin.cpp b/src/plugin/vst2-plugin.cpp index 0eda55f3..f789a4c4 100644 --- a/src/plugin/vst2-plugin.cpp +++ b/src/plugin/vst2-plugin.cpp @@ -50,7 +50,7 @@ extern "C" VST_EXPORT AEffect* VSTPluginMain( return &bridge->plugin; } catch (const std::exception& error) { - Logger logger = Logger::create_from_environment(); + Logger logger = Logger::create_exception_logger(); logger.log("Error during initialization:"); logger.log(error.what()); diff --git a/src/plugin/vst3-plugin.cpp b/src/plugin/vst3-plugin.cpp index f43d1761..e8fffda8 100644 --- a/src/plugin/vst3-plugin.cpp +++ b/src/plugin/vst3-plugin.cpp @@ -53,7 +53,7 @@ bool InitModule() { return true; } catch (const std::exception& error) { - Logger logger = Logger::create_from_environment(); + Logger logger = Logger::create_exception_logger(); logger.log("Error during initialization:"); logger.log(error.what());