default logger init

This commit is contained in:
Kueffner, Maximilian
2025-03-17 18:50:31 +01:00
parent ee39b254b4
commit 68cc9d1853
10 changed files with 179 additions and 26 deletions
+4 -3
View File
@@ -53,12 +53,14 @@ add_executable(${PROJECT_NAME} ${SRC})
message(STATUS "Linking " pixelariumimagelib) message(STATUS "Linking " pixelariumimagelib)
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
PRIVATE pixelariumimagelib PRIVATE pixelariumimagelib
PRIVATE pixelariumrenderlib) PRIVATE pixelariumrenderlib
PRIVATE pixelariumutilslib)
target_include_directories(${PROJECT_NAME} target_include_directories(${PROJECT_NAME}
PUBLIC ${PROJECT_SOURCE_DIR}/src PUBLIC ${PROJECT_SOURCE_DIR}/src
PUBLIC ${PROJECT_SOURCE_DIR}/lib PUBLIC ${PROJECT_SOURCE_DIR}/lib
PUBLIC ${PROJECT_SOURCE_DIR}/lib/imaging PUBLIC ${PROJECT_SOURCE_DIR}/lib/imaging
PUBLIC ${spdlog_DIR}/include
PUBLIC ${imgui_DIR} PUBLIC ${imgui_DIR}
PUBLIC ${imgui_DIR}/backends PUBLIC ${imgui_DIR}/backends
PUBLIC ${glfw_INCLUDE_DIR} PUBLIC ${glfw_INCLUDE_DIR}
@@ -71,8 +73,7 @@ if(WIN32)
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
PRIVATE opengl32.lib PRIVATE opengl32.lib
PRIVATE glfw PRIVATE glfw
PRIVATE OpenGL::GL PRIVATE OpenGL::GL)
PRIVATE spdlog::spdlog_header_only)
endif() endif()
if(LINUX) if(LINUX)
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
+1
View File
@@ -1,2 +1,3 @@
add_subdirectory(utilities)
add_subdirectory(imaging) add_subdirectory(imaging)
add_subdirectory(rendering) add_subdirectory(rendering)
-2
View File
@@ -1,5 +1,3 @@
set(RENDERSRC)
set(RENDERLIBNAME pixelariumrenderlib) set(RENDERLIBNAME pixelariumrenderlib)
set(RENDERLIBSRC set(RENDERLIBSRC
+12
View File
@@ -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)
+5 -9
View File
@@ -6,16 +6,12 @@ namespace pixelarium::utils::log
class ILog class ILog
{ {
public: public:
virtual void Info(const std::string& msg, virtual void Info(const std::string& msg) = 0;
const std::string& source = "") = 0; virtual void Debug(const std::string& msg) = 0;
virtual void Debug(const std::string& msg, virtual void Warn(const std::string& msg) = 0;
const std::string& source = "") = 0; virtual void Error(const std::string& msg) = 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 ~ILog() {} virtual ~ILog() {}
}; };
} // namespace pixelarium::utils } // namespace pixelarium::utils::log
+35
View File
@@ -0,0 +1,35 @@
#include "SpdLogger.hpp"
#include <spdlog/common.h>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <memory>
#include <string>
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);
}
+16 -9
View File
@@ -1,19 +1,26 @@
#pragma once #pragma once
#include <spdlog/common.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/spdlog.h>
#include <memory>
#include "ILog.hpp" #include "ILog.hpp"
namespace pixelarium::utils::log namespace pixelarium::utils::log
{ {
class SpdLogger : public ILog class SpdLogger : public ILog
{ {
public: public:
SpdLogger(const std::string& file_sink); explicit SpdLogger(const std::string& file_sink, const std::string& name);
void Info(const std::string& msg, void Info(const std::string& msg) override;
const std::string& source = "") override; void Debug(const std::string& msg) override;
void Debug(const std::string& msg, void Warn(const std::string& msg) override;
const std::string& source = "") override; void Error(const std::string& msg) override;
void Warn(const std::string& msg,
const std::string& source = "") override; private:
void Error(const std::string& msg, std::shared_ptr<spdlog::logger> _logger;
const std::string& source = "") override; std::string _file;
std::string _name;
}; };
} // namespace pixelarium::utils::log } // namespace pixelarium::utils::log
+89 -1
View File
@@ -32,6 +32,91 @@ using namespace pixelarium::imaging;
return ImVec2(img.GetImage().cols * fact, img.GetImage().rows * fact); return ImVec2(img.GetImage().cols * fact, img.GetImage().rows * fact);
} }
pixelarium::ui::AppGLFW::AppGLFW(std::unique_ptr<utils::log::ILog>& 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<const char*>("#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() pixelarium::ui::AppGLFW::AppGLFW()
{ {
glfwSetErrorCallback(glfw_error_callback); glfwSetErrorCallback(glfw_error_callback);
@@ -219,7 +304,10 @@ void pixelarium::ui::AppGLFW::LoadImageProt()
{ {
// lg::Logger::Debug("Adding image from " + std::string(p), // lg::Logger::Debug("Adding image from " + std::string(p),
// __FUNCTION__); // __FUNCTION__);
if (this->_logger)
{
this->_logger->Warn("Creating image");
}
// this->_img = Image(p); // this->_img = Image(p);
this->_img = std::make_shared<Image>(p); this->_img = std::make_shared<Image>(p);
this->_render = pixelarium::render::CvMatRender(this->_img); this->_render = pixelarium::render::CvMatRender(this->_img);
+4
View File
@@ -3,10 +3,12 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <cstdio> #include <cstdio>
#include <memory>
#include "Image.hpp" #include "Image.hpp"
#include "imgui.h" #include "imgui.h"
#include "rendering/CvMatRender.hpp" #include "rendering/CvMatRender.hpp"
#include "utilities/ILog.hpp"
namespace pixelarium::ui namespace pixelarium::ui
{ {
@@ -26,6 +28,7 @@ class AppGLFW
{ {
public: public:
AppGLFW(); AppGLFW();
AppGLFW(std::unique_ptr<utils::log::ILog>& log);
int Run(); int Run();
private: private:
@@ -34,6 +37,7 @@ class AppGLFW
private: private:
// LogLevelSelection log_level_ = static_cast<LogLevelSelection>(0); // LogLevelSelection log_level_ = static_cast<LogLevelSelection>(0);
utils::log::ILog* _logger;
GLFWwindow* window = nullptr; GLFWwindow* window = nullptr;
ImGuiWindowFlags window_flags = 0; ImGuiWindowFlags window_flags = 0;
std::shared_ptr<pixelarium::imaging::Image> _img; std::shared_ptr<pixelarium::imaging::Image> _img;
+13 -2
View File
@@ -1,11 +1,22 @@
#include <iostream> #include <iostream>
#include <memory>
#include "AppGLFW.hpp" #include "AppGLFW.hpp"
#include "utilities/ILog.hpp"
#include "utilities/SpdLogger.hpp"
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
std::cout << "ok\n"; using namespace pixelarium::utils::log;
auto app = pixelarium::ui::AppGLFW(); using namespace std;
cout << "ok\n";
unique_ptr<ILog> logger = make_unique<SpdLogger>(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(); return app.Run();
} }