added first working version of logger class

This commit is contained in:
Bob Polis 2020-04-29 15:27:14 +02:00
parent c16e2b212c
commit 50b9fd3af7
2 changed files with 100 additions and 0 deletions

29
logger.cpp Normal file
View File

@ -0,0 +1,29 @@
//
// logger.cpp
// libsclogging
//
// Created by Bob Polis at 2020-04-29
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#include "logger.hpp"
using namespace sc;
logger::logger(loglevel level) : _level(level) {
}
void logger::msg(loglevel level) {
if (level >= _level) {
if (_topcall) {
write_prefix();
} else {
_topcall = true;
}
std::cerr << '\n';
}
}
void logger::write_prefix() {
std::cerr << loglevel_desc(_level) << ": ";
}

71
logger.hpp Normal file
View File

@ -0,0 +1,71 @@
//
// logger.hpp
// libsclogging
//
// Created by Bob Polis at 2020-04-29
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#ifndef _logger_H_
#define _logger_H_
#include <iostream>
namespace sc {
enum class loglevel {
undefined,
debug,
info,
warning,
error,
critical
};
constexpr bool operator>=(loglevel lhs, loglevel rhs) {
return static_cast<int>(lhs) >= static_cast<int>(rhs);
}
constexpr const char* loglevel_desc(loglevel lvl) {
switch (lvl) {
case loglevel::undefined: return "UNDEFINED"; break;
case loglevel::debug: return "DEBUG"; break;
case loglevel::info: return "INFO"; break;
case loglevel::warning: return "WARNING"; break;
case loglevel::error: return "ERROR"; break;
case loglevel::critical: return "CRITICAL"; break;
}
return "";
}
class logger {
public:
logger(loglevel level);
void msg(loglevel level);
template<typename T, typename... Args>
void msg(loglevel level, T value, Args... args) {
if (level >= _level) {
if (_topcall) {
_topcall = false;
write_prefix();
}
std::cerr << value;
msg(level, args...);
}
}
loglevel level() const { return _level; }
void level(loglevel level) { _level = level; }
private:
void write_prefix();
loglevel _level {loglevel::info};
bool _topcall {true};
};
}
#endif // _logger_H_