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.
This commit is contained in:
Tommy 2022-07-18 14:00:26 +12:00
parent 78764ccd58
commit 63185a9609
2 changed files with 20 additions and 3 deletions

View File

@ -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;

View File

@ -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()