diff -r 5b351628a377 -r 15d87fdd6e6c Logger.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Logger.cpp Mon Jan 04 15:45:12 2021 +0100 @@ -0,0 +1,78 @@ +/** + * DJM-Fix + * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include + +#include "Logger.h" + +namespace djmfix { +namespace logging { + +class Blackhole : public Logger { +public: + + virtual void log(Level level, const std::string& message) override { + } +}; + +class LoggerImpl : public Logger { +private: + std::ostream& output; + Level minLevel; + + std::string getTimestamp() { + auto now = std::chrono::system_clock::now(); + auto itt = std::chrono::system_clock::to_time_t(now); + std::ostringstream ss; + ss << std::put_time(localtime(&itt), "%FT%T%z"); + return ss.str(); + } + + std::string toString(Level level) { + if (level == Level::SEVERE) return "SEVERE"; + else if (level == Level::WARNING) return "WARNING"; + else if (level == Level::INFO) return "INFO"; + else if (level == Level::CONFIG) return "CONFIG"; + else if (level == Level::FINE) return "FINE"; + else if (level == Level::FINER) return "FINER"; + else if (level == Level::FINEST) return "FINEST"; + else return "UNKNOWN"; + } + +public: + + LoggerImpl(std::ostream& output, Level minLevel) : output(output), minLevel(minLevel) { + } + + virtual void log(Level level, const std::string& message) override { + if (level <= minLevel) { + output << getTimestamp() << " " << std::setw(8) << toString(level) << ": " << message << std::endl; + } + } +}; + +Logger* create(std::ostream& output, Level minLevel) { + return new LoggerImpl(output, minLevel); +} + +Logger* blackhole() { + return new Blackhole(); +} + +} +}