From b48df3233197ca314b68dfc40ed84650fb8fd458 Mon Sep 17 00:00:00 2001 From: Grayson Riffe Date: Tue, 28 Jan 2025 16:53:15 -0600 Subject: [PATCH] Add basic logging and error system --- NothinFancy/CMakeLists.txt | 2 +- NothinFancy/src/Engine.cpp | 4 ++- NothinFancy/src/pch.h | 4 +++ NothinFancy/src/util.h | 4 +++ NothinFancy/src/util/log.cpp | 59 ++++++++++++++++++++++++++++++++++++ NothinFancy/src/util/log.h | 29 ++++++++++++++++++ 6 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 NothinFancy/src/util.h create mode 100644 NothinFancy/src/util/log.cpp create mode 100644 NothinFancy/src/util/log.h diff --git a/NothinFancy/CMakeLists.txt b/NothinFancy/CMakeLists.txt index 4c7515d..f2eb9b1 100644 --- a/NothinFancy/CMakeLists.txt +++ b/NothinFancy/CMakeLists.txt @@ -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) diff --git a/NothinFancy/src/Engine.cpp b/NothinFancy/src/Engine.cpp index e36fbaa..bcf79e2 100644 --- a/NothinFancy/src/Engine.cpp +++ b/NothinFancy/src/Engine.cpp @@ -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(); } } diff --git a/NothinFancy/src/pch.h b/NothinFancy/src/pch.h index 899f7c1..339b045 100644 --- a/NothinFancy/src/pch.h +++ b/NothinFancy/src/pch.h @@ -33,3 +33,7 @@ #include #include #include + +// Windows +#define WIN32_LEAN_AND_MEAN +#include diff --git a/NothinFancy/src/util.h b/NothinFancy/src/util.h new file mode 100644 index 0000000..ce1c3e0 --- /dev/null +++ b/NothinFancy/src/util.h @@ -0,0 +1,4 @@ +// Miscellaneous utilities header +#pragma once + +#include "util/log.h" diff --git a/NothinFancy/src/util/log.cpp b/NothinFancy/src/util/log.cpp new file mode 100644 index 0000000..7f0a140 --- /dev/null +++ b/NothinFancy/src/util/log.cpp @@ -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 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 + + } +} diff --git a/NothinFancy/src/util/log.h b/NothinFancy/src/util/log.h new file mode 100644 index 0000000..cac49a4 --- /dev/null +++ b/NothinFancy/src/util/log.h @@ -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 + void log(T data, LogType type) { + log(std::to_string(data), type); + } + + void error(const char* msg, const char* file, unsigned int line); +}