feat(logging): basic logger implementation
This commit is contained in:
parent
1fb2a793cf
commit
6d4dc844b3
6 changed files with 77 additions and 4 deletions
2
.clangd
Normal file
2
.clangd
Normal file
|
@ -0,0 +1,2 @@
|
|||
CompileFlags:
|
||||
Add: [-std=c++20]
|
|
@ -15,4 +15,9 @@ file(GLOB_RECURSE SOURCES "src/*.cpp" "src/*.h")
|
|||
add_library(openblocks STATIC ${SOURCES})
|
||||
set_target_properties(openblocks PROPERTIES OUTPUT_NAME "openblocks")
|
||||
target_link_libraries(openblocks ${GLEW_LIBRARIES} OpenGL::GL ReactPhysics3D::ReactPhysics3D pugixml::pugixml)
|
||||
target_include_directories(openblocks PUBLIC "src" "../include")
|
||||
target_include_directories(openblocks PUBLIC "src" "../include")
|
||||
|
||||
# Windows-specific dependencies
|
||||
if(WIN32)
|
||||
target_link_libraries(openblocks shell32.lib)
|
||||
endif()
|
43
core/src/logger.cpp
Normal file
43
core/src/logger.cpp
Normal file
|
@ -0,0 +1,43 @@
|
|||
#include "logger.h"
|
||||
#include "platform.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include <format>
|
||||
|
||||
static std::ofstream logStream;
|
||||
|
||||
void Logger::init() {
|
||||
initProgramLogsDir();
|
||||
|
||||
const auto now = std::chrono::system_clock::now();
|
||||
|
||||
std::string fileName = std::format("log_{0:%Y%m%d}_{0:%H%M%S}.txt", now);
|
||||
|
||||
logStream = std::ofstream(getProgramLogsDir() + "/" + fileName);
|
||||
Logger::debug("Logger initialized");
|
||||
}
|
||||
|
||||
void Logger::finish() {
|
||||
Logger::debug("Closing logger...");
|
||||
logStream.close();
|
||||
}
|
||||
|
||||
void log(std::string message, Logger::LogLevel logLevel) {
|
||||
std::string logLevelStr = logLevel == Logger::LogLevel::INFO ? "INFO" :
|
||||
logLevel == Logger::LogLevel::DEBUG ? "DEBUG" :
|
||||
logLevel == Logger::LogLevel::WARNING ? "WARN" :
|
||||
logLevel == Logger::LogLevel::ERROR ? "ERROR" :
|
||||
logLevel == Logger::LogLevel::FATAL_ERROR ? "FATAL" : "?";
|
||||
|
||||
const auto now = std::chrono::system_clock::now();
|
||||
|
||||
std::string formattedLogLine = std::format("[{:%Y-%m-%d %X}] [{}] {}", now, logLevelStr, message);
|
||||
|
||||
printf("%s\n", formattedLogLine.c_str());
|
||||
logStream << formattedLogLine << std::endl;
|
||||
|
||||
if (logLevel == Logger::LogLevel::FATAL_ERROR) {
|
||||
displayErrorMessage(formattedLogLine);
|
||||
}
|
||||
}
|
23
core/src/logger.h
Normal file
23
core/src/logger.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Logger {
|
||||
enum class LogLevel {
|
||||
INFO,
|
||||
DEBUG,
|
||||
WARNING,
|
||||
ERROR,
|
||||
FATAL_ERROR,
|
||||
};
|
||||
|
||||
void init();
|
||||
void finish();
|
||||
|
||||
void log(std::string message, LogLevel logLevel);
|
||||
inline void info(std::string message) { log(message, LogLevel::INFO); }
|
||||
inline void debug(std::string message) { log(message, LogLevel::DEBUG); }
|
||||
inline void warning(std::string message) { log(message, LogLevel::WARNING); }
|
||||
inline void error(std::string message) { log(message, LogLevel::ERROR); }
|
||||
inline void fatalError(std::string message) { log(message, LogLevel::FATAL_ERROR); }
|
||||
};
|
|
@ -22,7 +22,7 @@ std::string getProgramDataDir() {
|
|||
return std::string(homedir) + "/openblocks";
|
||||
}
|
||||
|
||||
void printErrorMessage(std::string message) {
|
||||
void displayErrorMessage(std::string message) {
|
||||
fprintf(stderr, "%s\n", message.c_str());
|
||||
}
|
||||
|
||||
|
@ -46,7 +46,7 @@ std::string getProgramDataDir() {
|
|||
return localAppData + "/openblocks";
|
||||
}
|
||||
|
||||
void printErrorMessage(std::string message) {
|
||||
void displayErrorMessage(std::string message) {
|
||||
fprintf(stderr, "%s\n", message.c_str());
|
||||
MessageBoxA(NULL, message.c_str(), "Fatal Error", MB_OK);
|
||||
}
|
||||
|
|
|
@ -20,4 +20,4 @@ void initProgramDataDir();
|
|||
void initProgramLogsDir();
|
||||
|
||||
// Displays an error message box on Windows, or prints to eprintf
|
||||
void printErrorMessage(std::string message);
|
||||
void displayErrorMessage(std::string message);
|
Loading…
Add table
Reference in a new issue