Add a +editor flag to YABRIDGE_DEBUG_LEVEL

This commit is contained in:
Robbert van der Helm
2021-07-20 02:48:12 +02:00
parent 9fcf91dc72
commit a1cb3b614b
4 changed files with 80 additions and 34 deletions
+42 -30
View File
@@ -41,21 +41,39 @@ constexpr char logging_file_environment_variable[] = "YABRIDGE_DEBUG_FILE";
constexpr char logging_verbosity_environment_variable[] =
"YABRIDGE_DEBUG_LEVEL";
/**
* The `YABRIDGE_DEBUG_LEVEL` flag for enabling editor tracing.
*/
constexpr char editor_tracing_flag[] = "+editor";
Logger::Logger(std::shared_ptr<std::ostream> stream,
Verbosity verbosity_level,
bool editor_tracing,
std::string prefix,
bool prefix_timestamp)
: verbosity(verbosity_level),
editor_tracing(editor_tracing),
stream(stream),
prefix(prefix),
prefix_timestamp(prefix_timestamp) {}
Logger Logger::create_from_environment(std::string prefix) {
Logger Logger::create_from_environment(std::string prefix,
std::shared_ptr<std::ostream> stream) {
bp::environment env = boost::this_process::environment();
std::string file_path = env[logging_file_environment_variable].to_string();
const std::string file_path =
env[logging_file_environment_variable].to_string();
std::string verbosity =
env[logging_verbosity_environment_variable].to_string();
// Editor debug tracing is an optional flag that can be added to any debug
// level (and technically it will also work fine if it's the only option,
// but you're not supposed to do that ;))
const bool editor_tracing = verbosity.ends_with(editor_tracing_flag);
if (editor_tracing) {
verbosity =
verbosity.substr(0, verbosity.size() - strlen(editor_tracing_flag));
}
// Default to `Verbosity::basic` if the environment variable has not
// been set or if it is not an integer.
Verbosity verbosity_level;
@@ -65,41 +83,35 @@ Logger Logger::create_from_environment(std::string prefix) {
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
auto log_file = std::make_shared<std::ofstream>(
file_path, std::fstream::out | std::fstream::app);
if (log_file->is_open()) {
return Logger(log_file, verbosity_level, prefix);
} else {
// For STDERR we sadly can't just use `std::cerr`. In the group process
// we need to capture all output generated by the process itself, and
// the only way to do this is by reopening the STDERR and STDOUT streams
// to a pipe. Luckily `/dev/stderr` stays unaffected, so we can still
// write there without causing infinite loops.
return Logger(std::make_shared<std::ofstream>(
"/dev/stderr", std::fstream::out | std::fstream::app),
verbosity_level, prefix);
if (!stream) {
// If `file` points to a valid location then use create/truncate the
// file and write all of the logs there, otherwise use STDERR
const auto log_file = std::make_shared<std::ofstream>(
file_path, std::fstream::out | std::fstream::app);
if (log_file->is_open()) {
stream = log_file;
return Logger(log_file, verbosity_level, editor_tracing, prefix);
} else {
// For STDERR we sadly can't just use `std::cerr`. In the group
// process we need to capture all output generated by the process
// itself, and the only way to do this is by reopening the STDERR
// and STDOUT streams to a pipe. Luckily `/dev/stderr` stays
// unaffected, so we can still write there without causing infinite
// loops.
stream = std::make_shared<std::ofstream>(
"/dev/stderr", std::fstream::out | std::fstream::app);
}
}
return Logger(stream, verbosity_level, editor_tracing, prefix);
}
Logger Logger::create_wine_stderr() {
bp::environment env = boost::this_process::environment();
std::string verbosity =
env[logging_verbosity_environment_variable].to_string();
Verbosity verbosity_level;
try {
verbosity_level = static_cast<Verbosity>(std::stoi(verbosity));
} catch (const std::invalid_argument&) {
verbosity_level = Verbosity::basic;
}
// We're logging directly to `std::cerr` instead of to `/dev/stderr` because
// we want the STDERR redirection from the group host processes to still
// function here
return Logger(std::shared_ptr<std::ostream>(&std::cerr, [](auto*) {}),
verbosity_level, "", false);
return create_from_environment(
"", std::shared_ptr<std::ostream>(&std::cerr, [](auto*) {}));
}
void Logger::log(const std::string& message) {