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