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" #include "Log.h"
/* /*
class Log * class Log
*/ */
// static member // static members
std::vector<Log::Output*> Log::outputs; 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() { void Log::init() {
// add default console logger // add default console logger
if (outputs.empty()) if (outputs.empty())
@ -138,4 +146,6 @@ std::ostream* Log::FileOutput::getOs(Log::Level lvl) {
if (lvl_min <= lvl && lvl <= lvl_max) if (lvl_min <= lvl && lvl <= lvl_max)
return &ofs; return &ofs;
return nullptr; return nullptr;
} }
Log::LeveledSink::LeveledSink(Log::Level level) : level(level) {}

64
Log.h
View File

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