From 8174c19f00251e7b5c073a42deaa838ef21b2762 Mon Sep 17 00:00:00 2001 From: cat Date: Sun, 4 Jan 2026 22:34:17 +0200 Subject: [PATCH] Added Logging and tweaked Systems --- .clangd | 16 +++++ sourceCode/Systems/Implementation/Logging.cpp | 60 +++++++++++++++++++ .../Systems/{ => Implementation}/Random.cpp | 2 +- sourceCode/Systems/Logging.hpp | 34 +++++++++++ sourceCode/Types/UUID.cpp | 4 +- 5 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 .clangd create mode 100644 sourceCode/Systems/Implementation/Logging.cpp rename sourceCode/Systems/{ => Implementation}/Random.cpp (97%) create mode 100644 sourceCode/Systems/Logging.hpp diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..b1f1585 --- /dev/null +++ b/.clangd @@ -0,0 +1,16 @@ +{ + "CompileFlags": { + "Add": [ + "-std=c++23", + "-Wall", + "-Wextra", + "-Wpedantic", + ] + }, + "Index": { + "Background": "Build" + }, + "Completion": { + "AllScopes": true + } +} diff --git a/sourceCode/Systems/Implementation/Logging.cpp b/sourceCode/Systems/Implementation/Logging.cpp new file mode 100644 index 0000000..cd6c28d --- /dev/null +++ b/sourceCode/Systems/Implementation/Logging.cpp @@ -0,0 +1,60 @@ +/* + * SPDX-FileCopyrightText: Dora "cat" + * SPDX-License-Identifier: MPL-2.0 + * + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "../Logging.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace Tourmaline::Systems; + +// This is what happens when it takes you 50 years to implement +// reflections to a language +std::array Logging::LogLevelToString{ + "Critical", "Error", "Info", "Debug", "Trace"}; +std::fstream Logging::File; + +void Logging::LogToFile(std::string File) { + if (File == "") { + const auto now = std::chrono::system_clock::now(); + File = std::format("Tourmaline-{:%Y-%j}.txt", now); + } + Logging::File.open(File, std::fstream::out); + + if (Logging::File.fail()) { + throw std::runtime_error("FAILED! Could not open or create the file: " + + File + "!\n" + strerror(errno)); + } +} + +void Logging::Log(const std::string &message, const std::string &position, + Logging::LogLevel severity, bool assertion) { + if (assertion) [[likely]] { + std::string output = + std::format("[{}@{}] {}\n", + Logging::LogLevelToString[static_cast(severity)], + position, message); + + std::print("{}", output); + if (Logging::File.is_open()) { + Logging::File.write(output.c_str(), output.size()); + Logging::File.flush(); // Terrible but necessary sadly + } + + if (severity == Logging::LogLevel::Critical) { + throw std::runtime_error(output); + } + } +} diff --git a/sourceCode/Systems/Random.cpp b/sourceCode/Systems/Implementation/Random.cpp similarity index 97% rename from sourceCode/Systems/Random.cpp rename to sourceCode/Systems/Implementation/Random.cpp index cbce031..1d36cd3 100644 --- a/sourceCode/Systems/Random.cpp +++ b/sourceCode/Systems/Implementation/Random.cpp @@ -6,7 +6,7 @@ * v. 2.0. If a copy of the MPL was not distributed with this file, You can * obtain one at http://mozilla.org/MPL/2.0/. */ -#include "Random.hpp" +#include "../Random.hpp" #include #include #include diff --git a/sourceCode/Systems/Logging.hpp b/sourceCode/Systems/Logging.hpp new file mode 100644 index 0000000..093618d --- /dev/null +++ b/sourceCode/Systems/Logging.hpp @@ -0,0 +1,34 @@ +/* + * SPDX-FileCopyrightText: Dora "cat" + * SPDX-License-Identifier: MPL-2.0 + * + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include +#include +#include + +namespace Tourmaline::Systems { +class Logging { +public: + enum class LogLevel { + Critical = 0, + Error = 1, + Info = 2, + Debug = 3, + Trace = 4 + }; + + static void LogToFile(std::string File = ""); + static void Log(const std::string &message, + const std::string &position = "Unknown", + LogLevel severity = LogLevel::Info, bool assertion = true); + +private: + static std::fstream File; + static std::array LogLevelToString; +}; +} // namespace Tourmaline::Systems diff --git a/sourceCode/Types/UUID.cpp b/sourceCode/Types/UUID.cpp index 2bffc70..2d157e9 100644 --- a/sourceCode/Types/UUID.cpp +++ b/sourceCode/Types/UUID.cpp @@ -25,7 +25,7 @@ UUID::UUID(const UUID &uuid) { } UUID &UUID::operator=(const UUID &uuid) { - if (this != &uuid) { + if (this != &uuid) [[likely]] { std::memcpy(data.get(), uuid.data.get(), UUID::ByteLength); } @@ -33,7 +33,7 @@ UUID &UUID::operator=(const UUID &uuid) { } UUID &UUID::operator=(UUID &&uuid) { - if (this != &uuid) { + if (this != &uuid) [[likely]] { data.swap(uuid.data); }