Switched to Corrade's string over STL
This commit is contained in:
@@ -8,9 +8,12 @@
|
||||
*/
|
||||
|
||||
#include "Systems/Logging.hpp"
|
||||
#include "Corrade/Containers/Array.h"
|
||||
|
||||
#include <Corrade/Tags.h>
|
||||
#include "Corrade/Containers/String.h"
|
||||
#include "Corrade/Containers/StringView.h"
|
||||
#include "Corrade/Tags.h"
|
||||
#include "Corrade/Utility/Format.h"
|
||||
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
@@ -18,65 +21,63 @@
|
||||
#include <exception>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <print>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
using namespace Tourmaline::Systems;
|
||||
using namespace Corrade::Containers;
|
||||
using namespace Corrade::Utility;
|
||||
|
||||
// This is what happens when it takes you 50 years to implement
|
||||
// reflections to a language
|
||||
Array<std::pair<const std::string, const std::string>>
|
||||
Logging::LogLevelToString{Corrade::InPlaceInit,
|
||||
{std::pair{"Critical", "[0;31m"},
|
||||
{"Error", "[0;91m"},
|
||||
{"Warning", "[0;33m"},
|
||||
{"Info", "[0;37m"},
|
||||
{"Debug", "[0;92m"},
|
||||
{"Trace", "[0;36m"}}};
|
||||
const char *Logging::LogLevelToColour[Logging::LogLevel::Trace + 1]{
|
||||
"[0;31m", "[0;91m", "[0;33m", "[0;37m", "[0;92m", "[0;36m"};
|
||||
const char *Logging::LogLevelToString[Logging::LogLevel::Trace + 1]{
|
||||
"Critical", "Error", "Warning", "Info", "Debug", "Trace"};
|
||||
std::fstream Logging::File;
|
||||
|
||||
void Logging::LogToFile(std::string File) {
|
||||
void Logging::LogToFile(String File) {
|
||||
if (File == "") {
|
||||
const auto now = std::chrono::system_clock::now();
|
||||
File = std::format("Tourmaline-{:%Y-%j}.txt", now);
|
||||
std::chrono::year_month_day ymd{std::chrono::floor<std::chrono::days>(now)};
|
||||
File = String{Corrade::ValueInit, 128};
|
||||
formatInto(File, "Tourmaline-{}-{}-{}.txt", static_cast<int>(ymd.year()),
|
||||
static_cast<unsigned>(ymd.month()),
|
||||
static_cast<unsigned>(ymd.day()));
|
||||
}
|
||||
Logging::File.open(File, std::fstream::out);
|
||||
Logging::File.open(File.data(), std::fstream::out);
|
||||
|
||||
if (Logging::File.fail()) {
|
||||
throw std::runtime_error("FAILED! Could not open or create the file: " +
|
||||
File + "!\n" + strerror(errno));
|
||||
String error =
|
||||
format("FAILED! Could not open or create the file: {}! Error: {}", File,
|
||||
strerror(errno));
|
||||
throw std::runtime_error(error.data());
|
||||
}
|
||||
}
|
||||
|
||||
void Logging::Log(std::string_view message, std::string_view position,
|
||||
void Logging::Log(StringView message, StringView position,
|
||||
Logging::LogLevel severity, bool assertion) {
|
||||
if (assertion) [[likely]] {
|
||||
static std::string
|
||||
output; // This is done to stop allocations per std::format
|
||||
const auto &loglevelData =
|
||||
Logging::LogLevelToString[static_cast<size_t>(severity)];
|
||||
std::format_to(std::back_inserter(output), "[{}@{}] {}\n",
|
||||
loglevelData.first, position, message);
|
||||
if (assertion) {
|
||||
static String output{Corrade::ValueInit,
|
||||
4096}; // This is done to stop allocations
|
||||
std::size_t formattedSize = formatInto(
|
||||
output, "[{}@{}] {}\n", LogLevelToString[severity], position, message);
|
||||
|
||||
std::print("\033{} {}\033[0m", loglevelData.second, output);
|
||||
std::print(
|
||||
"\033{} {}\033[0m", LogLevelToColour[severity],
|
||||
std::string_view{output.begin(), output.begin() + formattedSize});
|
||||
if (Logging::File.is_open()) {
|
||||
Logging::File.write(output.c_str(), output.size());
|
||||
Logging::File.write(output.data(), formattedSize);
|
||||
Logging::File.flush(); // Terrible but necessary sadly
|
||||
}
|
||||
|
||||
if (severity == Logging::LogLevel::Error) {
|
||||
throw std::runtime_error(output);
|
||||
throw std::runtime_error(output.data());
|
||||
}
|
||||
|
||||
if (severity == Logging::LogLevel::Critical) {
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
output.clear();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user