diff --git a/logger.cpp b/logger.cpp new file mode 100644 index 0000000..3254a8a --- /dev/null +++ b/logger.cpp @@ -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) << ": "; +} diff --git a/logger.hpp b/logger.hpp new file mode 100644 index 0000000..3e4a4b4 --- /dev/null +++ b/logger.hpp @@ -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 + +namespace sc { + + enum class loglevel { + undefined, + debug, + info, + warning, + error, + critical + }; + + constexpr bool operator>=(loglevel lhs, loglevel rhs) { + return static_cast(lhs) >= static_cast(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 + 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_