Move the log creation code to a header file

This commit is contained in:
Bailey Harrison 2022-09-04 00:36:17 +01:00
parent 0743cce845
commit 18751a3728
2 changed files with 48 additions and 1 deletions

View File

@ -34,6 +34,8 @@ add_library(${PROJECT_NAME} SHARED
"include/export.h"
"include/engine.hpp"
"include/log.hpp"
"include/window.hpp"
@ -80,7 +82,7 @@ target_include_directories(${PROJECT_NAME} PRIVATE src)
# Pass some project information into the source code
configure_file(config.h.in config.h)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
# libraries:

45
include/engine.hpp Normal file
View File

@ -0,0 +1,45 @@
#pragma once
#include <filesystem>
#include "log.hpp"
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>
namespace engine {
// To be executed in the target application, NOT engine.dll
void setupLog(const char* appName)
{
const std::string LOG_FILENAME{ std::string(appName) + ".log"};
#ifdef NDEBUG
// RELEASE
const std::filesystem::path log_path{ std::filesystem::temp_directory_path() / LOG_FILENAME };
#else
// DEBUG
const std::filesystem::path log_path{ LOG_FILENAME };
#endif
std::vector<spdlog::sink_ptr> sinks;
sinks.emplace_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>(log_path.string(), true));
#ifndef NDEBUG
// DEBUG
sinks.emplace_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
#endif
auto logger = std::make_shared<spdlog::logger>("sdltest", sinks.begin(), sinks.end());
logger->set_level(spdlog::level::trace);
spdlog::register_logger(logger);
spdlog::set_default_logger(logger);
INFO("Created log with path: {}", log_path.string());
}
}