engine/include/logger.hpp

48 lines
1.3 KiB
C++
Raw Normal View History

2022-09-13 18:25:18 +00:00
#pragma once
#include "log.hpp"
2022-09-13 18:25:18 +00:00
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <filesystem>
#include <memory>
2022-09-13 18:25:18 +00:00
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));
2023-02-19 13:55:08 +00:00
sinks.back()->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] %v");
2022-09-13 18:25:18 +00:00
sinks.emplace_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
2023-02-19 13:55:08 +00:00
sinks.back()->set_pattern("[%H:%M:%S.%e] [%l] %v");
2022-09-13 18:25:18 +00:00
2023-01-02 17:24:20 +00:00
auto logger = std::make_shared<spdlog::logger>(appName, sinks.begin(), sinks.end());
2022-09-13 18:25:18 +00:00
2023-02-02 17:32:19 +00:00
logger->set_level(spdlog::level::trace); // Logs below INFO are ignored through macros in release (see log.hpp)
2022-09-13 18:25:18 +00:00
spdlog::register_logger(logger);
spdlog::set_default_logger(logger);
2023-02-19 13:55:08 +00:00
spdlog::flush_every(std::chrono::seconds(60));
2022-09-13 18:25:18 +00:00
LOG_INFO("Created log with path: {}", log_path.string());
2022-09-13 18:25:18 +00:00
}
}