From 68cc9d1853431ca954b5d3fdb7f8eaa499f66a88 Mon Sep 17 00:00:00 2001 From: "Kueffner, Maximilian" Date: Mon, 17 Mar 2025 18:50:31 +0100 Subject: [PATCH] default logger init --- CMakeLists.txt | 7 +-- lib/CMakeLists.txt | 1 + lib/rendering/CMakeLists.txt | 2 - lib/utilities/CMakeLists.txt | 12 +++++ lib/utilities/ILog.hpp | 14 ++---- lib/utilities/SpdLogger.cpp | 35 ++++++++++++++ lib/utilities/SpdLogger.hpp | 25 ++++++---- src/AppGLFW.cpp | 90 +++++++++++++++++++++++++++++++++++- src/AppGLFW.hpp | 4 ++ src/main.cpp | 15 +++++- 10 files changed, 179 insertions(+), 26 deletions(-) create mode 100644 lib/utilities/CMakeLists.txt create mode 100644 lib/utilities/SpdLogger.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 97cafb6..b6e4874 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,12 +53,14 @@ add_executable(${PROJECT_NAME} ${SRC}) message(STATUS "Linking " pixelariumimagelib) target_link_libraries(${PROJECT_NAME} PRIVATE pixelariumimagelib - PRIVATE pixelariumrenderlib) + PRIVATE pixelariumrenderlib + PRIVATE pixelariumutilslib) target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/src PUBLIC ${PROJECT_SOURCE_DIR}/lib PUBLIC ${PROJECT_SOURCE_DIR}/lib/imaging + PUBLIC ${spdlog_DIR}/include PUBLIC ${imgui_DIR} PUBLIC ${imgui_DIR}/backends PUBLIC ${glfw_INCLUDE_DIR} @@ -71,8 +73,7 @@ if(WIN32) target_link_libraries(${PROJECT_NAME} PRIVATE opengl32.lib PRIVATE glfw - PRIVATE OpenGL::GL - PRIVATE spdlog::spdlog_header_only) + PRIVATE OpenGL::GL) endif() if(LINUX) target_link_libraries(${PROJECT_NAME} diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 9480cb7..201197c 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,2 +1,3 @@ +add_subdirectory(utilities) add_subdirectory(imaging) add_subdirectory(rendering) \ No newline at end of file diff --git a/lib/rendering/CMakeLists.txt b/lib/rendering/CMakeLists.txt index 486adb3..ccb0063 100644 --- a/lib/rendering/CMakeLists.txt +++ b/lib/rendering/CMakeLists.txt @@ -1,5 +1,3 @@ -set(RENDERSRC) - set(RENDERLIBNAME pixelariumrenderlib) set(RENDERLIBSRC diff --git a/lib/utilities/CMakeLists.txt b/lib/utilities/CMakeLists.txt new file mode 100644 index 0000000..ce56512 --- /dev/null +++ b/lib/utilities/CMakeLists.txt @@ -0,0 +1,12 @@ +set(UTILSLIBNAME pixelariumutilslib) + +set(UTILSLIBSRC + SpdLogger.cpp) + +add_library(${UTILSLIBNAME} STATIC ${UTILSLIBSRC}) + +target_include_directories(${UTILSLIBNAME} + PRIVATE ${spdlog_DIR}/include) + +target_link_libraries(${UTILSLIBNAME} + PRIVATE spdlog::spdlog_header_only) \ No newline at end of file diff --git a/lib/utilities/ILog.hpp b/lib/utilities/ILog.hpp index b754505..6e8539a 100644 --- a/lib/utilities/ILog.hpp +++ b/lib/utilities/ILog.hpp @@ -6,16 +6,12 @@ namespace pixelarium::utils::log class ILog { public: - virtual void Info(const std::string& msg, - const std::string& source = "") = 0; - virtual void Debug(const std::string& msg, - const std::string& source = "") = 0; - virtual void Warn(const std::string& msg, - const std::string& source = "") = 0; - virtual void Error(const std::string& msg, - const std::string& source = "") = 0; + virtual void Info(const std::string& msg) = 0; + virtual void Debug(const std::string& msg) = 0; + virtual void Warn(const std::string& msg) = 0; + virtual void Error(const std::string& msg) = 0; virtual ~ILog() {} }; -} // namespace pixelarium::utils \ No newline at end of file +} // namespace pixelarium::utils::log \ No newline at end of file diff --git a/lib/utilities/SpdLogger.cpp b/lib/utilities/SpdLogger.cpp new file mode 100644 index 0000000..17165c4 --- /dev/null +++ b/lib/utilities/SpdLogger.cpp @@ -0,0 +1,35 @@ +#include "SpdLogger.hpp" + +#include +#include +#include +#include +#include + + +using namespace pixelarium::utils::log; + +SpdLogger::SpdLogger(const std::string& file_sink, const std::string& name) + : _logger(spdlog::basic_logger_mt(file_sink, name)), _file(file_sink), _name(name) +{ + spdlog::set_default_logger(this->_logger); + spdlog::flush_on(spdlog::level::info); + _logger->info("Logger initiated"); +} + +void SpdLogger::Info(const std::string& msg) +{ + this->_logger->info(msg); +} +void SpdLogger::Debug(const std::string& msg) +{ + this->_logger->debug(msg); +} +void SpdLogger::Warn(const std::string& msg) +{ + this->_logger->warn(msg); +} +void SpdLogger::Error(const std::string& msg) +{ + this->_logger->error(msg); +} \ No newline at end of file diff --git a/lib/utilities/SpdLogger.hpp b/lib/utilities/SpdLogger.hpp index bcf9b5f..cb57206 100644 --- a/lib/utilities/SpdLogger.hpp +++ b/lib/utilities/SpdLogger.hpp @@ -1,19 +1,26 @@ #pragma once +#include +#include +#include + +#include + #include "ILog.hpp" namespace pixelarium::utils::log { class SpdLogger : public ILog { public: - SpdLogger(const std::string& file_sink); + explicit SpdLogger(const std::string& file_sink, const std::string& name); - void Info(const std::string& msg, - const std::string& source = "") override; - void Debug(const std::string& msg, - const std::string& source = "") override; - void Warn(const std::string& msg, - const std::string& source = "") override; - void Error(const std::string& msg, - const std::string& source = "") override; + void Info(const std::string& msg) override; + void Debug(const std::string& msg) override; + void Warn(const std::string& msg) override; + void Error(const std::string& msg) override; + + private: + std::shared_ptr _logger; + std::string _file; + std::string _name; }; } // namespace pixelarium::utils::log \ No newline at end of file diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index 3710a7d..4123b83 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -32,6 +32,91 @@ using namespace pixelarium::imaging; return ImVec2(img.GetImage().cols * fact, img.GetImage().rows * fact); } +pixelarium::ui::AppGLFW::AppGLFW(std::unique_ptr& log) : _logger(log.get()) +{ + glfwSetErrorCallback(glfw_error_callback); + if (!glfwInit()) + { + return; + } + + // Decide GL+GLSL versions +#if defined(IMGUI_IMPL_OPENGL_ES2) + // GL ES 2.0 + GLSL 100 + const char* glsl_version = "#version 100"; + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); +#elif defined(__APPLE__) + // GL 3.2 + GLSL 150 + const char* glsl_version = "#version 150"; + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac +#else + // GL 3.0 + GLSL 130 +#ifdef __linux__ + const char* glsl_version = "#version 130"; +#else + const char* glsl_version = reinterpret_cast("#version 130"); +#endif + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ + // only glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only +#endif + + // int count; + // GLFWmonitor** monitors = glfwGetMonitors(&count); // at [0] is always the + // main monitor GLFWmonitor* monitor = monitors[1]; + GLFWmonitor* monitor = glfwGetPrimaryMonitor(); + int xpos, ypos, width, height; + glfwGetMonitorWorkarea(monitor, &xpos, &ypos, &width, &height); + + // lg::Logger::Info("screen width " + std::to_string(width) + + // " screen heigth " + std::to_string(height)); + + // Create window with graphics context + window = glfwCreateWindow(1200, 800, PIXELARIUM_TITLE, nullptr, nullptr); + if (window == nullptr) + { + // lg::Logger::Error("no window"); + return; + } + + glfwMakeContextCurrent(window); + glfwSwapInterval(1); // Enable vsync + + // Setup Dear ImGui context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); + (void)io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking + io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform + // Windows + // io.ConfigViewportsNoAutoMerge = true; + // io.ConfigViewportsNoTaskBarIcon = true; + + // Setup Dear ImGui style + ImGui::StyleColorsDark(); + + ImGuiStyle& style = ImGui::GetStyle(); + + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + style.WindowRounding = 0.0f; + style.Colors[ImGuiCol_WindowBg].w = 1.0f; + } + + // Setup Platform/Renderer backends + ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplOpenGL3_Init(glsl_version); +} + pixelarium::ui::AppGLFW::AppGLFW() { glfwSetErrorCallback(glfw_error_callback); @@ -219,7 +304,10 @@ void pixelarium::ui::AppGLFW::LoadImageProt() { // lg::Logger::Debug("Adding image from " + std::string(p), // __FUNCTION__); - + if (this->_logger) + { + this->_logger->Warn("Creating image"); + } // this->_img = Image(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 f648929..6afcbbe 100644 --- a/src/AppGLFW.hpp +++ b/src/AppGLFW.hpp @@ -3,10 +3,12 @@ #include #include +#include #include "Image.hpp" #include "imgui.h" #include "rendering/CvMatRender.hpp" +#include "utilities/ILog.hpp" namespace pixelarium::ui { @@ -26,6 +28,7 @@ class AppGLFW { public: AppGLFW(); + AppGLFW(std::unique_ptr& log); int Run(); private: @@ -34,6 +37,7 @@ class AppGLFW private: // LogLevelSelection log_level_ = static_cast(0); + utils::log::ILog* _logger; GLFWwindow* window = nullptr; ImGuiWindowFlags window_flags = 0; std::shared_ptr _img; diff --git a/src/main.cpp b/src/main.cpp index 113630e..d287020 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,22 @@ #include +#include #include "AppGLFW.hpp" +#include "utilities/ILog.hpp" +#include "utilities/SpdLogger.hpp" int main(int argc, char** argv) { - std::cout << "ok\n"; - auto app = pixelarium::ui::AppGLFW(); + using namespace pixelarium::utils::log; + using namespace std; + cout << "ok\n"; + unique_ptr logger = make_unique(string(getenv("APPDATA")) + "/pixelarium/logfile.log", "default"); + + auto app = pixelarium::ui::AppGLFW(logger); + // auto app = pixelarium::ui::AppGLFW(); + + logger->Info("Starting Application"); + logger->Error("Starting Application"); return app.Run(); } \ No newline at end of file