modern cpp: std::array, virtual -> override; etc

This commit is contained in:
Oliver 2022-10-20 02:20:53 +02:00
parent fbc252701a
commit f47fe4bae0
Signed by untrusted user: okaestne
GPG Key ID: 06A81B143EA9588F
1 changed files with 15 additions and 12 deletions

27
Log.cpp
View File

@ -1,5 +1,6 @@
#include "Log.h"
#include <array>
#include <chrono> // date/time
#include <fstream> // ofstream (logging to file)
#include <iostream> // 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<const char*, 8> 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<Entry::MetaFunction> 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<char, 24> 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<const char*, 8> LevelTag = {
"", "[FATAL]", "[ERROR]", "[WARN ]", "[NOTE ]", "[INFO ]", "[DEBUG]", "[TRACE]"
};
return os << LevelTag[static_cast<int>(e.getLevel())];