Added Logging and tweaked Systems
This commit is contained in:
60
sourceCode/Systems/Implementation/Logging.cpp
Normal file
60
sourceCode/Systems/Implementation/Logging.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
|
||||
* 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 <cerrno>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <print>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
using namespace Tourmaline::Systems;
|
||||
|
||||
// This is what happens when it takes you 50 years to implement
|
||||
// reflections to a language
|
||||
std::array<const std::string, 5> 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<size_t>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
26
sourceCode/Systems/Implementation/Random.cpp
Normal file
26
sourceCode/Systems/Implementation/Random.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
|
||||
* 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 "../Random.hpp"
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
#include <ctime>
|
||||
|
||||
using namespace Tourmaline::Systems;
|
||||
|
||||
Xoshiro::Xoshiro256PP Random::generator(static_cast<uint64_t>(time(NULL)));
|
||||
Tourmaline::Type::UUID Random::GenerateUUID() {
|
||||
uint64_t random_ab = generator(), random_c = generator(), hold = 0;
|
||||
hold = std::rotr(random_ab, 12);
|
||||
hold = hold >> 4;
|
||||
hold = (hold << 4) + 0b0100;
|
||||
random_ab = std::rotl(hold, 12);
|
||||
random_c = ((random_c >> 2) << 2) + 2;
|
||||
|
||||
return Tourmaline::Type::UUID(random_ab, random_c);
|
||||
}
|
||||
Reference in New Issue
Block a user