From 64731467dccb9d6594e6d48279815dc9500192d5 Mon Sep 17 00:00:00 2001 From: "Diego E. Moreno" Date: Fri, 21 Jun 2024 23:21:27 +0200 Subject: [PATCH] Preparing advanced logging system --- CMakeLists.txt | 26 ++++++------- build/in_system/build.sh | 8 ++-- main.cpp | 39 ++++++++++++-------- main/include/vortex.h | 14 +++++-- main/include/vortex_internals.h | 8 ++++ main/src/vortex/logger/logger.cpp | 51 +++++++++++++++++++++++++- main/src/vortex/session/create.cpp | 14 +++++++ tools/crash_handler/CMakeLists.txt | 6 +-- tools/crash_handler/app/core/Crash.cpp | 2 - 9 files changed, 126 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f68a763..6d71c92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,30 +1,26 @@ +# CMakeLists.txt pour le projet principal cmake_minimum_required(VERSION 3.16) project(vortex) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fPIC") set(VORTEX_VERSION "1.1") # Liste des fichiers sources pour la bibliothèque partagée file(GLOB_RECURSE VORTEX_SHARED_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main/src/*.cpp") -file(GLOB_RECURSE VORTEXEDITOR_SHARED_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tools/editor/*.cpp") -file(GLOB_RECURSE VORTEXEDITOR_SHARED_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tools/launcher/*.cpp") -file(GLOB_RECURSE VORTEXEDITOR_SHARED_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tools/crash_handler/*.cpp") # Création de la bibliothèque partagée -add_library(vortex_shared SHARED -${VORTEX_SHARED_SOURCES} -${VORTEXEDITOR_SHARED_SOURCES} -) +add_library(vortex_shared SHARED ${VORTEX_SHARED_SOURCES}) # Définir les chemins d'inclusion pour la bibliothèque partagée target_include_directories(vortex_shared PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/main/include ${CMAKE_CURRENT_SOURCE_DIR}/main/src - ${CMAKE_CURRENT_SOURCE_DIR}/tools/editor ) -# Liste des fichiers sources pour l'exécutable principal -file(GLOB_RECURSE VORTEX_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main/src/*.cpp") +# Ajouter des dépendances supplémentaires à la bibliothèque partagée si nécessaire # Add editor add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/editor editor_build) @@ -38,13 +34,15 @@ target_link_libraries(vortex_shared PUBLIC crash_handler) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/launcher launcher_build) target_link_libraries(vortex_shared PUBLIC launcher) +# Liste des fichiers sources pour l'exécutable principal +file(GLOB_RECURSE VORTEX_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main/src/*.cpp") + # Création de l'exécutable principal add_executable(vortex ${VORTEX_SOURCES} main.cpp) +target_link_libraries(vortex PRIVATE vortex_shared) add_executable(vx ${VORTEX_SOURCES} bootstrapp.cpp) - -# Liaison avec la bibliothèque partagée -target_link_libraries(vortex PRIVATE vortex_shared) +target_link_libraries(vx PRIVATE vortex_shared) # Ajouter d'autres dépendances ou configurations pour l'exécutable principal @@ -93,4 +91,4 @@ install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/vortex_desktop.desktop DESTINATION /usr/share/applications/) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/build/handle_crash.sh - DESTINATION ${INSTALL_BIN_DIR}/) \ No newline at end of file + DESTINATION ${INSTALL_BIN_DIR}/) diff --git a/build/in_system/build.sh b/build/in_system/build.sh index a61305b..4317b38 100644 --- a/build/in_system/build.sh +++ b/build/in_system/build.sh @@ -1,7 +1,9 @@ mkdir build_spdlog mkdir build -cd build_spdlog && cmake ../../../lib/spdlog && make install -cd ../build && cmake ../../.. && make install +cd build_spdlog && cmake ../../../lib/spdlog && make -j$(nproc) install +cd ../build && cmake ../../.. && make -j$(nproc) install + +#TODO Embed spdlog into main CMakeLists.txt + -#TODO Embed spdlog into main CMakeLists.txt \ No newline at end of file diff --git a/main.cpp b/main.cpp index 9a6cd2b..fcc4e76 100644 --- a/main.cpp +++ b/main.cpp @@ -12,6 +12,8 @@ #include "./main/include/templates/load.h" #include "./lib/uikit/src/EntryPoint.h" +static std::string session_id = "unknow"; + void PrintInfinite() { std::cout << R"( @@ -107,7 +109,15 @@ VxContext *InitRuntime(bool logger) { std::cout << "Initializing runtime..." << std::endl; VxContext *ctx = VortexMaker::CreateContext(); + + ctx->state.session_id = session_id; + + VortexMaker::CreateGlobalLogger(); + VortexMaker::CreateConsoleLogger(); ctx->logger = logger; + + VortexMaker::CreateSessionTopic(ctx->state.session_id); + std::ifstream file("vortex.config"); if (file) @@ -131,8 +141,15 @@ VxContext *InitRuntime(bool logger) VxContext *InitBlankRuntime(bool logger) { VxContext *ctx = VortexMaker::CreateContext(); + + ctx->state.session_id = session_id; + + VortexMaker::CreateGlobalLogger(); + VortexMaker::CreateConsoleLogger(); VortexMaker::LogInfo("Bootstrapp", "Initializing runtime..."); + VortexMaker::CreateSessionTopic(ctx->state.session_id); + // Initialize environment VortexMaker::InitEnvironment(); @@ -168,21 +185,19 @@ int main(int argc, char *argv[]) else if (std::string(argv[1]) == "-crash" || std::string(argv[1]) == "--get-last-crash") { PrintHeader(); - InitBlankRuntime(true); - VortexMaker::LogInfo("Bootstrapp", "Opening the graphical interface..."); - - VxContext *ctx = VortexMaker::GetCurrentContext(); - std::string session_id; if (argc > 2) { std::string arg2 = argv[2]; if (arg2.rfind("--session_id=", 0) == 0) { session_id = arg2.substr(13); - VxContext *ctx = VortexMaker::GetCurrentContext(); - ctx->state.session_id = session_id; } } + InitBlankRuntime(true); + VortexMaker::LogInfo("Bootstrapp", "Opening the graphical interface..."); + + VxContext *ctx = VortexMaker::GetCurrentContext(); + std::thread receiveThread; std::thread Thread([&]() { VortexMaker::VortexCrashHandler(argc, argv); }); @@ -261,21 +276,15 @@ int main(int argc, char *argv[]) else if (std::string(argv[1]) == "-l" || std::string(argv[1]) == "--launcher") { PrintHeader(); - InitBlankRuntime(true); - VortexMaker::LogInfo("Bootstrapp", "Opening the graphical interface..."); - - std::string session_id; if (argc > 2) { std::string arg2 = argv[2]; if (arg2.rfind("--session_id=", 0) == 0) { session_id = arg2.substr(13); - VxContext *ctx = VortexMaker::GetCurrentContext(); - ctx->state.session_id = session_id; - - VortexMaker::CreateSessionTopic(ctx->state.session_id); } } + InitBlankRuntime(true); + VortexMaker::LogInfo("Bootstrapp", "Opening the graphical interface..."); std::thread receiveThread; try diff --git a/main/include/vortex.h b/main/include/vortex.h index 8800306..89d497a 100644 --- a/main/include/vortex.h +++ b/main/include/vortex.h @@ -73,6 +73,10 @@ namespace fs = std::filesystem; #include "../../lib/uikit/lib/stb-image/stb_image.h" #include "../../lib/uikit/lib/glfw/include/GLFW/glfw3.h" #include "../../lib/spdlog/include/spdlog/spdlog.h" +#include "../../lib/spdlog/include/spdlog/sinks/stdout_color_sinks.h" +#include "../../lib/spdlog/include/spdlog/sinks/basic_file_sink.h" +#include "../../lib/spdlog/include/spdlog/sinks/rotating_file_sink.h" +#include "../../lib/spdlog/include/spdlog/sinks/daily_file_sink.h" #include "../../lib/json/single_include/nlohmann/json.hpp" @@ -148,9 +152,6 @@ struct Task; struct VxContext; //_____________________________________________________________________________ -typedef int VortexMakerChannel_Props; -typedef int VortexMakerMatrix_Props; - // Callback and functions types typedef void *(*VortexMakerMemAllocFunc)(size_t sz, void *user_data); // Function signature for VortexMaker::SetAllocatorFunctions() typedef void (*VortexMakerMemFreeFunc)(void *ptr, void *user_data); // Function signature for VortexMaker::SetAllocatorFunctions() @@ -176,6 +177,9 @@ namespace VortexMaker VORTEX_API void LogInfo(const std::string &scope, const std::string &message); #define VXINFO(scope, message) LogInfo(scope, message); + VORTEX_API void LogInfo(const std::string &pool_name, const std::string &scope, const std::string &message); +#define VXPOOLINFO(pool_name, scope, message) LogInfo(pool_name, scope, message); + VORTEX_API void LogWarn(const std::string &scope, const std::string &message); #define VXWARN(scope, message) LogWarn(scope, message); @@ -231,6 +235,10 @@ namespace VortexMaker VORTEX_API std::string getCurrentTimeStamp(); + VORTEX_API std::shared_ptr CreateLogPool(const std::string &pool_name); + VORTEX_API std::shared_ptr CreateGlobalLogger(); + VORTEX_API std::shared_ptr CreateConsoleLogger(); + VORTEX_API std::vector SearchFiles(const std::string &path, const std::string &filename); VORTEX_API std::vector SearchFiles(const std::string &path, const std::string &filename, int recursions); VORTEX_API std::vector SearchSystemFiles(const std::string &path, const std::string &filename); diff --git a/main/include/vortex_internals.h b/main/include/vortex_internals.h index be9b899..2b73200 100644 --- a/main/include/vortex_internals.h +++ b/main/include/vortex_internals.h @@ -184,9 +184,17 @@ struct VxPaths //----------------------------------------------------------------------------- struct VxContext { + // Master flags bool initialized; + + // Loger bool logger; bool logger_registering = true; + std::shared_ptr global_logger; + std::shared_ptr console_logger; + std::vector>> pool_loggers; + + // Components VxIO IO; SessionState state; VortexMakerDebugAllocInfo debugAllocInfo; diff --git a/main/src/vortex/logger/logger.cpp b/main/src/vortex/logger/logger.cpp index 0272b26..a649ed8 100644 --- a/main/src/vortex/logger/logger.cpp +++ b/main/src/vortex/logger/logger.cpp @@ -1,6 +1,50 @@ #include "../../../include/vortex.h" #include "../../../include/vortex_internals.h" + +VORTEX_API std::shared_ptr VortexMaker::CreateLogPool(const std::string &pool_name) +{ + return nullptr; +} + +VORTEX_API std::shared_ptr VortexMaker::CreateGlobalLogger() +{ + VxContext &ctx = *CVortexMaker; + std::string file_path = VortexMaker::getHomeDirectory() + "/.vx/sessions/" + ctx.state.session_id + "/logs/global.log"; + ctx.global_logger = spdlog::basic_logger_mt("global_logger", file_path); + ctx.global_logger->flush_on(spdlog::level::info); + return nullptr; +} + +VORTEX_API std::shared_ptr VortexMaker::CreateConsoleLogger() +{ + VxContext &ctx = *CVortexMaker; + ctx.console_logger = spdlog::stdout_color_mt("console"); + return nullptr; +} + +VORTEX_API void VortexMaker::LogInfo(const std::string &pool_name, const std::string &scope, const std::string &message) +{ + VxContext &ctx = *CVortexMaker; + if (ctx.logger) + { + for (auto pool : ctx.pool_loggers) + { + if (pool.first == pool_name) + { + spdlog::info("[" + scope + "] : " + message); + + if (ctx.logger_registering) + { + std::shared_ptr log = std::make_shared(spdlog::level::level_enum::info, scope, message); + log->m_timestamp = VortexMaker::getCurrentTimeStamp(); + ctx.registered_logs.push_back(log); + } + } + } + } +} + /** * @brief LogInfo logs an informational message. * @@ -12,7 +56,12 @@ VORTEX_API void VortexMaker::LogInfo(const std::string& scope, const std::string& message){ VxContext &ctx = *CVortexMaker; if(ctx.logger){ - spdlog::info("[" + scope + "] : "+ message); + + // Ajout du nom du thread au message pour le logger global + ctx.global_logger->info("[{}] {}", "global", "[" + scope + "] : "+ message); + + // Également logguer dans le logger global (console) + ctx.console_logger->info("[" + scope + "] : "+ message); if(ctx.logger_registering){ std::shared_ptr log = std::make_shared(spdlog::level::level_enum::info, scope, message); diff --git a/main/src/vortex/session/create.cpp b/main/src/vortex/session/create.cpp index 1f19a9d..00ff14a 100644 --- a/main/src/vortex/session/create.cpp +++ b/main/src/vortex/session/create.cpp @@ -31,6 +31,20 @@ VORTEX_API void VortexMaker::CreateSessionTopic(const std::string &post_topic) } } + std::string log_path = topic_path + "/logs"; + + { + std::string cmd = "mkdir " + log_path; + if (std::system(cmd.c_str()) == 0) + { + VortexMaker::LogInfo("Core", "Session logs folder of \"" + post_topic + "\" is created with success !"); + } + else + { + VortexMaker::LogError("Core", "Failed to create session logs folder of \"" + post_topic + "\"!"); + } + } + { std::string cmd = "touch " + crash_path + "/core_dump.txt"; if (std::system(cmd.c_str()) == 0) diff --git a/tools/crash_handler/CMakeLists.txt b/tools/crash_handler/CMakeLists.txt index 44270ad..ce981ca 100644 --- a/tools/crash_handler/CMakeLists.txt +++ b/tools/crash_handler/CMakeLists.txt @@ -1,3 +1,4 @@ +# CMakeLists.txt pour le crash_handler cmake_minimum_required(VERSION 3.16) project(crash_handler) @@ -8,9 +9,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) add_library(crash_handler - # API - ${CMAKE_CURRENT_SOURCE_DIR}/../../main/src/vortex.cpp - # crash_handler requirements ${CMAKE_CURRENT_SOURCE_DIR}/../../lib/uikit/platform/GUI/editor/ImGui/ImGuiTheme.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../lib/uikit/platform/GUI/editor/UI/UI.cpp @@ -44,4 +42,4 @@ target_link_libraries(crash_handler PUBLIC stbimage) target_link_libraries(crash_handler PUBLIC spdlog) endif() -install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/../../lib/uikit/platform/GUI/editor/icon.png DESTINATION /usr/include/vortex) \ No newline at end of file +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/../../lib/uikit/platform/GUI/editor/icon.png DESTINATION /usr/include/vortex) diff --git a/tools/crash_handler/app/core/Crash.cpp b/tools/crash_handler/app/core/Crash.cpp index a1f79ce..0690775 100644 --- a/tools/crash_handler/app/core/Crash.cpp +++ b/tools/crash_handler/app/core/Crash.cpp @@ -277,9 +277,7 @@ void Crash::OnImGuiRender(const std::string &parent, std::functionctx->state.session_id + "/crash/core_dumped.txt"; if (!loadFileToString(core_dumped_file, text, bufferSize)) { // Handle error opening or reading the file - ImGui::Begin("Error"); ImGui::Text("Failed to open or read file: %s", core_dumped_file.c_str()); - ImGui::End(); } static ImGuiInputTextFlags flags = ImGuiInputTextFlags_AllowTabInput | ImGuiInputTextFlags_ReadOnly;