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