Split src/common/* into headers and definitions

This commit is contained in:
Robbert van der Helm
2020-03-06 19:18:27 +01:00
parent 7d0bf3e3c4
commit 54d62c6a66
5 changed files with 104 additions and 80 deletions
+30
View File
@@ -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<char*>(data)));
const Event event{opcode, index, value, option, payload};
write_object(socket, event);
const auto response = read_object<EventResult>(socket);
if (response.data.has_value()) {
char* char_data = static_cast<char*>(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;
}
+11 -33
View File
@@ -24,15 +24,15 @@
#include <bitsery/traits/vector.h>
#include <vestige/aeffect.h>
#ifdef __WINE__
#include "../wine-host/boost-fix.h"
#endif
#include <boost/asio/buffer.hpp>
#include <boost/asio/local/stream_protocol.hpp>
#include <cinttypes>
#include <iostream>
#include <optional>
#ifdef __WINE__
#include "../wine-host/boost-fix.h"
#endif
#include <boost/asio/local/stream_protocol.hpp>
// 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<char*>(data)));
const Event event{opcode, index, value, option, payload};
write_object(socket, event);
const auto response = read_object<EventResult>(socket);
if (response.data.has_value()) {
char* char_data = static_cast<char*>(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.
+54
View File
@@ -0,0 +1,54 @@
#include "logging.h"
#include <fstream>
#include <iostream>
#ifdef __WINE__
#include "../wine-host/boost-fix.h"
#endif
#include <boost/process/environment.hpp>
/**
* 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<Verbosity>(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);
}
}
+5 -47
View File
@@ -16,27 +16,7 @@
#pragma once
#ifdef __WINE__
#include "../wine-host/boost-fix.h"
#endif
#include <boost/process/environment.hpp>
#include <fstream>
#include <iostream>
/**
* 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 <ostream>
/**
* 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<Verbosity>(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