From 8388fd5d99a71e3fddba626e5b2753522c647a29 Mon Sep 17 00:00:00 2001 From: shweet Date: Fri, 15 Aug 2025 17:17:11 -0400 Subject: [PATCH] logging --- src/COMMON.h | 19 +++++++------------ src/anm2.h | 1 - src/dialog.cpp | 8 ++++---- src/dialog.h | 8 ++++++++ src/imgui.h | 2 +- src/log.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/log.h | 17 +++++++++++++++++ src/main.cpp | 2 ++ src/settings.cpp | 4 +--- src/shader.h | 2 +- src/state.cpp | 16 ++++++++++------ src/texture.h | 2 +- 12 files changed, 97 insertions(+), 29 deletions(-) create mode 100644 src/log.cpp create mode 100644 src/log.h diff --git a/src/COMMON.h b/src/COMMON.h index f4c54ab..6a598bf 100644 --- a/src/COMMON.h +++ b/src/COMMON.h @@ -44,6 +44,8 @@ typedef double f64; using namespace glm; +#define PREFERENCES_DIRECTORY "anm2ed" + #define ROUND_NEAREST_MULTIPLE(value, multiple) (roundf((value) / (multiple)) * (multiple)) #define FLOAT_TO_U8(x) (static_cast((x) * 255.0f)) #define U8_TO_FLOAT(x) ((x) / 255.0f) @@ -98,19 +100,12 @@ static const vec4 COLOR_OPAQUE = {1.0f, 1.0f, 1.0f, 1.0f}; static const vec4 COLOR_TRANSPARENT = {0.0f, 0.0f, 0.0f, 0.0f}; static const vec3 COLOR_OFFSET_NONE = {0.0f, 0.0f, 0.0f}; -static inline void log_error(const std::string& string) +static inline std::string preferences_path_get(void) { - std::println("[ERROR] {}", string); -} - -static inline void log_info(const std::string& string) -{ - std::println("[INFO] {}", string); -} - -static inline void log_warning(const std::string& string) -{ - std::println("[WARNING] {}", string); + char* preferencesPath = SDL_GetPrefPath("", PREFERENCES_DIRECTORY); + std::string preferencesPathString = preferencesPath; + SDL_free(preferencesPath); + return preferencesPathString; } static inline bool string_to_bool(const std::string& string) diff --git a/src/anm2.h b/src/anm2.h index 158a03d..aa82771 100644 --- a/src/anm2.h +++ b/src/anm2.h @@ -1,6 +1,5 @@ #pragma once -#include "COMMON.h" #include "resources.h" #define ANM2_SCALE_CONVERT(x) ((f32)x / 100.0f) diff --git a/src/dialog.cpp b/src/dialog.cpp index f2634be..f3a1449 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -73,11 +73,11 @@ void dialog_ffmpeg_path_set(Dialog* self) void dialog_explorer_open(const std::string& path) { #ifdef _WIN32 - ShellExecuteA(NULL, "open", path.c_str(), NULL, NULL, SW_SHOWNORMAL); + ShellExecuteA(NULL, DIALOG_FILE_EXPLORER_COMMAND, path.c_str(), NULL, NULL, SW_SHOWNORMAL); #else - char cmd[512]; - snprintf(cmd, sizeof(cmd), "xdg-open \"%s\" &", path.c_str()); - system(cmd); + char command[DIALOG_FILE_EXPLORER_COMMAND_SIZE]; + snprintf(command, sizeof(command), DIALOG_FILE_EXPLORER_COMMAND, path.c_str()); + system(command); #endif } diff --git a/src/dialog.h b/src/dialog.h index 11e6380..f624e17 100644 --- a/src/dialog.h +++ b/src/dialog.h @@ -3,6 +3,14 @@ #include "render.h" #include "window.h" +#define DIALOG_FILE_EXPLORER_COMMAND_SIZE 512 + +#ifdef _WIN32 +#define DIALOG_FILE_EXPLORER_COMMAND "open" +#else +#define DIALOG_FILE_EXPLORER_COMMAND "xdg-open \"%s\" &" +#endif + const SDL_DialogFileFilter DIALOG_FILE_FILTER_ANM2[] = { {"Anm2 file", "anm2;xml"} diff --git a/src/imgui.h b/src/imgui.h index 1b79cf7..58da0d4 100644 --- a/src/imgui.h +++ b/src/imgui.h @@ -207,7 +207,7 @@ struct ImguiHotkey static void imgui_log_push(Imgui* self, const std::string& text) { self->log.push_back({text, IMGUI_LOG_DURATION}); - std::println("[IMGUI] {}", text); + log_imgui(text); } static std::vector& imgui_hotkey_registry() diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 0000000..761c681 --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,45 @@ +#include "log.h" + +inline std::ofstream logFile; + +static void _log_write(const std::string& string) +{ + std::println("{}", string); + + if (logFile.is_open()) + { + logFile << string << '\n'; + logFile.flush(); + } +} + +void log_init(const std::string& file) +{ + logFile.open(file, std::ios::out | std::ios::trunc); + if (!logFile) std::println("{}", std::format(LOG_INIT_ERROR, file)); +} + +void log_error(const std::string& error) +{ + _log_write(LOG_ERROR_FORMAT + error); +} + +void log_info(const std::string& info) +{ + _log_write(LOG_INFO_FORMAT + info); +} + +void log_warning(const std::string& warning) +{ + _log_write(LOG_WARNING_FORMAT + warning); +} + +void log_imgui(const std::string& imgui) +{ + _log_write(LOG_IMGUI_FORMAT + imgui); +} + +void log_free(void) +{ + logFile.close(); +} \ No newline at end of file diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..3ef0fc6 --- /dev/null +++ b/src/log.h @@ -0,0 +1,17 @@ +#pragma once + +#include "COMMON.h" + +#define LOG_WARNING_FORMAT "[WARNING] " +#define LOG_ERROR_FORMAT "[ERROR] " +#define LOG_INFO_FORMAT "[INFO] " +#define LOG_IMGUI_FORMAT "[IMGUI] " +#define LOG_INIT_ERROR "[ERROR] Failed to open log file: {}" +#define LOG_PATH "log.txt" + +void log_init(const std::string& file); +void log_error(const std::string& error); +void log_info(const std::string& info); +void log_warning(const std::string& warning); +void log_imgui(const std::string& imgui); +void log_free(void); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ef1765f..faf69f9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,8 @@ main(s32 argc, char* argv[]) { State state; + log_init(preferences_path_get() + LOG_PATH); + if (argc > 0 && argv[1]) { if (std::string(argv[1]) == ARGUMENT_RESCALE) diff --git a/src/settings.cpp b/src/settings.cpp index 67c441e..31bdccd 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -74,9 +74,7 @@ static void _settings_setting_load(Settings* self, const std::string& line) std::string settings_path_get(void) { - char* path = SDL_GetPrefPath("", SETTINGS_FOLDER); - std::string filePath = std::string(path) + SETTINGS_PATH; - SDL_free(path); + std::string filePath = preferences_path_get() + SETTINGS_PATH; return filePath; } diff --git a/src/shader.h b/src/shader.h index 276a64c..5b34f38 100644 --- a/src/shader.h +++ b/src/shader.h @@ -1,6 +1,6 @@ #pragma once -#include "COMMON.h" +#include "log.h" #define SHADER_INFO_LOG_MAX 0xFF #define SHADER_INIT_ERROR "Failed to initialize shader {}:\n{}" diff --git a/src/state.cpp b/src/state.cpp index 0cc00e1..f628db8 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -24,9 +24,6 @@ static void _draw(State* self) void init(State* self) { - - log_info(STATE_INIT_INFO); - settings_init(&self->settings); if (!SDL_Init(SDL_INIT_VIDEO)) @@ -78,8 +75,6 @@ void init(State* self) SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); self->glContext = SDL_GL_CreateContext(self->window); - - window_vsync_set(self->settings.isVsync); if (!self->glContext) { @@ -87,10 +82,18 @@ void init(State* self) quit(self); } - glewInit(); + GLenum glewError = glewInit(); + + if (glewError != GLEW_OK) + { + log_error(std::format(STATE_GL_CONTEXT_INIT_ERROR, (const char*)glewGetErrorString(glewError))); + quit(self); + } log_info(std::format(STATE_GL_CONTEXT_INIT_INFO, (const char*)glGetString(GL_VERSION))); + window_vsync_set(self->settings.isVsync); + glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glLineWidth(STATE_GL_LINE_WIDTH); @@ -191,4 +194,5 @@ void quit(State* self) settings_save(&self->settings); log_info(STATE_QUIT_INFO); + log_free(); } \ No newline at end of file diff --git a/src/texture.h b/src/texture.h index 7553bbd..c222cf9 100644 --- a/src/texture.h +++ b/src/texture.h @@ -1,6 +1,6 @@ #pragma once -#include "COMMON.h" +#include "log.h" #define TEXTURE_CHANNELS 4 #define TEXTURE_INIT_INFO "Initialized texture from file: {}"