-
Notifications
You must be signed in to change notification settings - Fork 3
/
global.h
134 lines (119 loc) · 4.59 KB
/
global.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef GLOBALS_H
#define GLOBALS_H
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
#include <thread>
#include <ctime>
#include <Eigen/Dense>
extern double temp_K;
extern std::ofstream outfile;
class Logger;
extern Logger logger; // Global logger object, include global.h to use it
void update_progress(double progress_f);
// Logger class for logging messages to the console and a log file
class Logger {
public:
Logger() : outfile("EVPSC_log.txt") {}
// Define the message hierarchy levels inside the Logger class
enum class MessageLevel {
DEBUG,
NOTICE,
WARN,
INFO,
ERROR,
};
// Log a message at a specific message level
void log(MessageLevel level, const std::string& message) {
// Get the current time
std::time_t t = std::time(nullptr);
std::string timestamp = std::asctime(std::localtime(&t));
timestamp.pop_back(); // Remove the newline character from the end
// Output the message to the console
if (console_level_ <= level) {
std::cout << "[" << timestamp << "] ";
printPrefix(level);
std::cout << message << std::endl;
}
// Output the message to the log file
if (file_level_ <= level) {
outfile << "[" << timestamp << "] ";
printPrefix(level, outfile);
outfile << message << std::endl;
}
}
void log(MessageLevel level, const Eigen::MatrixXd& message) {
// Get the current time
std::time_t t = std::time(nullptr);
std::string timestamp = std::asctime(std::localtime(&t));
timestamp.pop_back(); // Remove the newline character from the end
Eigen::IOFormat LogFmt(4, 0, ", ", "\n", "["+timestamp+"] "+getPrefix(level)+ "[", "]");
// Output the message to the console
if (console_level_ <= level) {
std::cout << message.format(LogFmt) << std::endl;
}
// Output the message to the log file
if (file_level_ <= level) {
outfile << message.format(LogFmt) << std::endl;
}
}
// Overloaded functions for different message levels
void debug(const std::string& message) { log(MessageLevel::DEBUG, message); }
void info(const std::string& message) { log(MessageLevel::INFO, message); }
void notice(const std::string& message) { log(MessageLevel::NOTICE, message); }
void warn(const std::string& message) { log(MessageLevel::WARN, message); }
void error(const std::string& message) { log(MessageLevel::ERROR, message); }
void debug(const Eigen::MatrixXd& message) { log(MessageLevel::DEBUG, message); }
void info(const Eigen::MatrixXd& message) { log(MessageLevel::INFO, message); }
void notice(const Eigen::MatrixXd& message) { log(MessageLevel::NOTICE, message); }
void warn(const Eigen::MatrixXd& message) { log(MessageLevel::WARN, message); }
void error(const Eigen::MatrixXd& message) { log(MessageLevel::ERROR, message); }
// Set the message level filter for console output
void setConsoleLevel(MessageLevel level) { console_level_ = level; }
// Set the message level filter for file output
void setFileLevel(MessageLevel level) { file_level_ = level; }
private:
std::ofstream outfile;
MessageLevel console_level_ = MessageLevel::INFO;
MessageLevel file_level_ = MessageLevel::DEBUG;
// Helper function to print the message prefix based on the message level
void printPrefix(MessageLevel level, std::ostream& stream = std::cout) {
switch (level) {
case MessageLevel::DEBUG:
stream << "[ DEBUG] ";
break;
case MessageLevel::INFO:
stream << "[ INFO ] ";
break;
case MessageLevel::NOTICE:
stream << "[NOTICE] ";
break;
case MessageLevel::WARN:
stream << "[ WARN ] ";
break;
case MessageLevel::ERROR:
stream << "[ ERROR] ";
break;
default:
break;
}
}
std::string getPrefix(MessageLevel level) {
switch (level) {
case MessageLevel::DEBUG:
return "[ DEBUG] ";
case MessageLevel::INFO:
return "[ INFO ] ";
case MessageLevel::NOTICE:
return "[NOTICE] ";
case MessageLevel::WARN:
return "[ WARN ] ";
case MessageLevel::ERROR:
return "[ ERROR] ";
default:
return "";
}
}
};
#endif