diff --git a/engine/NBCore/Logger.hpp b/engine/NBCore/Logger.hpp index 95ae814..17f4939 100644 --- a/engine/NBCore/Logger.hpp +++ b/engine/NBCore/Logger.hpp @@ -30,99 +30,119 @@ struct LogEvent{ typedef std::string (*LogProcessFunction)(const LoggerTimePoint&, const std::string&); typedef std::unordered_map LogProcessFunctionMap; -template +template class Logger { public: - Logger(StreamType& stream) : _ostream(&stream) {} + using Type = LoggerName; - bool isRunning() const; - unsigned int count() const; + Logger(std::ostream& stream) : _ostream(&stream) {} + + bool isRunning() const { + return _running && bool(_runningThread); + } + + unsigned int count() const { + return _queue.size(); + } + + bool run() { + auto this_type = static_cast(this); + if (!isRunning()) { + _runningThread = std::make_shared([&]{ + while(this_type->isRunning()) { + this_type->flush(); + } + }); + } + return isRunning(); + } + + bool stop() { + if (isRunning()) { + _running = false; + _runningThread->join(); + _runningThread = nullptr; + flush(); + } + return isRunning(); + } + + LogEvent pop() { + std::shared_ptr event; + _queue.pop(event); + static_cast(this)->process(*event); + return *event; + } - bool run(); - bool stop(); - LogEvent pop(); void flush() { while(_queue.size()) { pop(); } } + void deleteAll() { _queue.empty(); } - void msg(const std::string& _msg, uint8_t lvl=0) { + + template + void log(const T&, const uint8_t); + + void log(const std::string& msg, const uint8_t& lvl) { _queue.push(LogEvent{ std::chrono::system_clock::now(), lvl, - _msg, + msg, std::this_thread::get_id(), - get_pid() + get_pid(), }); } - void warn(const std::string& _msg, uint8_t lvl=1) { - msg(_msg, lvl); + + void log(const std::exception& err, const uint8_t& lvl) { + _queue.push(LogEvent{ + std::chrono::system_clock::now(), + lvl, + err.what(), + std::this_thread::get_id(), + get_pid(), + }); } - void warn(const std::exception& _err, uint8_t lvl=1) { - msg(_err.what(), lvl); + + void msg(const std::string& msg, const uint8_t& lvl=0x00) { + static_cast(this)->log(msg, lvl); } - void error(const std::string& _msg, uint8_t lvl=0xFF) { - msg(_msg, lvl); + + void warn(const std::string& msg, const uint8_t& lvl=0x01) { + static_cast(this)->log(msg, lvl); } - void error(const std::exception& _err, uint8_t lvl=0xFF) { - msg(_err.what(), lvl); + void warn(const std::exception& err, const uint8_t lvl=0x01) { + static_cast(this)->log(err, lvl); + } + + void error(const std::string& msg, const uint8_t lvl=0xFF) { + static_cast(this)->log(msg, lvl); + } + void error(const std::exception& err, const uint8_t lvl=0xFF) { + static_cast(this)->log(err, lvl); } protected: - virtual void process(const LogEvent& event) { - uint8_t lvl = event.lvl; - *_ostream << "\n"; - } + void process(const LogEvent&); ThreadsafeQueue _queue; - StreamType* const _ostream; + std::ostream* const _ostream; std::atomic _running; std::shared_ptr _runningThread = nullptr; }; -template -bool Logger::isRunning() const { - return _running && bool(_runningThread); -} +class BasicLogger : public Logger { +public: + using Base = Logger; + BasicLogger(std::ostream& stream) : Base(stream) {} -template -unsigned int Logger::count() const { - return _queue.size(); -} - -template -bool Logger::run() { - if (!isRunning()) { - _runningThread = std::make_shared([&]{ - while(this->isRunning()) { - this->flush(); - } - }); + virtual void process(const LogEvent& event) { + *_ostream << event.msg << "\n"; } - return isRunning(); -} - -template -bool Logger::stop() { - if (isRunning()) { - _running = false; - _runningThread->join(); - _runningThread = nullptr; - flush(); - } - return isRunning(); -} - -template -LogEvent Logger::pop() { - std::shared_ptr event; - _queue.pop(event); - process(*event); - return *event; -} +}; } // namespace nb #endif // _NB_LOGGER \ No newline at end of file diff --git a/engine/NBCore/tests/ErrorTest/main.cpp b/engine/NBCore/tests/ErrorTest/main.cpp index 7b0885b..04643a8 100644 --- a/engine/NBCore/tests/ErrorTest/main.cpp +++ b/engine/NBCore/tests/ErrorTest/main.cpp @@ -42,7 +42,7 @@ int main() { } } catch(const std::exception& e) { // THROW(Error, Error::ErrorCodes::UNDEFINED, e); - Logger log(std::cout); + BasicLogger log(std::cout); std::cout << "Logger is starting: " << log.run() << std::endl; std::cout << "Logger is running: " << log.isRunning() << std::endl; log.error(e);