add LeveledSink to log using shift operator without braces
This commit is contained in:
parent
a22d89f3f6
commit
5810f83a0a
18
Log.cpp
18
Log.cpp
|
@ -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
64
Log.h
|
@ -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);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue