Skip to content

Commit

Permalink
Preparing advanced logging system
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrno committed Jun 21, 2024
1 parent d551b34 commit 6473146
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 42 deletions.
26 changes: 12 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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}/)
DESTINATION ${INSTALL_BIN_DIR}/)
8 changes: 5 additions & 3 deletions build/in_system/build.sh
Original file line number Diff line number Diff line change
@@ -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
39 changes: 24 additions & 15 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"(
Expand Down Expand Up @@ -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)
Expand All @@ -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();

Expand Down Expand Up @@ -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); });
Expand Down Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions main/include/vortex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -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()
Expand All @@ -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);

Expand Down Expand Up @@ -231,6 +235,10 @@ namespace VortexMaker

VORTEX_API std::string getCurrentTimeStamp();

VORTEX_API std::shared_ptr<spdlog::logger> CreateLogPool(const std::string &pool_name);
VORTEX_API std::shared_ptr<spdlog::logger> CreateGlobalLogger();
VORTEX_API std::shared_ptr<spdlog::logger> CreateConsoleLogger();

VORTEX_API std::vector<std::string> SearchFiles(const std::string &path, const std::string &filename);
VORTEX_API std::vector<std::string> SearchFiles(const std::string &path, const std::string &filename, int recursions);
VORTEX_API std::vector<std::string> SearchSystemFiles(const std::string &path, const std::string &filename);
Expand Down
8 changes: 8 additions & 0 deletions main/include/vortex_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,17 @@ struct VxPaths
//-----------------------------------------------------------------------------
struct VxContext
{
// Master flags
bool initialized;

// Loger
bool logger;
bool logger_registering = true;
std::shared_ptr<spdlog::logger> global_logger;
std::shared_ptr<spdlog::logger> console_logger;
std::vector<std::pair<std::string, std::shared_ptr<spdlog::logger>>> pool_loggers;

// Components
VxIO IO;
SessionState state;
VortexMakerDebugAllocInfo debugAllocInfo;
Expand Down
51 changes: 50 additions & 1 deletion main/src/vortex/logger/logger.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
#include "../../../include/vortex.h"
#include "../../../include/vortex_internals.h"


VORTEX_API std::shared_ptr<spdlog::logger> VortexMaker::CreateLogPool(const std::string &pool_name)
{
return nullptr;
}

VORTEX_API std::shared_ptr<spdlog::logger> 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<spdlog::logger> 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<VxSystemLog> log = std::make_shared<VxSystemLog>(spdlog::level::level_enum::info, scope, message);
log->m_timestamp = VortexMaker::getCurrentTimeStamp();
ctx.registered_logs.push_back(log);
}
}
}
}
}

/**
* @brief LogInfo logs an informational message.
*
Expand All @@ -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<VxSystemLog> log = std::make_shared<VxSystemLog>(spdlog::level::level_enum::info, scope, message);
Expand Down
14 changes: 14 additions & 0 deletions main/src/vortex/session/create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions tools/crash_handler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# CMakeLists.txt pour le crash_handler
cmake_minimum_required(VERSION 3.16)
project(crash_handler)

Expand All @@ -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
Expand Down Expand Up @@ -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)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/../../lib/uikit/platform/GUI/editor/icon.png DESTINATION /usr/include/vortex)
2 changes: 0 additions & 2 deletions tools/crash_handler/app/core/Crash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,7 @@ void Crash::OnImGuiRender(const std::string &parent, std::function<void(ImGuiWin
static std::string core_dumped_file = VortexMaker::getHomeDirectory() + "/.vx/sessions/" + this->ctx->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;
Expand Down

0 comments on commit 6473146

Please sign in to comment.