3 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 class Blackhole : public Logger {
29 virtual void log(Level level, const std::string& message) override {
33 class LoggerImpl : public Logger {
38 std::string getTimestamp() {
39 auto now = std::chrono::system_clock::now();
40 auto itt = std::chrono::system_clock::to_time_t(now);
41 std::ostringstream ss;
42 ss << std::put_time(localtime(&itt), "%FT%T%z");
46 std::string toString(Level level) {
47 if (level == Level::SEVERE) return "SEVERE";
48 else if (level == Level::WARNING) return "WARNING";
49 else if (level == Level::INFO) return "INFO";
50 else if (level == Level::CONFIG) return "CONFIG";
51 else if (level == Level::FINE) return "FINE";
52 else if (level == Level::FINER) return "FINER";
53 else if (level == Level::FINEST) return "FINEST";
54 else return "UNKNOWN";
59 LoggerImpl(std::ostream& output, Level minLevel) : output(output), minLevel(minLevel) {
62 virtual void log(Level level, const std::string& message) override {
63 if (level <= minLevel) {
64 output << getTimestamp() << " " << std::setw(8) << toString(level) << ": " << message << std::endl;
69 Logger* create(std::ostream& output, Level minLevel) {
70 return new LoggerImpl(output, minLevel);
74 return new Blackhole();