add LeveledSink to log using shift operator without braces

This commit is contained in:
Oliver 2020-09-23 18:52:49 +02:00
parent a22d89f3f6
commit 5810f83a0a
Signed by untrusted user: okaestne
GPG Key ID: 06A81B143EA9588F
2 changed files with 35 additions and 47 deletions

18
Log.cpp
View File

@ -1,12 +1,20 @@
#include "Log.h"
/*
class Log
*/
* class Log
*/
// static member
// static members
std::vector<Log::Output*> Log::outputs;
Log::LeveledSink Log::fatal(Log::Level::FATAL);
Log::LeveledSink Log::error(Log::Level::ERROR);
Log::LeveledSink Log::warn(Log::Level::WARN);
Log::LeveledSink Log::note(Log::Level::NOTE);
Log::LeveledSink Log::info(Log::Level::INFO);
Log::LeveledSink Log::debug(Log::Level::DEBUG);
Log::LeveledSink Log::trace(Log::Level::TRACE);
void Log::init() {
// add default console logger
if (outputs.empty())
@ -138,4 +146,6 @@ std::ostream* Log::FileOutput::getOs(Log::Level lvl) {
if (lvl_min <= lvl && lvl <= lvl_max)
return &ofs;
return nullptr;
}
}
Log::LeveledSink::LeveledSink(Log::Level level) : level(level) {}

64
Log.h
View File

@ -95,51 +95,29 @@ public:
}
};
template <typename T>
static void fatal(const T& msg) {
Entry(FATAL) << msg;
}
static Entry fatal() { return Entry(FATAL); }
class LeveledSink {
private:
Log::Level level;
public:
LeveledSink(Log::Level level);
template <typename T>
static void error(const T& msg) {
Entry(ERROR) << msg;
}
static Entry error() { return Entry(ERROR); }
template <typename T>
Log::Entry operator<<(const T& msg) {
Log::Entry entry(level);
entry << msg;
return entry;
}
};
template <typename T>
static void warn(const T& msg) {
Entry(WARN) << msg;
}
static Entry warn() { return Entry(WARN); }
// LeveledSinks
static LeveledSink fatal;
static LeveledSink error;
static LeveledSink warn;
static LeveledSink note;
static LeveledSink info;
static LeveledSink debug;
static LeveledSink trace;
template <typename T>
static void note(const T& msg) {
Entry(NOTE) << msg;
}
static Entry note() { return Entry(NOTE); }
template <typename T>
static void info(const T& msg) {
Entry(INFO) << msg;
}
static Entry info() { return Entry(INFO); }
template <typename T>
static void debug(const T& msg) {
Entry(DEBUG) << msg;
}
static Entry debug() { return Entry(DEBUG); }
template <typename T>
static void trace(const T& msg) {
Entry(TRACE) << msg;
}
static Entry trace() { return Entry(TRACE); }
template <typename T>
static void log(const T& msg, Level lvl) {
Entry(lvl) << msg;
}
private:
static void log(Level lvl, std::stringbuf* strb);
};