NeTrainSim 0.1.1 beta
The Open-Source Network Trains Simulator
 
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1
6#ifndef LOGGER_H
7#define LOGGER_H
8
9#include <string>
10#include <fstream>
11#include <chrono>
12#include <iomanip>
13#include <iostream>
14
15namespace Logger {
17 enum class LogLevel {
18 DEBUG,
19 INFO,
20 WARNING,
21 ERROR
22 };
23
34 inline static std::string levelToString(LogLevel level) {
35 switch (level) {
36 case LogLevel::DEBUG: return "DEBUG";
37 case LogLevel::INFO: return "INFO";
38 case LogLevel::WARNING: return "WARNING";
39 case LogLevel::ERROR: return "ERROR";
40 default: return "UNKNOWN";
41 }
42 }
43
50 class Logger {
51 private:
53 static constexpr LogLevel minLogLevel = LogLevel::DEBUG;
55 static std::ofstream logFile;
56
57 public:
58
68 static void logMessage(LogLevel level, const std::string& message) {
69 if (level <= minLogLevel) { return; }
70 if (!logFile.is_open()) {
71 std::cerr << "Error opening log file" << std::endl;
72 return;
73 }
74
75 auto now = std::chrono::system_clock::now();
76 std::time_t now_c = std::chrono::system_clock::to_time_t(now);
77 logFile << std::put_time(std::localtime(&now_c), "[%F %T]") << " ";
78 logFile << levelToString(level) << ": ";
79 logFile << message << std::endl;
80 }
81
90 static void DebugLogMessageToConsole(const std::string& message) {
91 if (LogLevel::DEBUG <= minLogLevel) { return; }
92 std::cout << levelToString(LogLevel::DEBUG) << ": ";
93 std::cout << message << std::endl;
94
95 }
96 };
97
98}
99
100#endif // LOGGER_H
static void logMessage(LogLevel level, const std::string &message)
Logs a message.
Definition logger.h:68
static void DebugLogMessageToConsole(const std::string &message)
Debug log message to console.
Definition logger.h:90
Definition logger.h:15
LogLevel
Values that represent log levels.
Definition logger.h:17