sys: Log to a file when ImHex wasn't opened though a terminal

This commit is contained in:
WerWolv
2022-01-17 20:06:00 +01:00
parent 2df4e22bf8
commit 8701e0f402
15 changed files with 155 additions and 42 deletions

View File

@@ -14,7 +14,7 @@ namespace hex {
constexpr auto GetPluginDescriptionSymbol = "_ZN3hex6plugin{0}{1}8internal20getPluginDescriptionEv";
constexpr auto SetImGuiContextSymbol = "_ZN3hex6plugin{0}{1}8internal15setImGuiContextEP12ImGuiContext";
Plugin::Plugin(const fs::path &path) {
Plugin::Plugin(const fs::path &path) : m_path(path) {
this->m_handle = dlopen(path.string().c_str(), RTLD_LAZY);
if (this->m_handle == nullptr) {
@@ -33,6 +33,8 @@ namespace hex {
Plugin::Plugin(Plugin &&other) noexcept {
this->m_handle = other.m_handle;
this->m_path = std::move(other.m_path);
this->m_initializePluginFunction = other.m_initializePluginFunction;
this->m_getPluginNameFunction = other.m_getPluginNameFunction;
this->m_getPluginAuthorFunction = other.m_getPluginAuthorFunction;
@@ -52,9 +54,13 @@ namespace hex {
dlclose(this->m_handle);
}
void Plugin::initializePlugin() const {
if (this->m_initializePluginFunction != nullptr)
bool Plugin::initializePlugin() const {
if (this->m_initializePluginFunction != nullptr) {
this->m_initializePluginFunction();
return true;
} else {
return false;
}
}
std::string Plugin::getPluginName() const {
@@ -83,6 +89,12 @@ namespace hex {
this->m_setImGuiContextFunction(ctx);
}
const fs::path &Plugin::getPath() const {
return this->m_path;
}
bool PluginManager::load(const fs::path &pluginFolder) {
if (!fs::exists(pluginFolder))
return false;

View File

@@ -49,7 +49,8 @@ namespace hex::init {
try {
status = task() && status;
} catch (...) {
} catch (std::exception &e) {
log::error("Init task {} threw an exception: {}", name, e.what());
status = false;
}

View File

@@ -57,7 +57,7 @@ namespace hex::init {
bool createDirectories() {
bool result = true;
std::array paths = {
constexpr std::array paths = {
ImHexPath::Patterns,
ImHexPath::PatternsInclude,
ImHexPath::Magic,
@@ -66,7 +66,8 @@ namespace hex::init {
ImHexPath::Config,
ImHexPath::Constants,
ImHexPath::Yara,
ImHexPath::Python
ImHexPath::Python,
ImHexPath::Logs
};
for (auto path : paths) {
@@ -225,7 +226,8 @@ namespace hex::init {
}
for (const auto &plugin : PluginManager::getPlugins()) {
plugin.initializePlugin();
if (!plugin.initializePlugin())
log::error("Failed to initialize plugin {}", plugin.getPath().filename().string());
}
return true;

View File

@@ -3,13 +3,18 @@
#if defined(OS_LINUX)
#include <hex/helpers/utils.hpp>
#include <hex/helpers/logger.hpp>
#include <nlohmann/json.hpp>
#include <sys/wait.h>
#include <unistd.h>
namespace hex {
void Window::initNative() {
if (!isatty(STDOUT_FILENO)) {
log::redirectToFile();
}
}
void Window::setupNativeWindow() {

View File

@@ -2,12 +2,17 @@
#if defined(OS_MACOS)
#include <hex/helpers/logger.hpp>
#include <nlohmann/json.hpp>
#include <unistd.h>
namespace hex {
void Window::initNative() {
if (!isatty(STDOUT_FILENO)) {
log::redirectToFile();
}
}
void Window::setupNativeWindow() {

View File

@@ -172,25 +172,26 @@
// Redirect cin, cout and cerr to that console
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONERR$", "w", stderr);
freopen("CONOUT$", "w", stderr);
setvbuf(stdin, nullptr, _IONBF, 0);
setvbuf(stdout, nullptr, _IONBF, 0);
setvbuf(stderr, nullptr, _IONBF, 0);
fmt::print("\n");
}
// Enable color format specifiers in console
{
auto hConsole = ::GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsole != INVALID_HANDLE_VALUE) {
DWORD mode = 0;
if (::GetConsoleMode(hConsole, &mode)) {
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
::SetConsoleMode(hConsole, mode);
// Enable color format specifiers in console
{
auto hConsole = ::GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsole != INVALID_HANDLE_VALUE) {
DWORD mode = 0;
if (::GetConsoleMode(hConsole, &mode)) {
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
::SetConsoleMode(hConsole, mode);
}
}
}
} else {
log::redirectToFile();
}
// Open new files in already existing ImHex instance