Add basic logging and error system

This commit is contained in:
Grayson Riffe 2025-01-28 16:53:15 -06:00
parent 581db91157
commit b48df32331
6 changed files with 100 additions and 2 deletions

View File

@ -1,5 +1,5 @@
# NF library CMakeLists.txt
add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h")
add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h" "src/util.h" "src/util/log.h" "src/util/log.cpp")
# Use C++20
set_property(TARGET NothinFancy PROPERTY CXX_STANDARD 20)

View File

@ -2,10 +2,12 @@
#include "pch.h"
#include "version.h"
#include "util.h"
namespace nf {
void startEngine() {
std::cout << std::format("Nothin' Fancy {}\n", NFVERSION);
NFLog(std::format("Nothin' Fancy {}", NFVERSION));
NFError("Test error.");
std::cin.get();
}
}

View File

@ -33,3 +33,7 @@
#include <algorithm>
#include <numbers>
#include <random>
// Windows
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

4
NothinFancy/src/util.h Normal file
View File

@ -0,0 +1,4 @@
// Miscellaneous utilities header
#pragma once
#include "util/log.h"

View File

@ -0,0 +1,59 @@
// NF logging system implementation
#include "pch.h"
#include "log.h"
namespace nf::util {
// Log is only present in debug builds
#ifdef _DEBUG
// Mutex to synchronize access to stdout
static std::mutex s_logMutex;
// Needed to enable Win32 virtual terminal processing
static bool s_isLogInit = false;
// Mapping of type strings
static std::map<LogType, const char*> s_logTypesMap = {
{LogType::Log, "\x1b[93mLog\x1b[0m"},
{LogType::Error, "\x1b[91mError\x1b[0m"}
};
static void initLog() {
HANDLE stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD consoleMode = 0;
GetConsoleMode(stdoutHandle, &consoleMode);
consoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(stdoutHandle, consoleMode);
s_isLogInit = true;
}
void log(const char* msg, LogType type) {
std::lock_guard logLock(s_logMutex);
if (!s_isLogInit)
initLog();
std::cout << std::format("NF {}: {}\n", s_logTypesMap[type], msg);
}
void log(const std::string& msg, LogType type) {
log(msg.c_str(), type);
}
#endif
void error(const char* msg, const char* file, unsigned int line) {
std::string filename = file;
filename = filename.substr(filename.rfind("\\src\\") + 5);
#ifdef _DEBUG
log(std::format("({}, {}) {}", filename, line, msg), LogType::Error);
MessageBeep(0);
__debugbreak();
#else
std::string fmtMsg = std::format("{}\n\n({} at line {})", msg, filename, line);
MessageBox(nullptr, fmtMsg.c_str(), "Engine Error", MB_OK | MB_ICONERROR);
std::exit(1);
#endif
}
}

View File

@ -0,0 +1,29 @@
// NF logging and error system header
#pragma once
// Define NFLog
#ifdef _DEBUG // Debug builds
#define NFLog(x) ::nf::util::log(x, ::nf::util::LogType::Log)
#else // Release builds
#define NFLog(x)
#endif
// Define NFError
#define NFError(x) ::nf::util::error(x, __FILE__, __LINE__)
namespace nf::util {
enum class LogType {
Log,
Error
};
void log(const char* msg, LogType type);
void log(const std::string& msg, LogType type);
template<typename T>
void log(T data, LogType type) {
log(std::to_string(data), type);
}
void error(const char* msg, const char* file, unsigned int line);
}