-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.hpp
140 lines (94 loc) · 3.88 KB
/
logger.hpp
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
134
135
136
137
138
139
140
#pragma once
#include <stdio.h>
//used for log timestamps
#include <ctime>
enum LogPriorityLevel { TRACE, DEBUG, INFO, WARN, ERROR, FATAL };
class Logger{
private:
static LogPriorityLevel LoggingPriority;
static const char* filepath;
static FILE* file;
public:
static void SetLoggingPriority(LogPriorityLevel newPriority){
LoggingPriority = newPriority;
}
//used when user enable file logging
static void EnableFileOutput(){
filepath = "logs/log.txt";
enable_file_output();
}
static void EnableFileOutput(const char* newFilePath){
filepath = newFilePath;
enable_file_output();
}
static void DisableFileOutput(){
free_file();
}
//Logging for TRACE level priority, takes a format string and a variable number of args using variadic template
template<typename... Args>
static void Trace(const char* message, Args... args){
log("TRACE",TRACE, message, args...);
}
//Logging for DEBUG level priority, takes a format string and a variable number of args using variadic template
template<typename... Args>
static void Debug(const char* message, Args... args){
log("DEBUG", DEBUG, message, args...);
}
//Logging for INFO level priority, takes a format string and a variable number of args using variadic template
template<typename... Args>
static void Info(const char* message, Args... args){
log("Info", INFO, message, args...);
}
//Logging for WARN level priority, takes a format string and a variable number of args using variadic template
template<typename... Args>
static void Warn(const char* message, Args... args){
log("WARN", WARN, message, args...);
}
//Logging for ERROR level priority, takes a format string and a variable number of args using variadic template
template<typename... Args>
static void Error(const char* message, Args... args){
log("ERROR", ERROR, message, args...);
}
//Logging for FATAL level priority, takes a format string and a variable number of args using variadic template
template<typename... Args>
static void Fatal(const char* message, Args... args){
log("FATAL", FATAL, message, args...);
}
private:
template<typename... Args>
static void log(const char* logPrefix, LogPriorityLevel priority, const char* message, Args... args){
if(LoggingPriority <= priority){
//get current time and format it
std::time_t current_time = std::time(0);
std::tm* timestamp = std::localtime(¤t_time);
char buffer[80];
strftime(buffer,80,"%c", timestamp);
//log to console
printf("%s\t", buffer);
printf("[%s]\t", logPrefix);
printf(message, args...);
printf("\n");
//log to file if enabled
if(file){
fprintf(file, "%s\t", buffer);
fprintf(file,"[%s]\t", logPrefix);
fprintf(file,message, args...);
fprintf(file,"\n");
}
}
}
static void enable_file_output(){
if(file!=0)
fclose(file);
file = fopen(filepath, "a");
if(file == 0)
printf("Logger: Failed top open file at %s\n", filepath);
}
static void free_file(){
fclose(file);
file = 0;
}
};
LogPriorityLevel Logger::LoggingPriority = TRACE;
const char* Logger::filepath = 0;
FILE* Logger::file = 0;