engine/include/logger.hpp

53 lines
1.4 KiB
C++
Raw Normal View History

2023-04-29 14:22:25 +00:00
#ifndef ENGINE_INCLUDE_LOGGER_H_
#define ENGINE_INCLUDE_LOGGER_H_
2022-09-13 18:25:18 +00:00
#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 {
2023-04-29 14:22:25 +00:00
// To be executed in the target application, NOT engine.dll
void SetupLog(const char* appName) {
2022-09-13 18:25:18 +00:00
2023-04-29 14:22:25 +00:00
const std::string LOG_FILENAME{ std::string(appName) + ".log"};
2022-09-13 18:25:18 +00:00
#ifdef NDEBUG
2023-04-29 14:22:25 +00:00
// RELEASE
const std::filesystem::path log_path{
std::filesystem::temp_directory_path() / LOG_FILENAME};
2022-09-13 18:25:18 +00:00
#else
2023-04-29 14:22:25 +00:00
// DEBUG
const std::filesystem::path log_path{ LOG_FILENAME };
2022-09-13 18:25:18 +00:00
#endif
2023-04-29 14:22:25 +00:00
std::vector<spdlog::sink_ptr> sinks;
2022-09-13 18:25:18 +00:00
2023-04-29 14:22:25 +00:00
sinks.emplace_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>(
log_path.string(), true));
sinks.back()->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] %v");
2022-09-13 18:25:18 +00:00
2023-04-29 14:22:25 +00:00
sinks.emplace_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
sinks.back()->set_pattern("[%H:%M:%S.%e] [%l] %v");
2022-09-13 18:25:18 +00:00
2023-04-29 14:22:25 +00:00
auto logger = std::make_shared<spdlog::logger>(appName, sinks.begin(),
sinks.end());
2022-09-13 18:25:18 +00:00
2023-04-29 14:22:25 +00:00
// Logs below INFO are ignored through macros in release (see log.hpp)
logger->set_level(spdlog::level::trace);
2022-09-13 18:25:18 +00:00
2023-04-29 14:22:25 +00:00
spdlog::register_logger(logger);
spdlog::set_default_logger(logger);
spdlog::flush_every(std::chrono::seconds(60));
2022-09-13 18:25:18 +00:00
2023-04-29 14:22:25 +00:00
LOG_INFO("Created log with path: {}", log_path.string());
2022-09-13 18:25:18 +00:00
}
2023-04-29 14:22:25 +00:00
} // namespace engine
#endif