this is insane but sure
This commit is contained in:
45
src/log.cpp
45
src/log.cpp
@@ -1,5 +1,7 @@
|
||||
#include "log.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <iterator>
|
||||
#include <print>
|
||||
|
||||
#include "sdl.h"
|
||||
@@ -9,6 +11,37 @@ using namespace anm2ed::util;
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
namespace
|
||||
{
|
||||
bool ensure_utf8_bom(const std::filesystem::path& path)
|
||||
{
|
||||
std::error_code ec{};
|
||||
if (!std::filesystem::exists(path, ec)) return false;
|
||||
|
||||
std::ifstream existing(path, std::ios::binary);
|
||||
if (!existing) return false;
|
||||
|
||||
char bom[3]{};
|
||||
existing.read(bom, sizeof(bom));
|
||||
auto bytesRead = existing.gcount();
|
||||
auto hasBom = bytesRead == sizeof(bom) && bom[0] == '\xEF' && bom[1] == '\xBB' && bom[2] == '\xBF';
|
||||
if (hasBom) return true;
|
||||
|
||||
existing.clear();
|
||||
existing.seekg(0, std::ios::beg);
|
||||
std::string contents((std::istreambuf_iterator<char>(existing)), std::istreambuf_iterator<char>());
|
||||
existing.close();
|
||||
|
||||
std::ofstream output(path, std::ios::binary | std::ios::trunc);
|
||||
if (!output) return false;
|
||||
output << "\xEF\xBB\xBF";
|
||||
output.write(contents.data(), static_cast<std::streamsize>(contents.size()));
|
||||
output.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::write_raw(const std::string& message)
|
||||
{
|
||||
std::println("{}", message);
|
||||
@@ -26,7 +59,17 @@ namespace anm2ed
|
||||
void Logger::error(const std::string& message) { write(ERROR, message); }
|
||||
void Logger::fatal(const std::string& message) { write(FATAL, message); }
|
||||
void Logger::command(const std::string& message) { write(COMMAND, message); }
|
||||
void Logger::open(const std::filesystem::path& path) { file.open(path, std::ios::out | std::ios::app); }
|
||||
void Logger::open(const std::filesystem::path& path)
|
||||
{
|
||||
std::error_code ec{};
|
||||
auto exists = std::filesystem::exists(path, ec);
|
||||
if (exists) ensure_utf8_bom(path);
|
||||
|
||||
file.open(path, std::ios::out | std::ios::app);
|
||||
if (!file.is_open()) return;
|
||||
|
||||
if (!exists) file << "\xEF\xBB\xBF";
|
||||
}
|
||||
|
||||
std::filesystem::path Logger::path() { return sdl::preferences_directory_get() / "log.txt"; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user