From 95e06b2f6d87b597a52029dbfa9896f4bd8ca74b Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sun, 17 Mar 2024 15:04:35 +0100 Subject: logging --- build_win32.bat | 4 ++-- src/config.cpp | 4 +++- src/export.cpp | 11 ++++++++--- src/fonts.cpp | 6 +++++- src/fonts.h | 28 +++++++++++++++++++--------- src/image.cpp | 3 ++- src/import.cpp | 9 ++++++++- src/logging.cpp | 3 +++ src/logging.h | 32 ++++++++++++++++++++++++++++++++ src/main.cpp | 29 ++++++++++++++++++++++++++--- src/search.cpp | 7 +++++++ src/windows/main_windows.cpp | 5 +++-- 12 files changed, 118 insertions(+), 23 deletions(-) create mode 100644 src/logging.cpp create mode 100644 src/logging.h diff --git a/build_win32.bat b/build_win32.bat index ad001e0..001154e 100644 --- a/build_win32.bat +++ b/build_win32.bat @@ -19,5 +19,5 @@ if "%1"=="-release" ( mkdir %OUT_DIR% cl /std:c++17 /nologo %FLAGS% /W3 /MD /EHsc /Isrc/windows /external:W0 /external:Iimgui /external:Iimgui/backends /Isrc /utf-8 %INCLUDES% /D UNICODE /D _UNICODE %SOURCES% /Fe%OUT_DIR%/%OUT_EXE%.exe /Fd%OUT_DIR%/vc140.pdb /Fo%OUT_DIR%/ /link %LIBS% -if "%1"=="-r" call "bin/debug/text-search.exe" -if "%1"=="-d" call devenv "bin/debug/text-search.exe" +if "%1"=="-r" call "%OUT_DIR%/%OUT_EXE%.exe" +if "%1"=="-d" call devenv "%OUT_DIR%/%OUT_EXE%.exe" diff --git a/src/config.cpp b/src/config.cpp index 4c5533d..51d45a8 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,7 +1,7 @@ #include "config.h" #include "../imgui/imgui_internal.h" #include "search.h" - +#include "logging.h" #include #include #include @@ -72,4 +72,6 @@ void ts_load_config() { ini_handler.ApplyAllFn = _ts_config_ApplyAll; ini_handler.WriteAllFn = _ts_config_WriteAll; ImGui::AddSettingsHandler(&ini_handler); + + TS_LOG_TRACE("Loaded config"); } \ No newline at end of file diff --git a/src/export.cpp b/src/export.cpp index b020b77..22e8956 100644 --- a/src/export.cpp +++ b/src/export.cpp @@ -4,6 +4,7 @@ #include "../imfiledialog/ImFileDialog.h" #include #include +#include "logging.h" export_result last_export_result = EXPORT_NONE; @@ -249,16 +250,20 @@ struct t_export_thread_args { static void* _ts_export_thread(void* args) { struct t_export_thread_args* arg = (struct t_export_thread_args*)args; + bool result = false; if (ts_str_has_extension(arg->path, ".json")) { - _ts_export_json(arg->result, arg->path); + result = _ts_export_json(arg->result, arg->path); } if (ts_str_has_extension(arg->path, ".csv")) { - _ts_export_csv(arg->result, arg->path); + result = _ts_export_csv(arg->result, arg->path); } if (ts_str_has_extension(arg->path, ".xml")) { - _ts_export_xml(arg->result, arg->path); + result = _ts_export_xml(arg->result, arg->path); } + if (!result) TS_LOG_TRACE("Export failed: cound not open file %s", arg->path) + else TS_LOG_TRACE("Export completed: %s", arg->path); + arg->result->is_saving = false; free(arg); diff --git a/src/fonts.cpp b/src/fonts.cpp index 34df8f4..2fff05f 100644 --- a/src/fonts.cpp +++ b/src/fonts.cpp @@ -5,6 +5,7 @@ #include "../fonts/NotoSerifTC.h" #include "../fonts/NotoSansSC.h" #include "../fonts/NotoSansThai.h" +#include "logging.h" #include "imgui.h" #include @@ -42,6 +43,8 @@ ts_font_range ts_locale_to_range(wchar_t* buffer) { if (wcscmp(buffer, L"th-TH") == 0) result = FONT_RANGE_THAI; if (wcscmp(buffer, L"vi-VN") == 0) result = FONT_RANGE_VIETNAMESE; + + TS_LOG_TRACE("Loaded locale: %ls, alphabet: %s", buffer, FONT_RANGE_STRING[result]); return result; } @@ -126,5 +129,6 @@ void ts_load_fonts(float size, ts_font_range locale) { size, &config, io.Fonts->GetGlyphRangesVietnamese()); } - io.Fonts->Build(); + io.Fonts->Build(); + TS_LOG_TRACE("Loaded fonts"); } \ No newline at end of file diff --git a/src/fonts.h b/src/fonts.h index ef2a23d..82fc324 100644 --- a/src/fonts.h +++ b/src/fonts.h @@ -2,17 +2,27 @@ #include +#define FOREACH_FONT_RANGE(FONT_RANGE) \ + FONT_RANGE(FONT_RANGE_ENGLISH) \ + FONT_RANGE(FONT_RANGE_GREEK) \ + FONT_RANGE(FONT_RANGE_KOREAN) \ + FONT_RANGE(FONT_RANGE_JAPANESE) \ + FONT_RANGE(FONT_RANGE_CHINESE_FULL) \ + FONT_RANGE(FONT_RANGE_CHINESE_SIMPLE) \ + FONT_RANGE(FONT_RANGE_CYRILLIC) \ + FONT_RANGE(FONT_RANGE_THAI) \ + FONT_RANGE(FONT_RANGE_VIETNAMESE) \ + +#define GENERATE_ENUM(ENUM) ENUM, +#define GENERATE_STRING(STRING) #STRING, + typedef enum t_ts_font_range { - FONT_RANGE_ENGLISH, - FONT_RANGE_GREEK, - FONT_RANGE_KOREAN, - FONT_RANGE_JAPANESE, - FONT_RANGE_CHINESE_FULL, - FONT_RANGE_CHINESE_SIMPLE, - FONT_RANGE_CYRILLIC, - FONT_RANGE_THAI, - FONT_RANGE_VIETNAMESE, + FOREACH_FONT_RANGE(GENERATE_ENUM) } ts_font_range; +static const char *FONT_RANGE_STRING[] = { + FOREACH_FONT_RANGE(GENERATE_STRING) +}; + void ts_load_fonts(float size, ts_font_range locale); ts_font_range ts_locale_to_range(wchar_t* locale); \ No newline at end of file diff --git a/src/image.cpp b/src/image.cpp index 6f87eda..1fa551e 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -1,7 +1,7 @@ #include "image.h" #include "definitions.h" #include "../imfiledialog/ImFileDialog.h" - +#include "logging.h" #define STB_IMAGE_IMPLEMENTATION #include "../stb_image.h" @@ -88,4 +88,5 @@ void ts_load_images() { GLuint texID = (GLuint)(intptr_t)tex; glDeleteTextures(1, &texID); }; + TS_LOG_TRACE("Loaded images"); } \ No newline at end of file diff --git a/src/import.cpp b/src/import.cpp index 254050d..ed3df4d 100644 --- a/src/import.cpp +++ b/src/import.cpp @@ -2,7 +2,7 @@ #include "search.h" #include "export.h" #include "../imfiledialog/ImFileDialog.h" - +#include "logging.h" #include #include #include @@ -131,6 +131,13 @@ static void* _ts_import_thread(void* args) { last_import_result = _ts_import_csv(arg->result, arg->path); } + switch(last_import_result) { + case IMPORT_NONE: TS_LOG_TRACE("Import success: %s", arg->path); break; + case IMPORT_FILE_ERROR: TS_LOG_TRACE("Import error: could not open file %s", arg->path); break; + case IMPORT_INVALID_VERSION: TS_LOG_TRACE("Import error: invalid version %s", arg->path); break; + case IMPORT_INVALID_DATA: TS_LOG_TRACE("Import error: invalid data %s", arg->path); break; + } + arg->result->done_finding_files = true; arg->result->search_completed = true; diff --git a/src/logging.cpp b/src/logging.cpp new file mode 100644 index 0000000..e00b373 --- /dev/null +++ b/src/logging.cpp @@ -0,0 +1,3 @@ +#include "logging.h" + +ts_log logger = {0}; \ No newline at end of file diff --git a/src/logging.h b/src/logging.h new file mode 100644 index 0000000..625004d --- /dev/null +++ b/src/logging.h @@ -0,0 +1,32 @@ +#pragma once + +#include "array.h" +#include "platform.h" +#include "../utf8.h" + +#define LOG_ENTRY_SIZE 120 + +typedef struct t_ts_log +{ + ts_array entries; + uint64_t start_time; +} ts_log; + +extern ts_log logger; + +#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) + +#define TS_LOG_TRACE(__format, ...) { \ + if (logger.entries.data == NULL) { \ + logger.start_time = ts_platform_get_time(); \ + logger.entries = ts_array_create(LOG_ENTRY_SIZE); \ + ts_array_reserve(&logger.entries, 50); \ + logger.entries.reserve_jump = 50; \ + } \ + utf8_int8_t __tmp[LOG_ENTRY_SIZE]; \ + utf8_int8_t* __buffer = (utf8_int8_t*)malloc(LOG_ENTRY_SIZE); \ + memset(__buffer, 0, LOG_ENTRY_SIZE); \ + snprintf(__tmp, LOG_ENTRY_SIZE, "[%10.3f] %s", ts_platform_get_time(logger.start_time)/1000.0f, __format); \ + snprintf(__buffer, LOG_ENTRY_SIZE, __tmp, __VA_ARGS__); \ + ts_array_push(&logger.entries, __buffer); \ +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 0828859..a6e8dba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,12 +11,13 @@ #include "config.h" #include "export.h" #include "import.h" - +#include "logging.h" #include // Popups bool open_settings_window = false; bool open_about_window = false; +bool open_log_window = false; const char* help_text = "1. Search directory\n" @@ -28,8 +29,25 @@ const char* help_text = "3. Text to search\n" " - Supports wildcards '*' & '?' in text\n"; -static void _ts_create_popups() { +static void _ts_create_popups(int window_w, int window_h) { ImGuiIO& io = ImGui::GetIO(); + + if (open_log_window) { + ImGui::OpenPopup("Text-Search Log"); + ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f), ImGuiCond_Always, ImVec2(0.5f,0.5f)); + } + + // Settings window + if (ImGui::BeginPopupModal("Text-Search Log", &open_log_window, ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove)) { + ImGui::SetWindowSize({window_w*0.8f, window_h*0.8f}); + for (uint32_t i = 0; i < logger.entries.length; i++) { + utf8_int8_t* entry = (utf8_int8_t*)ts_array_at(&logger.entries, i); + ImGui::Text("%s", entry); + } + + ImGui::EndPopup(); + } + if (open_settings_window) { ImGui::OpenPopup("Text-Search settings"); ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f), ImGuiCond_Always, ImVec2(0.5f,0.5f)); @@ -164,6 +182,9 @@ static int _ts_create_menu(int window_w, int window_h) { if (ImGui::MenuItem("Settings")) { open_settings_window = true; } + if (ImGui::MenuItem("Log")) { + open_log_window = true; + } if (ImGui::MenuItem("About")) { open_about_window = true; } @@ -177,7 +198,7 @@ static int _ts_create_menu(int window_w, int window_h) { ImGui::PopStyleVar(); ImGui::PopStyleColor(); - _ts_create_popups(); + _ts_create_popups(window_w, window_h); ts_create_import_popup(window_w, window_h); ts_create_export_popup(window_w, window_h); @@ -185,6 +206,8 @@ static int _ts_create_menu(int window_w, int window_h) { } void ts_init() { + TS_LOG_TRACE("Program started"); + memset(save_path, 0, sizeof(save_path)); memset(path_buffer, 0, sizeof(path_buffer)); memset(filter_buffer, 0, sizeof(filter_buffer)); diff --git a/src/search.cpp b/src/search.cpp index 35e9a23..3622f28 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -1,6 +1,7 @@ #include "search.h" #include "platform.h" #include "config.h" +#include "logging.h" #include ts_search_result *current_search_result = NULL; @@ -374,6 +375,8 @@ static void *_ts_list_files_thread(void *args) ts_platform_list_files_block(info, NULL); info->done_finding_files = true; + TS_LOG_TRACE("Search done listing files: %p", info); + // Use this thread to cleanup previous result. if (info->prev_result) { while (!info->prev_result->search_completed || info->prev_result->is_saving) { @@ -388,6 +391,8 @@ static void *_ts_list_files_thread(void *args) if (info->completed_match_threads == info->max_ts_thread_count) { info->search_completed = true; // No memory is written after this point. info->timestamp = ts_platform_get_time(info->timestamp); + + TS_LOG_TRACE("Search completed: %p", info); } ts_thread_sleep(10); } @@ -407,6 +412,7 @@ void ts_start_search(utf8_int8_t *path, utf8_int8_t *filter, utf8_int8_t *query, return; } + if (current_search_result) { current_search_result->cancel_search = true; @@ -420,6 +426,7 @@ void ts_start_search(utf8_int8_t *path, utf8_int8_t *filter, utf8_int8_t *query, new_result->max_ts_thread_count = thread_count; new_result->max_file_size = max_fs; new_result->respect_capitalization = case_sensitive; + TS_LOG_TRACE("Search started: %p %s -> %s", new_result, path, query); if (utf8len(query) == 0) { new_result->search_text = NULL; diff --git a/src/windows/main_windows.cpp b/src/windows/main_windows.cpp index becb1c1..c68f326 100644 --- a/src/windows/main_windows.cpp +++ b/src/windows/main_windows.cpp @@ -12,6 +12,7 @@ #include "image.h" #include "config.h" #include "fonts.h" +#include "logging.h" #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif @@ -118,10 +119,12 @@ ts_font_range _ts_get_font_range_to_load() { int main(int, char**) { + if (OleInitialize(NULL) != S_OK) { return -1; } + QueryPerformanceFrequency(&Frequency); ts_init(); // Create application window @@ -161,8 +164,6 @@ int main(int, char**) ImGui_ImplWin32_InitForOpenGL(hwnd); ImGui_ImplOpenGL3_Init(); - QueryPerformanceFrequency(&Frequency); - ts_load_fonts(18.0f, _ts_get_font_range_to_load()); ts_load_images(); ts_load_config(); -- cgit v1.2.3-70-g09d2