const std::string& to std::string_view and static output buffer on Log
This commit is contained in:
@@ -10,7 +10,7 @@
|
|||||||
#define GUARD_TOURMALINE_LOGGING_H
|
#define GUARD_TOURMALINE_LOGGING_H
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string_view>
|
||||||
|
|
||||||
namespace Tourmaline::Systems {
|
namespace Tourmaline::Systems {
|
||||||
class Logging {
|
class Logging {
|
||||||
@@ -25,8 +25,8 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
static void LogToFile(std::string File = "");
|
static void LogToFile(std::string File = "");
|
||||||
static void Log(const std::string &message,
|
static void Log(std::string_view message,
|
||||||
const std::string &position = "Unknown",
|
std::string_view position = "Unknown",
|
||||||
LogLevel severity = LogLevel::Info, bool assertion = true);
|
LogLevel severity = LogLevel::Info, bool assertion = true);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <Systems/Logging.hpp>
|
#include "../../headers/Systems/Logging.hpp"
|
||||||
|
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@@ -16,9 +16,11 @@
|
|||||||
#include <exception>
|
#include <exception>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iterator>
|
||||||
#include <print>
|
#include <print>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
using namespace Tourmaline::Systems;
|
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) {
|
Logging::LogLevel severity, bool assertion) {
|
||||||
if (assertion) [[likely]] {
|
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)];
|
Logging::LogLevelToString[static_cast<size_t>(severity)];
|
||||||
std::string output =
|
std::format_to(std::back_inserter(output), "[{}@{}] {}\n",
|
||||||
std::format("[{}@{}] {}\n", loglevelData.first, position, message);
|
loglevelData.first, position, message);
|
||||||
|
|
||||||
std::print("\033{} {}\033[0m", loglevelData.second, output);
|
std::print("\033{} {}\033[0m", loglevelData.second, output);
|
||||||
if (Logging::File.is_open()) {
|
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) {
|
if (severity == Logging::LogLevel::Critical) {
|
||||||
std::terminate();
|
std::terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user