55#include < utility>
66#include < windows.h>
77#include < vector>
8+ #include < fstream>
9+ #include < chrono>
10+ #include " ../WdeUtils/Constants.hpp"
811
912namespace wde {
1013 /* * List of different LoggerChannels */
@@ -17,6 +20,7 @@ namespace wde {
1720
1821 class Logger {
1922 public:
23+ // Base parameters
2024 /* * Verbose log level class */
2125 enum class LoggerLogLevel {
2226 /* * Log only errors */ ERR,
@@ -27,15 +31,70 @@ namespace wde {
2731
2832 /* *
2933 * Initialize a new Logger with a new config
34+ * @param filepath
3035 * @param logLevel The LoggerLogLevel log output level
3136 * @param activatedChannels The list of every activated logged channels
3237 */
33- static void initialize (LoggerLogLevel& logLevel, std::vector<LoggerChannel>& activatedChannels) {
38+ static void initialize (const std::string& filepath, LoggerLogLevel& logLevel, std::vector<LoggerChannel>& activatedChannels) {
3439 Logger::_logLevel = logLevel;
3540 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 ;
3668 }
3769
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+ }
3895
96+
97+ // Output functions
3998 /* *
4099 * Prints a debug message to the console
41100 * @param message The std::string message
@@ -65,20 +124,17 @@ namespace wde {
65124 static void err (const std::string &message, LoggerChannel channel);
66125
67126
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-
76127
77128 private:
129+ // Core parameters
78130 /* * List of activated channels */
79131 static LoggerLogLevel _logLevel;
80132 static std::vector<LoggerChannel> _activatedChannels;
133+ static std::ofstream _logFile;
134+ static bool _logFileInitialized;
135+
81136
137+ // Helper functions
82138 /* *
83139 * @param channel The channel to check
84140 * @param providedLogLevel The log level of the message to be shown
@@ -102,7 +158,7 @@ namespace wde {
102158 static std::string getNameOf (LoggerChannel channel);
103159
104160
105-
161+ // Log output functions
106162 /* *
107163 * Outputs a message to the console
108164 * @param message The message
0 commit comments