libsclogging/logger.hpp

72 lines
1.7 KiB
C++
Raw Normal View History

//
// 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_