From 2180f07ea92f2013199fc884096f9af01a9477a2 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Sat, 14 Jun 2025 16:39:39 +0200 Subject: [PATCH] logger business --- lib/utilities/SpdLogger.cpp | 11 ++++++++++- src/AppGLFW.cpp | 27 ++++++++++++++++++++++----- src/AppGLFW.hpp | 21 ++++++++++----------- src/uiresources.h.in | 10 ++++++++-- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/lib/utilities/SpdLogger.cpp b/lib/utilities/SpdLogger.cpp index b60b606..288306e 100644 --- a/lib/utilities/SpdLogger.cpp +++ b/lib/utilities/SpdLogger.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "ILog.hpp" @@ -34,29 +35,37 @@ void SpdLogger::Error(const std::string& msg) { this->logger_->error(msg); } void SpdLogger::ChangeLevel(LogLevel lvl) { + std::stringstream st{}; + st << std::format("with argument {}", static_cast(lvl)); switch (lvl) { case LogLevel::Trace: this->logger_->set_level(spdlog::level::trace); spdlog::flush_on(spdlog::level::trace); + st << "Trace"; break; case LogLevel::Info: this->logger_->set_level(spdlog::level::info); spdlog::flush_on(spdlog::level::info); + st << "Info"; break; case LogLevel::Warn: this->logger_->set_level(spdlog::level::warn); spdlog::flush_on(spdlog::level::warn); + st << "Warn"; break; case LogLevel::Error: this->logger_->set_level(spdlog::level::err); spdlog::flush_on(spdlog::level::err); + st << "Error"; break; case LogLevel::Debug: default: this->logger_->set_level(spdlog::level::debug); spdlog::flush_on(spdlog::level::debug); + st << "Debug"; } - this->logger_->debug("Changed log level;"); + // you will only get this message for log levels <= info! I.e., not for error or warning. + this->logger_->info(std::format("{}: Changed log level {}", __FUNCTION__, st.str()).c_str()); } diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index 49f0aa5..97c46b5 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -10,6 +10,7 @@ #include "portable-file-dialogs.h" #include "rendering/CvMatRender.hpp" #include "uiresources.h" +#include "utilities/ILog.hpp" using namespace pixelarium::imaging; @@ -134,7 +135,8 @@ int pixelarium::ui::AppGLFW::Run() ImGui::DockSpaceOverViewport(ImGui::GetID("Backspace")); this->MenuBar(); - + if (demop_) + ImGui::ShowDemoWindow(&this->demop_); if (this->imagep_) { // auto render = render::CvMatRender(this->_img); @@ -200,6 +202,24 @@ void pixelarium::ui::AppGLFW::MenuBar() // main menu if (ImGui::BeginMenu(MAINMENUNAME)) { + if (ImGui::BeginCombo(LOGLEVELSELECT, LOGLEVELS[log_level_])) + { + for (int n = 0; n < IM_ARRAYSIZE(LOGLEVELS); n++) + { + bool is_selected = (LOGLEVELS[log_level_] == LOGLEVELS[n]); + if (ImGui::Selectable(LOGLEVELS[n], is_selected)) + { + log_level_ = n; + this->logger_.ChangeLevel(static_cast(1 << log_level_)); + } + if (is_selected) + ImGui::SetItemDefaultFocus(); + } + ImGui::EndCombo(); + } + + ImGui::MenuItem(SHOWIMGUIDEMOS, NULL, &this->demop_); + ImGui::EndMenu(); } @@ -223,10 +243,7 @@ void pixelarium::ui::AppGLFW::LoadImageProt() auto res{pfd::open_file("Load Inputs", pfd::path::home(), {"All Files", "*"}, pfd::opt::multiselect).result()}; for (auto& p : res) { - if (this->logger_) - { - this->logger_->Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); - } + this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p)); this->img_ = std::make_shared(p); this->render_ = pixelarium::render::CvMatRender(this->img_); diff --git a/src/AppGLFW.hpp b/src/AppGLFW.hpp index 7deca27..5ff7f96 100644 --- a/src/AppGLFW.hpp +++ b/src/AppGLFW.hpp @@ -29,19 +29,16 @@ enum LogLevelSelection class AppGLFW { public: - AppGLFW() { this->InitMainWindow(); } - AppGLFW(std::unique_ptr& log) : AppGLFW() + explicit AppGLFW(std::unique_ptr& log) : logger_(*log) { - logger_ = log.get(); - if (logger_) - { - logger_->Debug(std::format("{}: Initiating a new window", __FUNCTION__).c_str()); + logger_.Debug(std::format("{}: Initiating a new window", __FUNCTION__).c_str()); - if (pool_) - { - logger_->Debug(std::format("{}: We have an image resource pool!", __FUNCTION__).c_str()); - } + if (pool_) + { + logger_.Debug(std::format("{}: We have an image resource pool!", __FUNCTION__).c_str()); } + + this->InitMainWindow(); } AppGLFW(std::unique_ptr& log, std::unique_ptr& pool) : AppGLFW(log) @@ -57,13 +54,15 @@ class AppGLFW private: // LogLevelSelection log_level_ = static_cast(0); - utils::log::ILog* logger_; + utils::log::ILog& logger_; resources::ImageResourcePool* pool_; GLFWwindow* window = nullptr; ImGuiWindowFlags window_flags_ = 0; std::shared_ptr img_; pixelarium::render::CvMatRender render_; bool imagep_{false}; + bool demop_{false}; + int log_level_{0}; ImVec2 curr_dim_; }; diff --git a/src/uiresources.h.in b/src/uiresources.h.in index 706bc9c..36ce5c2 100644 --- a/src/uiresources.h.in +++ b/src/uiresources.h.in @@ -1,8 +1,14 @@ #pragma once - /*-- Gets filled in during the cmake configuration step --*/ #cmakedefine PIXELARIUM_TITLE "@PIXELARIUM_TITLE@" #define MAINMENUNAME "Menu" -#define FILEMENUNAME "File" \ No newline at end of file +#define FILEMENUNAME "File" +#define LOGLEVELSELECT "Log Level" +#define SHOWIMGUIDEMOS "ImGui Demos" + +namespace +{ +const char* LOGLEVELS[] = {"Trace", "Debug", "Info", "Warning", "Error"}; +}