Skip to content

Commit 5a9887b

Browse files
committed
Added logger file outputs
1 parent 99780f2 commit 5a9887b

File tree

6 files changed

+109
-34
lines changed

6 files changed

+109
-34
lines changed

src/WaterDropEngine/WaterDropEngine.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,36 @@ namespace wde {
44
WaterDropEngine::WaterDropEngine() {
55
try {
66
WDE_PROFILE_BEGIN_SESSION("Initialization", "logs/profiler_init.json");
7-
Logger::forceLog("======== Initializing program ========", LoggerChannel::MAIN);
87

9-
// Create WaterDropEngine instance
8+
// Initialize logger
9+
// Log level
10+
auto logLevel = Logger::LoggerLogLevel::DEBUG;
11+
std::vector<LoggerChannel> activatedChannels = { LoggerChannel::MAIN, LoggerChannel::RENDERING_ENGINE, LoggerChannel::COMMON };
12+
Logger::initialize("logs/", logLevel, activatedChannels);
13+
14+
Logger::debug("======== Initializing program ========", LoggerChannel::MAIN);
15+
16+
// Create WaterDropEngine instance
1017
_instance = std::make_unique<WdeInstance>();
1118
}
1219
catch (const WdeException &e) {
13-
Logger::forceLog("", LoggerChannel::MAIN);
20+
Logger::err("", LoggerChannel::MAIN);
1421
Logger::err(e.what(), e.getChannel());
1522
}
1623
}
1724

1825
WdeStatus WaterDropEngine::initialize() {
1926
try {
20-
// Log level
21-
auto logLevel = Logger::LoggerLogLevel::DEBUG;
22-
std::vector<LoggerChannel> activatedChannels = { LoggerChannel::MAIN, LoggerChannel::RENDERING_ENGINE, LoggerChannel::COMMON };
23-
2427
// Initialize instance
25-
if (_instance->initialize(logLevel, activatedChannels) != WdeStatus::WDE_SUCCESS)
28+
if (_instance->initialize() != WdeStatus::WDE_SUCCESS)
2629
throw WdeException("Error initializing engine.", LoggerChannel::MAIN);
2730

2831
// End of initialization
2932
Logger::debug("======== Initialization done ========\n\n", LoggerChannel::MAIN);
3033
WDE_PROFILE_END_SESSION();
3134
}
3235
catch (const WdeException &e) {
33-
Logger::forceLog("", LoggerChannel::MAIN);
36+
Logger::err("", LoggerChannel::MAIN);
3437
Logger::err(e.what(), e.getChannel());
3538
return WdeStatus::WDE_ERROR;
3639
}
@@ -55,11 +58,14 @@ namespace wde {
5558
Logger::debug("======== Cleaning up ========", LoggerChannel::MAIN);
5659
if (_instance->cleanUp() != WdeStatus::WDE_SUCCESS)
5760
throw WdeException("Error cleaning up engine.", LoggerChannel::MAIN);
58-
Logger::forceLog("======== End of program ========\n", LoggerChannel::MAIN);
61+
Logger::debug("======== End of program ========\n", LoggerChannel::MAIN);
62+
63+
// Clean up logger
64+
Logger::cleanUp();
5965
WDE_PROFILE_END_SESSION();
6066
}
6167
catch (const WdeException &e) {
62-
Logger::forceLog("", LoggerChannel::MAIN);
68+
Logger::err("", LoggerChannel::MAIN);
6369
Logger::err(e.what(), e.getChannel());
6470
return WdeStatus::WDE_ERROR;
6571
}

src/WaterDropEngine/WdeCommon/WdeLogger/Logger.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "Logger.hpp"
22

33
namespace wde {
4+
// Log file not initialized by default
5+
bool Logger::_logFileInitialized = false;
6+
std::ofstream Logger::_logFile;
7+
48
// Initialize Log Level values
59
Logger::LoggerLogLevel Logger::_logLevel = Logger::LoggerLogLevel::DEBUG;
610
std::vector<LoggerChannel> Logger::_activatedChannels = { LoggerChannel::MAIN };
@@ -33,11 +37,6 @@ namespace wde {
3337
}
3438

3539

36-
void Logger::forceLog(const std::string &message, LoggerChannel channel) {
37-
outputMessage(getFormatedMessage(message, " MAIN ", channel));
38-
}
39-
40-
4140

4241
bool Logger::checkValidInput(LoggerChannel channel, LoggerLogLevel providedLogLevel) {
4342
return
@@ -78,10 +77,24 @@ namespace wde {
7877

7978
// Message output
8079
void Logger::outputMessage(const std::string &message) {
80+
// Outputs the message
8181
std::cout << message << std::endl;
82+
83+
// Stores the message to the log file
84+
if (_logFileInitialized) {
85+
_logFile << message << std::endl;
86+
_logFile.flush();
87+
}
8288
}
8389

8490
void Logger::outputError(const std::string &message) {
91+
// Outputs the error
8592
std::cerr << message << std::endl;
93+
94+
// Stores the error to the log file
95+
if (_logFileInitialized) {
96+
_logFile << message << std::endl;
97+
_logFile.flush();
98+
}
8699
}
87100
}

src/WaterDropEngine/WdeCommon/WdeLogger/Logger.hpp

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <utility>
66
#include <windows.h>
77
#include <vector>
8+
#include <fstream>
9+
#include <chrono>
10+
#include "../WdeUtils/Constants.hpp"
811

912
namespace 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

src/WaterDropEngine/WdeCommon/WdeUtils/Constants.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@
55

66
namespace wde::Constants {
77
extern const std::string APPLICATION_NAME = "WaterDrop Engine";
8-
extern const uint32_t APPLICATION_VERSION = VK_MAKE_VERSION(1, 0, 0);
8+
9+
extern const std::vector APPLICATION_VERSION_RAW {1, 0, 0};
10+
extern const uint32_t APPLICATION_VERSION = VK_MAKE_VERSION(APPLICATION_VERSION_RAW[0],
11+
APPLICATION_VERSION_RAW[1], APPLICATION_VERSION_RAW[2]);
12+
extern const std::string APPLICATION_VERSION_FORMATTED = "v" + std::to_string(APPLICATION_VERSION_RAW[0]) + "."
13+
+ std::to_string(APPLICATION_VERSION_RAW[1]) + "." + std::to_string(APPLICATION_VERSION_RAW[2]);
914
}

src/WaterDropEngine/WdeCore/WdeInstance.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#include "WdeInstance.hpp"
22

33
namespace wde {
4-
WdeStatus WdeInstance::initialize(Logger::LoggerLogLevel logLevel, std::vector<LoggerChannel> logActivatedChannels) {
5-
// Initialize Logging system
6-
Logger::initialize(logLevel, logActivatedChannels);
7-
4+
WdeStatus WdeInstance::initialize() {
85
// Initialize 3D Rendering engine
96
wdeRenderingEngine.initialize();
107

src/WaterDropEngine/WdeCore/WdeInstance.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ namespace wde {
1111
public:
1212
/**
1313
* Initialize the WdeInstance and all of its important different class components
14-
* @param logLevel The level of the displayed console log
15-
* @param logActivatedChannels A vector containing a list of every enabled channels
1614
*/
17-
WdeStatus initialize(Logger::LoggerLogLevel logLevel, std::vector<LoggerChannel> logActivatedChannels);
15+
WdeStatus initialize();
1816

1917
/** Run the WdeInstance and all of its important different class components */
2018
WdeStatus run();

0 commit comments

Comments
 (0)