mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 21:47:40 -05:00
sys: Log to a file when ImHex wasn't opened though a terminal
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
namespace hex {
|
||||
|
||||
File::File(const fs::path &path, Mode mode) : m_path(path) {
|
||||
File::File(const fs::path &path, Mode mode) noexcept : m_path(path) {
|
||||
if (mode == File::Mode::Read)
|
||||
this->m_file = fopen64(path.string().c_str(), "rb");
|
||||
else if (mode == File::Mode::Write)
|
||||
@@ -13,7 +13,7 @@ namespace hex {
|
||||
this->m_file = fopen64(path.string().c_str(), "w+b");
|
||||
}
|
||||
|
||||
File::File() {
|
||||
File::File() noexcept {
|
||||
this->m_file = nullptr;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,16 @@ namespace hex {
|
||||
this->close();
|
||||
}
|
||||
|
||||
File& File::operator=(File &&other) noexcept {
|
||||
this->m_file = other.m_file;
|
||||
other.m_file = nullptr;
|
||||
|
||||
this->m_path = std::move(other.m_path);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
void File::seek(u64 offset) {
|
||||
fseeko64(this->m_file, offset, SEEK_SET);
|
||||
}
|
||||
|
||||
33
lib/libimhex/source/helpers/logger.cpp
Normal file
33
lib/libimhex/source/helpers/logger.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <hex/helpers/logger.hpp>
|
||||
#include <hex/helpers/file.hpp>
|
||||
#include <hex/helpers/paths.hpp>
|
||||
#include <hex/helpers/fmt.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace hex::log {
|
||||
|
||||
static File g_loggerFile;
|
||||
|
||||
FILE* getDestination() {
|
||||
if (g_loggerFile.isValid())
|
||||
return g_loggerFile.getHandle();
|
||||
else
|
||||
return stdout;
|
||||
}
|
||||
|
||||
bool isRedirected() {
|
||||
return g_loggerFile.isValid();
|
||||
}
|
||||
|
||||
void redirectToFile() {
|
||||
if (g_loggerFile.isValid()) return;
|
||||
|
||||
for (const auto &path : hex::getPath(ImHexPath::Logs)) {
|
||||
g_loggerFile = File(path / hex::format("{0:%Y%m%d_%H%M%S}.log", fmt::localtime(std::chrono::system_clock::now())), File::Mode::Create);
|
||||
|
||||
if (g_loggerFile.isValid()) break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -98,6 +98,11 @@ namespace hex {
|
||||
return (path / "constants").string();
|
||||
});
|
||||
break;
|
||||
case ImHexPath::Logs:
|
||||
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path){
|
||||
return (path / "logs").string();
|
||||
});
|
||||
break;
|
||||
default: __builtin_unreachable();
|
||||
}
|
||||
#elif defined(OS_MACOS)
|
||||
@@ -137,6 +142,9 @@ namespace hex {
|
||||
case ImHexPath::Constants:
|
||||
result.push_back((applicationSupportDir / "constants").string());
|
||||
break;
|
||||
case ImHexPath::Logs:
|
||||
result.push_back((applicationSupportDir / "logs").string());
|
||||
break;
|
||||
default: __builtin_unreachable();
|
||||
}
|
||||
#else
|
||||
@@ -191,6 +199,10 @@ namespace hex {
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "constants").string(); });
|
||||
break;
|
||||
case ImHexPath::Logs:
|
||||
std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result),
|
||||
[](auto p) { return (p / "logs").string(); });
|
||||
break;
|
||||
default: __builtin_unreachable();
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user