|
1 /** |
|
2 * DJM-Fix |
|
3 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 * |
|
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. |
|
8 * |
|
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. |
|
13 * |
|
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/>. |
|
16 */ |
|
17 #include <chrono> |
|
18 #include <iomanip> |
|
19 #include <sstream> |
|
20 |
|
21 #include "Logger.h" |
|
22 |
|
23 namespace djmfix { |
|
24 namespace logging { |
|
25 |
|
26 class Blackhole : public Logger { |
|
27 public: |
|
28 |
|
29 virtual void log(Level level, const std::string& message) override { |
|
30 } |
|
31 }; |
|
32 |
|
33 class LoggerImpl : public Logger { |
|
34 private: |
|
35 std::ostream& output; |
|
36 Level minLevel; |
|
37 |
|
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"); |
|
43 return ss.str(); |
|
44 } |
|
45 |
|
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"; |
|
55 } |
|
56 |
|
57 public: |
|
58 |
|
59 LoggerImpl(std::ostream& output, Level minLevel) : output(output), minLevel(minLevel) { |
|
60 } |
|
61 |
|
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; |
|
65 } |
|
66 } |
|
67 }; |
|
68 |
|
69 Logger* create(std::ostream& output, Level minLevel) { |
|
70 return new LoggerImpl(output, minLevel); |
|
71 } |
|
72 |
|
73 Logger* blackhole() { |
|
74 return new Blackhole(); |
|
75 } |
|
76 |
|
77 } |
|
78 } |