From f47fe4bae07f603bb36962ba3d9ec7f810c78522 Mon Sep 17 00:00:00 2001 From: okaestne Date: Thu, 20 Oct 2022 02:20:53 +0200 Subject: [PATCH] modern cpp: std::array, virtual -> override; etc --- Log.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/Log.cpp b/Log.cpp index a46fc40..fc41315 100644 --- a/Log.cpp +++ b/Log.cpp @@ -1,5 +1,6 @@ #include "Log.h" +#include #include // date/time #include // ofstream (logging to file) #include // std::ostream, std::cout, std::cin @@ -65,9 +66,11 @@ private: std::ostream* osErr = &std::cerr; bool coloredOutput = false; - virtual void log(Level lvl, std::stringbuf* sbuf) { - // off fatal error warn note info debug trace - static const char* color_codes[] = {"", "1;31;40m", "31m", "33m", "96m", "32m", "0m", "0m"}; + void log(Level lvl, std::stringbuf* sbuf) override { + static constexpr std::array color_codes = { + // off fatal error warn note info debug trace + "", "1;31;40m", "31m", "33m", "96m", "32m", "0m", "0m" + }; //aquire lock #if LOG_USEMUTEX == 1 @@ -88,7 +91,7 @@ private: } } - virtual std::ostream* getOs(Level lvl) { + std::ostream* getOs(Level lvl) override { // out of scope? if (lvl == Level::off || lvl > lvl_max) { return nullptr; @@ -123,7 +126,7 @@ private: std::mutex ostreamLock; //used for both streams #endif - virtual std::ostream* getOs(Level lvl) { + std::ostream* getOs(Level lvl) override { if (lvl_min <= lvl && lvl <= lvl_max) { return &ofs; } @@ -154,7 +157,7 @@ const LeveledSink trace{Level::trace}; */ Entry::Entry(Level lvl) : lvl{lvl} { - for(auto metafunc : entryMetaFunctions) { + for(const auto& metafunc : entryMetaFunctions) { metafunc(ss, *this); } } @@ -168,20 +171,20 @@ std::vector entryMetaFunctions; std::ostream& defaultEntryMetaTime(std::ostream& os, const Entry& e) { (void) e; // unused - using namespace std::chrono; + using std::chrono::system_clock; auto now = system_clock::to_time_t(system_clock::now()); - auto tm = *std::localtime(&now); + auto time = *std::localtime(&now); // MinGW doesn't support the ISO8601 formatting characters like "%F" and "%T" // ref: https://sourceforge.net/p/mingw-w64/bugs/793/ // Therefore, use a more verbose time string - char buf[24]; - std::strftime(buf, sizeof(buf), "[%Y-%m-%d %H:%M:%S]", &tm); - return os << buf; + std::array buf; + (void) std::strftime(buf.data(), buf.size(), "[%Y-%m-%d %H:%M:%S]", &time); + return os << buf.data(); } std::ostream& defaultEntryMetaLevel(std::ostream& os, const Entry& e) { - static const char* LevelTag[] = { + static constexpr std::array LevelTag = { "", "[FATAL]", "[ERROR]", "[WARN ]", "[NOTE ]", "[INFO ]", "[DEBUG]", "[TRACE]" }; return os << LevelTag[static_cast(e.getLevel())];