Logger.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 04 Jan 2021 15:45:12 +0100
branchv_0
changeset 12 15d87fdd6e6c
permissions -rw-r--r--
use Logger instead of messing with STDIO directly
franta-hg@12
     1
/**
franta-hg@12
     2
 * DJM-Fix
franta-hg@12
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
franta-hg@12
     4
 *
franta-hg@12
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@12
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@12
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@12
     8
 *
franta-hg@12
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@12
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@12
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@12
    12
 * GNU General Public License for more details.
franta-hg@12
    13
 *
franta-hg@12
    14
 * You should have received a copy of the GNU General Public License
franta-hg@12
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@12
    16
 */
franta-hg@12
    17
#include <chrono>
franta-hg@12
    18
#include <iomanip>
franta-hg@12
    19
#include <sstream>
franta-hg@12
    20
franta-hg@12
    21
#include "Logger.h"
franta-hg@12
    22
franta-hg@12
    23
namespace djmfix {
franta-hg@12
    24
namespace logging {
franta-hg@12
    25
franta-hg@12
    26
class Blackhole : public Logger {
franta-hg@12
    27
public:
franta-hg@12
    28
franta-hg@12
    29
	virtual void log(Level level, const std::string& message) override {
franta-hg@12
    30
	}
franta-hg@12
    31
};
franta-hg@12
    32
franta-hg@12
    33
class LoggerImpl : public Logger {
franta-hg@12
    34
private:
franta-hg@12
    35
	std::ostream& output;
franta-hg@12
    36
	Level minLevel;
franta-hg@12
    37
franta-hg@12
    38
	std::string getTimestamp() {
franta-hg@12
    39
		auto now = std::chrono::system_clock::now();
franta-hg@12
    40
		auto itt = std::chrono::system_clock::to_time_t(now);
franta-hg@12
    41
		std::ostringstream ss;
franta-hg@12
    42
		ss << std::put_time(localtime(&itt), "%FT%T%z");
franta-hg@12
    43
		return ss.str();
franta-hg@12
    44
	}
franta-hg@12
    45
franta-hg@12
    46
	std::string toString(Level level) {
franta-hg@12
    47
		if (level == Level::SEVERE) return "SEVERE";
franta-hg@12
    48
		else if (level == Level::WARNING) return "WARNING";
franta-hg@12
    49
		else if (level == Level::INFO) return "INFO";
franta-hg@12
    50
		else if (level == Level::CONFIG) return "CONFIG";
franta-hg@12
    51
		else if (level == Level::FINE) return "FINE";
franta-hg@12
    52
		else if (level == Level::FINER) return "FINER";
franta-hg@12
    53
		else if (level == Level::FINEST) return "FINEST";
franta-hg@12
    54
		else return "UNKNOWN";
franta-hg@12
    55
	}
franta-hg@12
    56
franta-hg@12
    57
public:
franta-hg@12
    58
franta-hg@12
    59
	LoggerImpl(std::ostream& output, Level minLevel) : output(output), minLevel(minLevel) {
franta-hg@12
    60
	}
franta-hg@12
    61
franta-hg@12
    62
	virtual void log(Level level, const std::string& message) override {
franta-hg@12
    63
		if (level <= minLevel) {
franta-hg@12
    64
			output << getTimestamp() << " " << std::setw(8) << toString(level) << ":  " << message << std::endl;
franta-hg@12
    65
		}
franta-hg@12
    66
	}
franta-hg@12
    67
};
franta-hg@12
    68
franta-hg@12
    69
Logger* create(std::ostream& output, Level minLevel) {
franta-hg@12
    70
	return new LoggerImpl(output, minLevel);
franta-hg@12
    71
}
franta-hg@12
    72
franta-hg@12
    73
Logger* blackhole() {
franta-hg@12
    74
	return new Blackhole();
franta-hg@12
    75
}
franta-hg@12
    76
franta-hg@12
    77
}
franta-hg@12
    78
}