From 54d62c6a66fb7df48878fbed0d2f4755550366eb Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Fri, 6 Mar 2020 19:18:27 +0100 Subject: [PATCH] Split src/common/* into headers and definitions --- meson.build | 4 +++ src/common/communication.cpp | 30 ++++++++++++++++++++ src/common/communication.h | 44 ++++++++--------------------- src/common/logging.cpp | 54 ++++++++++++++++++++++++++++++++++++ src/common/logging.h | 52 ++++------------------------------ 5 files changed, 104 insertions(+), 80 deletions(-) create mode 100644 src/common/communication.cpp create mode 100644 src/common/logging.cpp diff --git a/meson.build b/meson.build index d07f3fca..3107b85f 100644 --- a/meson.build +++ b/meson.build @@ -31,6 +31,8 @@ include_dir = include_directories('src/include') shared_library( 'yabridge', [ + 'src/common/communication.cpp', + 'src/common/logging.cpp', 'src/plugin/host-bridge.cpp', 'src/plugin/plugin.cpp', ], @@ -43,6 +45,8 @@ shared_library( executable( 'yabridge-host', [ + 'src/common/communication.cpp', + 'src/common/logging.cpp', 'src/wine-host/plugin-bridge.cpp', 'src/wine-host/vst-host.cpp', ], diff --git a/src/common/communication.cpp b/src/common/communication.cpp new file mode 100644 index 00000000..f1669e15 --- /dev/null +++ b/src/common/communication.cpp @@ -0,0 +1,30 @@ +#include "communication.h" + +intptr_t send_event(boost::asio::local::stream_protocol::socket& socket, + int32_t opcode, + int32_t index, + intptr_t value, + void* data, + float option) { + auto payload = + data == nullptr + ? std::nullopt + : std::make_optional(std::string(static_cast(data))); + + const Event event{opcode, index, value, option, payload}; + write_object(socket, event); + + const auto response = read_object(socket); + if (response.data.has_value()) { + char* char_data = static_cast(data); + + // For correctness we will copy the entire buffer and add a terminating + // null byte ourselves. In practice `response.data` will only ever + // contain C-style strings, but this would work with any other data + // format that can contain null bytes. + std::copy(response.data->begin(), response.data->end(), char_data); + char_data[response.data->size()] = 0; + } + + return response.return_value; +} diff --git a/src/common/communication.h b/src/common/communication.h index 820be9b8..f5049983 100644 --- a/src/common/communication.h +++ b/src/common/communication.h @@ -24,15 +24,15 @@ #include #include -#ifdef __WINE__ -#include "../wine-host/boost-fix.h" -#endif -#include -#include #include #include #include +#ifdef __WINE__ +#include "../wine-host/boost-fix.h" +#endif +#include + // These are for the serialization done by bitsery /** @@ -305,34 +305,12 @@ inline T read_object(Socket& socket) { * * @relates passthrough_event */ -inline intptr_t send_event(boost::asio::local::stream_protocol::socket& socket, - int32_t opcode, - int32_t index, - intptr_t value, - void* data, - float option) { - auto payload = - data == nullptr - ? std::nullopt - : std::make_optional(std::string(static_cast(data))); - - const Event event{opcode, index, value, option, payload}; - write_object(socket, event); - - const auto response = read_object(socket); - if (response.data.has_value()) { - char* char_data = static_cast(data); - - // For correctness we will copy the entire buffer and add a terminating - // null byte ourselves. In practice `response.data` will only ever - // contain C-style strings, but this would work with any other data - // format that can contain null bytes. - std::copy(response.data->begin(), response.data->end(), char_data); - char_data[response.data->size()] = 0; - } - - return response.return_value; -} +intptr_t send_event(boost::asio::local::stream_protocol::socket& socket, + int32_t opcode, + int32_t index, + intptr_t value, + void* data, + float option); /** * Receive an event from a socket and pass it through to some callback function. diff --git a/src/common/logging.cpp b/src/common/logging.cpp new file mode 100644 index 00000000..c1d4122b --- /dev/null +++ b/src/common/logging.cpp @@ -0,0 +1,54 @@ +#include "logging.h" + +#include +#include + +#ifdef __WINE__ +#include "../wine-host/boost-fix.h" +#endif +#include + +/** + * The environment variable indicating whether to log to a file. Will log to + * STDERR if not specified. + */ +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. + * + * @see Logger::Verbosity + */ +constexpr char logging_verbosity_environment_variable[] = + "YABRIDGE_DEBUG_VERBOSITY"; + +Logger::Logger(std::ostream&& stream, + Verbosity verbosity_level, + std::string prefix) + : stream(stream), verbosity(verbosity_level), prefix(prefix) {} + +Logger Logger::create_from_environment(std::string prefix) { + auto env = boost::this_process::environment(); + std::string file_path = env.get(logging_file_environment_variable); + std::string verbosity = env.get(logging_verbosity_environment_variable); + + // Default to `Verbosity::basic` if the environment variable has not + // been set or if it is not an integer. + Verbosity verbosity_level; + try { + verbosity_level = static_cast(std::stoi(verbosity)); + } catch (const std::invalid_argument&) { + verbosity_level = Verbosity::basic; + } + + // 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); + } else { + return Logger(std::move(std::cerr), verbosity_level, prefix); + } +} diff --git a/src/common/logging.h b/src/common/logging.h index fd3e9091..00065512 100644 --- a/src/common/logging.h +++ b/src/common/logging.h @@ -16,27 +16,7 @@ #pragma once -#ifdef __WINE__ -#include "../wine-host/boost-fix.h" -#endif -#include -#include -#include - -/** - * The environment variable indicating whether to log to a file. Will log to - * STDERR if not specified. - */ -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. - * - * @see Logger::Verbosity - */ -constexpr char logging_verbosity_environment_variable[] = - "YABRIDGE_DEBUG_VERBOSITY"; +#include /** * Super basic logging facility meant for debugging malfunctioning VST @@ -85,38 +65,16 @@ class Logger { */ Logger(std::ostream&& stream, Verbosity verbosity_level, - std::string prefix = "") - : stream(stream), verbosity(verbosity_level), prefix(prefix){}; + std::string prefix = ""); /** - * Create a logger instance based on the set environment variables. + * Create a logger instance based on the set environment variables. See the + * constants in `logging.cpp` for more information. * * @param prefix A message to prepend for every log message, useful to * differentiate between the Wine process and the Linus VST plugin. */ - static Logger create_from_environment(std::string prefix = "") { - auto env = boost::this_process::environment(); - std::string file_path = env.get(logging_file_environment_variable); - std::string verbosity = env.get(logging_verbosity_environment_variable); - - // Default to `Verbosity::basic` if the environment variable has not - // been set or if it is not an integer. - Verbosity verbosity_level; - try { - verbosity_level = static_cast(std::stoi(verbosity)); - } catch (const std::invalid_argument&) { - verbosity_level = Verbosity::basic; - } - - // 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); - } else { - return Logger(std::move(std::cerr), verbosity_level, prefix); - } - } + static Logger create_from_environment(std::string prefix = ""); // TODO: Add dedicated logging functions for events and the Wine process's // STDOUT and STDERR