engine/include/logger.h

46 lines
1.3 KiB
C
Raw Normal View History

2024-03-31 10:11:22 +00:00
#pragma once
2022-09-13 18:25:18 +00:00
2023-04-29 14:56:49 +00:00
#include <filesystem>
#include <memory>
2023-05-01 13:13:35 +00:00
#include "log.h"
2023-05-01 12:55:49 +00:00
2022-09-13 18:25:18 +00:00
#include <spdlog/sinks/basic_file_sink.h>
2023-04-29 14:56:49 +00:00
#include <spdlog/sinks/stdout_color_sinks.h>
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
2024-03-31 10:11:22 +00:00
void SetupLog(const char* appName)
{
const std::string LOG_FILENAME{std::string(appName) + ".log"};
2022-09-13 18:25:18 +00:00
#ifdef NDEBUG
2024-03-31 10:11:22 +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
2024-03-31 10:11:22 +00:00
// DEBUG
const std::filesystem::path log_path{LOG_FILENAME};
2022-09-13 18:25:18 +00:00
#endif
2024-03-31 10:11:22 +00:00
std::vector<spdlog::sink_ptr> sinks;
2022-09-13 18:25:18 +00:00
2024-03-31 10:11:22 +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
2024-03-31 10:11:22 +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
2024-03-31 10:11:22 +00:00
auto logger = std::make_shared<spdlog::logger>(appName, sinks.begin(), sinks.end());
2022-09-13 18:25:18 +00:00
2024-03-31 10:11:22 +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
2024-03-31 10:11:22 +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
2024-03-31 10:11:22 +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
2024-03-31 10:11:22 +00:00
} // namespace engine