From e5348fc5f797ce2af1f568a418e54856cd376711 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sat, 7 Mar 2020 18:58:23 +0100 Subject: [PATCH] Fix logging to files --- src/common/logging.cpp | 20 +++++++++----------- src/common/logging.h | 5 +++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/common/logging.cpp b/src/common/logging.cpp index 6ea3b98f..4514bdf8 100644 --- a/src/common/logging.cpp +++ b/src/common/logging.cpp @@ -18,16 +18,14 @@ constexpr char logging_file_environment_variable[] = "YABRIDGE_DEBUG_FILE"; /** - * The verbosity of the logging, defaults to `Logger::Verbosity::events` if - * `logging_file_environment_variable` has been set and - * `Logger::Verbosity::basic` otherwise. + * The verbosity of the logging, defaults to `Logger::Verbosity::basic`. * * @see Logger::Verbosity */ constexpr char logging_verbosity_environment_variable[] = "YABRIDGE_DEBUG_VERBOSITY"; -Logger::Logger(std::ostream&& stream, +Logger::Logger(std::shared_ptr stream, Verbosity verbosity_level, std::string prefix) : stream(stream), verbosity(verbosity_level), prefix(prefix) {} @@ -49,11 +47,13 @@ Logger Logger::create_from_environment(std::string prefix) { // If `file` points to a valid location then use create/truncate the // file and write all of the logs there, otherwise use STDERR - std::ofstream log_file(file_path, std::fstream::out); - if (log_file.is_open()) { - return Logger(std::move(log_file), verbosity_level, prefix); + auto log_file = std::make_shared( + file_path, std::fstream::out | std::fstream::app); + if (log_file->is_open()) { + return Logger(log_file, verbosity_level, prefix); } else { - return Logger(std::move(std::cerr), verbosity_level, prefix); + return Logger(std::shared_ptr(&std::cerr, [](auto) {}), + verbosity_level, prefix); } } @@ -77,7 +77,5 @@ void Logger::log(const std::string& message) { // stream to prevent two messages from being put on the same row formatted_message << std::endl; - // No flush. Should technically be necessary, but it decreases throughput by - // a lot and it seems to work fine without. - stream << formatted_message.str(); + *stream << formatted_message.str() << std::flush; } diff --git a/src/common/logging.h b/src/common/logging.h index b2534df4..ac1e6d7e 100644 --- a/src/common/logging.h +++ b/src/common/logging.h @@ -16,6 +16,7 @@ #pragma once +#include #include /** @@ -63,7 +64,7 @@ class Logger { * @param prefix An optional prefix for the logger. Useful for differentiate * messages coming from the Wine VST host. */ - Logger(std::ostream&& stream, + Logger(std::shared_ptr stream, Verbosity verbosity_level, std::string prefix = ""); @@ -92,7 +93,7 @@ class Logger { * The output stream to write the log messages to. Typically either STDERR * or a file stream. */ - std::ostream& stream; + std::shared_ptr stream; /** * The verbosity level of this logger instance. Based on this certain * messages may or may not be shown.