From 63185a9609a4ca221552c4f94e0128f896476c10 Mon Sep 17 00:00:00 2001 From: Tommy Date: Mon, 18 Jul 2022 14:00:26 +1200 Subject: [PATCH] log: Automatically add newlines to log entries I also added a STREAMING_LOG logger, which is equivalent to PLAIN_LOG except that it does not add the newline at the end. Other loggers reprint the timestamp every time, so eliding the newline was never useful for them anyway. --- src/log.cpp | 16 +++++++++++++++- src/log.hpp | 7 +++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/log.cpp b/src/log.cpp index 723f0605aae..0250b76d9b1 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -227,7 +227,13 @@ std::string sanitize_log(const std::string& logstr) return str; } -log_in_progress logger::operator()(const log_domain& domain, bool show_names, bool do_indent, bool show_timestamps, bool break_strict) const +log_in_progress logger::operator() ( + const log_domain& domain, + bool show_names, + bool do_indent, + bool show_timestamps, + bool break_strict, + bool auto_newline) const { if (severity_ > domain.domain_->second) { return null_ostream; @@ -246,6 +252,7 @@ log_in_progress logger::operator()(const log_domain& domain, bool show_names, bo stream | formatter() << "Error (strict mode, strict_level = " << strict_level_ << "): wesnoth reported on channel " << name_ << " " << domain.domain_->first << std::endl; strict_threw_ = true; } + stream.set_auto_newline(auto_newline); return stream; } } @@ -267,6 +274,9 @@ void log_in_progress::operator|(formatter&& message) } } stream_ << prefix_ << sanitize_log(message.str()); + if(auto_newline_) { + stream_ << std::endl; + } } void log_in_progress::set_indent(int level) { @@ -281,6 +291,10 @@ void log_in_progress::set_prefix(const std::string& prefix) { prefix_ = prefix; } +void log_in_progress::set_auto_newline(bool auto_newline) { + auto_newline_ = auto_newline; +} + void scope_logger::do_log_entry(const std::string& str) noexcept { str_ = str; diff --git a/src/log.hpp b/src/log.hpp index 176acfa33e6..8332749808f 100644 --- a/src/log.hpp +++ b/src/log.hpp @@ -130,12 +130,14 @@ class log_in_progress { int indent_ = 0; bool timestamp_ = false; std::string prefix_; + bool auto_newline_ = true; public: log_in_progress(std::ostream& stream); void operator|(formatter&& message); void set_indent(int level); void enable_timestamp(); void set_prefix(const std::string& prefix); + void set_auto_newline(bool enabled); }; class logger { @@ -144,7 +146,7 @@ class logger { public: logger(char const *name, int severity): name_(name), severity_(severity) {} log_in_progress operator()(const log_domain& domain, - bool show_names = true, bool do_indent = false, bool show_timestamps = true, bool break_strict = true) const; + bool show_names = true, bool do_indent = false, bool show_timestamps = true, bool break_strict = true, bool auto_newline = true) const; bool dont_log(const log_domain& domain) const { @@ -234,4 +236,5 @@ std::stringstream& log_to_chat(); // always log (since it's at the error level) to the general log stream // outputting the log domain and timestamp is disabled // meant as a replacement to using cerr/cout, but that goes through the same logging infrastructure as everything else -#define PLAIN_LOG lg::err()(lg::general(), false, false, false, false) | formatter() +#define PLAIN_LOG lg::err()(lg::general(), false, false, false, false, true) | formatter() +#define STREAMING_LOG lg::err()(lg::general(), false, false, false, false, false) | formatter()