5
5
#include < utility>
6
6
#include < windows.h>
7
7
#include < vector>
8
+ #include < fstream>
9
+ #include < chrono>
10
+ #include " ../WdeUtils/Constants.hpp"
8
11
9
12
namespace wde {
10
13
/* * List of different LoggerChannels */
@@ -17,6 +20,7 @@ namespace wde {
17
20
18
21
class Logger {
19
22
public:
23
+ // Base parameters
20
24
/* * Verbose log level class */
21
25
enum class LoggerLogLevel {
22
26
/* * Log only errors */ ERR,
@@ -27,15 +31,70 @@ namespace wde {
27
31
28
32
/* *
29
33
* Initialize a new Logger with a new config
34
+ * @param filepath
30
35
* @param logLevel The LoggerLogLevel log output level
31
36
* @param activatedChannels The list of every activated logged channels
32
37
*/
33
- static void initialize (LoggerLogLevel& logLevel, std::vector<LoggerChannel>& activatedChannels) {
38
+ static void initialize (const std::string& filepath, LoggerLogLevel& logLevel, std::vector<LoggerChannel>& activatedChannels) {
34
39
Logger::_logLevel = logLevel;
35
40
Logger::_activatedChannels = activatedChannels;
41
+
42
+ // Gets time as format %Y.%m.%d-%H.%M.%S
43
+ std::time_t rawtime;
44
+ std::tm* timeinfo;
45
+ char buffer [80 ];
46
+ std::time (&rawtime);
47
+ timeinfo = std::localtime (&rawtime);
48
+ std::strftime (buffer,80 ," %Y.%m.%d-%H.%M.%S" , timeinfo);
49
+
50
+ // Initialize log file
51
+ _logFile.open (filepath + " logs_" + buffer + " .txt" );
52
+ if (!_logFile.is_open ())
53
+ throw std::runtime_error (" Couldn't open log file." );
54
+
55
+ // Log header
56
+ char buffer2 [80 ];
57
+ std::strftime (buffer2, 80 , " %d/%m/%Y-%H:%M:%S" , timeinfo);
58
+ std::time_t timer = std::time (nullptr );
59
+ _logFile << " ======================================================================\n "
60
+ << " Begin Output log ("
61
+ << buffer2
62
+ << " ) : "
63
+ << Constants::APPLICATION_NAME << " - " << Constants::APPLICATION_VERSION_FORMATTED
64
+ << " \n ======================================================================\n\n " ;
65
+ _logFile.flush ();
66
+
67
+ _logFileInitialized = true ;
36
68
}
37
69
70
+ /* *
71
+ * Clean up the Logger and close the log file
72
+ */
73
+ static void cleanUp () {
74
+ _logFileInitialized = false ;
75
+
76
+ std::time_t rawtime;
77
+ std::tm* timeinfo;
78
+ char buffer [80 ];
79
+ std::time (&rawtime);
80
+ timeinfo = std::localtime (&rawtime);
81
+ std::strftime (buffer, 80 , " %d/%m/%Y-%H:%M:%S" , timeinfo);
82
+
83
+ // Log footer
84
+ _logFile << " ======================================================================\n "
85
+ << " End Output log ("
86
+ << buffer
87
+ << " ) : "
88
+ << Constants::APPLICATION_NAME << " - " << Constants::APPLICATION_VERSION_FORMATTED
89
+ << " \n ======================================================================\n\n " ;
90
+ _logFile.flush ();
91
+
92
+ // Close file
93
+ _logFile.close ();
94
+ }
38
95
96
+
97
+ // Output functions
39
98
/* *
40
99
* Prints a debug message to the console
41
100
* @param message The std::string message
@@ -65,20 +124,17 @@ namespace wde {
65
124
static void err (const std::string &message, LoggerChannel channel);
66
125
67
126
68
- /* *
69
- * Forces a message to be displayed on the console
70
- * @param message The std::string message
71
- * @param channel The channel of the message
72
- */
73
- static void forceLog (const std::string &message, LoggerChannel channel);
74
-
75
-
76
127
77
128
private:
129
+ // Core parameters
78
130
/* * List of activated channels */
79
131
static LoggerLogLevel _logLevel;
80
132
static std::vector<LoggerChannel> _activatedChannels;
133
+ static std::ofstream _logFile;
134
+ static bool _logFileInitialized;
135
+
81
136
137
+ // Helper functions
82
138
/* *
83
139
* @param channel The channel to check
84
140
* @param providedLogLevel The log level of the message to be shown
@@ -102,7 +158,7 @@ namespace wde {
102
158
static std::string getNameOf (LoggerChannel channel);
103
159
104
160
105
-
161
+ // Log output functions
106
162
/* *
107
163
* Outputs a message to the console
108
164
* @param message The message
0 commit comments