!refactor(Log:Level): as enum class and renamed members to lowercase

matching modern conventions and fixing Windows compat issue (wingdi.h)
This commit is contained in:
Oliver 2022-09-04 19:58:19 +02:00
parent d403a32766
commit 6ecbc406d7
Signed by untrusted user: okaestne
GPG Key ID: 06A81B143EA9588F
3 changed files with 59 additions and 59 deletions

30
Log.cpp
View File

@ -39,7 +39,7 @@ public:
virtual void setLogLevel(Level lvl) { lvl_max = lvl; } virtual void setLogLevel(Level lvl) { lvl_max = lvl; }
protected: protected:
Level lvl_max = INFO; Level lvl_max = Level::info;
// returns the correct ostream for the given log-level // returns the correct ostream for the given log-level
// or returns nullptr if no ostream is set/enabled for this level // or returns nullptr if no ostream is set/enabled for this level
virtual std::ostream* getOs(Level lvl) = 0; // abstract virtual std::ostream* getOs(Level lvl) = 0; // abstract
@ -66,7 +66,7 @@ private:
bool coloredOutput; bool coloredOutput;
virtual void log(Level lvl, std::stringbuf* sbuf) { virtual void log(Level lvl, std::stringbuf* sbuf) {
// OFF FATAL ERROR WARN NOTE INFO DEBUG TRACE // off fatal error warn note info debug trace
static const char* color_codes[] = {"", "1;31;40m", "31m", "33m", "96m", "32m", "0m", "0m"}; static const char* color_codes[] = {"", "1;31;40m", "31m", "33m", "96m", "32m", "0m", "0m"};
//aquire lock //aquire lock
@ -79,7 +79,7 @@ private:
if (os) { if (os) {
// print colors if enabled // print colors if enabled
if (coloredOutput) if (coloredOutput)
*os << "\x1B[" << color_codes[lvl] << sbuf << "\x1B[0m"; *os << "\x1B[" << color_codes[static_cast<int>(lvl)] << sbuf << "\x1B[0m";
else else
*os << sbuf; *os << sbuf;
@ -89,11 +89,11 @@ private:
virtual std::ostream* getOs(Level lvl) { virtual std::ostream* getOs(Level lvl) {
// out of scope? // out of scope?
if (!lvl || lvl > lvl_max) if (lvl == Level::off || lvl > lvl_max)
return nullptr; return nullptr;
// stderr for FATAL, ERROR, WARN // stderr for fatal, error, warn
if (lvl <= Level::WARN) if (lvl <= Level::warn)
return osErr; return osErr;
else else
return osStd; return osStd;
@ -114,7 +114,7 @@ public:
private: private:
std::string filename; std::string filename;
std::ofstream ofs; std::ofstream ofs;
Level lvl_min = FATAL; Level lvl_min = Level::fatal;
#if LOG_USEMUTEX == 1 #if LOG_USEMUTEX == 1
std::mutex ostreamLock; //used for both streams std::mutex ostreamLock; //used for both streams
@ -137,13 +137,13 @@ void log(Level lvl, std::stringbuf* strb) {
} }
} }
LeveledSink fatal(Level::FATAL); LeveledSink fatal(Level::fatal);
LeveledSink error(Level::ERROR); LeveledSink error(Level::error);
LeveledSink warn(Level::WARN); LeveledSink warn(Level::warn);
LeveledSink note(Level::NOTE); LeveledSink note(Level::note);
LeveledSink info(Level::INFO); LeveledSink info(Level::info);
LeveledSink debug(Level::DEBUG); LeveledSink debug(Level::debug);
LeveledSink trace(Level::TRACE); LeveledSink trace(Level::trace);
/* /*
* class Entry * class Entry
@ -172,7 +172,7 @@ void Entry::addMetadataHeader() {
ss << "[" << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << "]"; ss << "[" << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << "]";
// log level // log level
ss << LevelTag[lvl]; ss << LevelTag[static_cast<int>(lvl)];
} }
void init() { void init() {

2
Log.h
View File

@ -8,7 +8,7 @@
#endif #endif
namespace Log { namespace Log {
enum Level { OFF = 0, FATAL, ERROR, WARN, NOTE, INFO, DEBUG, TRACE }; enum class Level { off = 0, fatal, error, warn, note, info, debug, trace };
// set up the logger with a ConsoleOutput // set up the logger with a ConsoleOutput
void init(); void init();

View File

@ -6,23 +6,23 @@ int main() {
Log::init(); Log::init();
Log::info << "Hello World!"; Log::info << "Hello World!";
Log::addLogfile("test.warn.log", Log::Level::WARN); Log::addLogfile("test.warn.log", Log::Level::warn);
Log::addLogfile("test.info.log", Log::Level::INFO, Log::Level::INFO); Log::addLogfile("test.info.log", Log::Level::info, Log::Level::info);
Log::addLogfile("test.trace.log", Log::Level::DEBUG, Log::Level::TRACE); Log::addLogfile("test.trace.log", Log::Level::debug, Log::Level::trace);
// log single values only // log single values only
std::cout << "=== Log level TRACE ===" << std::endl; std::cout << "=== Log level trace ===" << std::endl;
Log::setConsoleLogLevel(Log::Level::TRACE); Log::setConsoleLogLevel(Log::Level::trace);
Log::trace << "trace msg @Lvl TRACE"; Log::trace << "trace msg @Lvl trace";
Log::debug << "dbg msg @Lvl TRACE"; Log::debug << "dbg msg @Lvl trace";
Log::info << "info msg @Lvl TRACE"; Log::info << "info msg @Lvl trace";
Log::note << "note msg @Lvl TRACE"; Log::note << "note msg @Lvl trace";
Log::warn << "warn msg @Lvl TRACE"; Log::warn << "warn msg @Lvl trace";
Log::error << "error msg @Lvl TRACE"; Log::error << "error msg @Lvl trace";
Log::fatal << "fatal msg @Lvl TRACE"; Log::fatal << "fatal msg @Lvl trace";
std::cout << "=== Log level WARN ===" << std::endl; std::cout << "=== Log level warn ===" << std::endl;
Log::setConsoleLogLevel(Log::Level::WARN); Log::setConsoleLogLevel(Log::Level::warn);
Log::trace << 42; Log::trace << 42;
Log::debug << 42; Log::debug << 42;
Log::info << 42; Log::info << 42;
@ -31,8 +31,8 @@ int main() {
Log::error << 42; Log::error << 42;
Log::fatal << 42; Log::fatal << 42;
std::cout << "=== Log level FATAL ===" << std::endl; std::cout << "=== Log level fatal ===" << std::endl;
Log::setConsoleLogLevel(Log::Level::FATAL); Log::setConsoleLogLevel(Log::Level::fatal);
Log::trace << 3.14159; Log::trace << 3.14159;
Log::debug << 3.14159; Log::debug << 3.14159;
Log::info << 3.14159; Log::info << 3.14159;
@ -43,35 +43,35 @@ int main() {
// log streams + colors // log streams + colors
Log::setColoredOutput(true); Log::setColoredOutput(true);
Log::setConsoleLogLevel(Log::Level::TRACE); Log::setConsoleLogLevel(Log::Level::trace);
std::cout << "=== Log level DEBUG ===" << std::endl; std::cout << "=== Log level debug ===" << std::endl;
Log::trace << "trace msg @Lvl TRACE; " << 42 << "; " << 3.14159; Log::trace << "trace msg @Lvl trace; " << 42 << "; " << 3.14159;
Log::debug << "dbg msg @Lvl TRACE; " << 42 << "; " << 3.14159; Log::debug << "dbg msg @Lvl trace; " << 42 << "; " << 3.14159;
Log::info << "info msg @Lvl TRACE; " << 42 << "; " << 3.14159; Log::info << "info msg @Lvl trace; " << 42 << "; " << 3.14159;
Log::note << "note msg @Lvl TRACE; " << 42 << "; " << 3.14159; Log::note << "note msg @Lvl trace; " << 42 << "; " << 3.14159;
Log::warn << "warn msg @Lvl TRACE; " << 42 << "; " << 3.14159; Log::warn << "warn msg @Lvl trace; " << 42 << "; " << 3.14159;
Log::error << "error msg @Lvl TRACE; " << 42 << "; " << 3.14159; Log::error << "error msg @Lvl trace; " << 42 << "; " << 3.14159;
Log::fatal << "fatal msg @Lvl TRACE; " << 42 << "; " << 3.14159; Log::fatal << "fatal msg @Lvl trace; " << 42 << "; " << 3.14159;
std::cout << "=== Log level WARN ===" << std::endl; std::cout << "=== Log level warn ===" << std::endl;
Log::setConsoleLogLevel(Log::Level::WARN); Log::setConsoleLogLevel(Log::Level::warn);
Log::trace << "trace msg @Lvl WARN; " << 42 << "; " << 3.14159; Log::trace << "trace msg @Lvl warn; " << 42 << "; " << 3.14159;
Log::debug << "dbg msg @Lvl WARN; " << 42 << "; " << 3.14159; Log::debug << "dbg msg @Lvl warn; " << 42 << "; " << 3.14159;
Log::info << "info msg @Lvl WARN; " << 42 << "; " << 3.14159; Log::info << "info msg @Lvl warn; " << 42 << "; " << 3.14159;
Log::note << "note msg @Lvl WARN; " << 42 << "; " << 3.14159; Log::note << "note msg @Lvl warn; " << 42 << "; " << 3.14159;
Log::warn << "warn msg @Lvl WARN; " << 42 << "; " << 3.14159; Log::warn << "warn msg @Lvl warn; " << 42 << "; " << 3.14159;
Log::error << "error msg @Lvl WARN; " << 42 << "; " << 3.14159; Log::error << "error msg @Lvl warn; " << 42 << "; " << 3.14159;
Log::fatal << "fatal msg @Lvl WARN; " << 42 << "; " << 3.14159; Log::fatal << "fatal msg @Lvl warn; " << 42 << "; " << 3.14159;
std::cout << "=== Log level FATAL ===" << std::endl; std::cout << "=== Log level fatal ===" << std::endl;
Log::setConsoleLogLevel(Log::Level::FATAL); Log::setConsoleLogLevel(Log::Level::fatal);
Log::trace << "trace msg @Lvl FATAL; " << 42 << "; " << 3.14159; Log::trace << "trace msg @Lvl fatal; " << 42 << "; " << 3.14159;
Log::debug << "dbg msg @Lvl FATAL; " << 42 << "; " << 3.14159; Log::debug << "dbg msg @Lvl fatal; " << 42 << "; " << 3.14159;
Log::info << "info msg @Lvl FATAL; " << 42 << "; " << 3.14159; Log::info << "info msg @Lvl fatal; " << 42 << "; " << 3.14159;
Log::note << "note msg @Lvl FATAL; " << 42 << "; " << 3.14159; Log::note << "note msg @Lvl fatal; " << 42 << "; " << 3.14159;
Log::warn << "warn msg @Lvl FATAL; " << 42 << "; " << 3.14159; Log::warn << "warn msg @Lvl fatal; " << 42 << "; " << 3.14159;
Log::error << "error msg @Lvl FATAL; " << 42 << "; " << 3.14159; Log::error << "error msg @Lvl fatal; " << 42 << "; " << 3.14159;
Log::fatal << "fatal msg @Lvl FATAL; " << 42 << "; " << 3.14159; Log::fatal << "fatal msg @Lvl fatal; " << 42 << "; " << 3.14159;
Log::stop(); Log::stop();