const std::string& to std::string_view and static output buffer on Log

This commit is contained in:
2026-02-26 20:34:23 +02:00
parent 3d17902844
commit 4eb846f215
2 changed files with 14 additions and 8 deletions

View File

@@ -10,7 +10,7 @@
#define GUARD_TOURMALINE_LOGGING_H
#include <array>
#include <fstream>
#include <string>
#include <string_view>
namespace Tourmaline::Systems {
class Logging {
@@ -25,8 +25,8 @@ public:
};
static void LogToFile(std::string File = "");
static void Log(const std::string &message,
const std::string &position = "Unknown",
static void Log(std::string_view message,
std::string_view position = "Unknown",
LogLevel severity = LogLevel::Info, bool assertion = true);
private:

View File

@@ -7,7 +7,7 @@
* obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <Systems/Logging.hpp>
#include "../../headers/Systems/Logging.hpp"
#include <cerrno>
#include <chrono>
@@ -16,9 +16,11 @@
#include <exception>
#include <format>
#include <fstream>
#include <iterator>
#include <print>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
using namespace Tourmaline::Systems;
@@ -47,13 +49,15 @@ void Logging::LogToFile(std::string File) {
}
}
void Logging::Log(const std::string &message, const std::string &position,
void Logging::Log(std::string_view message, std::string_view position,
Logging::LogLevel severity, bool assertion) {
if (assertion) [[likely]] {
auto loglevelData =
static std::string
output; // This is done to stop allocations per std::format
const auto &loglevelData =
Logging::LogLevelToString[static_cast<size_t>(severity)];
std::string output =
std::format("[{}@{}] {}\n", loglevelData.first, position, message);
std::format_to(std::back_inserter(output), "[{}@{}] {}\n",
loglevelData.first, position, message);
std::print("\033{} {}\033[0m", loglevelData.second, output);
if (Logging::File.is_open()) {
@@ -68,5 +72,7 @@ void Logging::Log(const std::string &message, const std::string &position,
if (severity == Logging::LogLevel::Critical) {
std::terminate();
}
output.clear();
}
}