2026-01-03 11:06:07 -05:00
|
|
|
#include "Common/Log.h"
|
2018-12-08 23:44:41 -07:00
|
|
|
|
2026-01-03 11:06:07 -05:00
|
|
|
#include <chrono>
|
2025-12-17 13:44:30 -05:00
|
|
|
#include <cstdio>
|
2026-01-03 11:06:07 -05:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <memory>
|
2018-12-08 23:44:41 -07:00
|
|
|
|
2026-01-03 11:06:07 -05:00
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
#include <spdlog/fmt/chrono.h>
|
|
|
|
|
#include <spdlog/sinks/basic_file_sink.h>
|
|
|
|
|
#include <spdlog/sinks/stdout_color_sinks.h>
|
2019-04-01 22:52:08 -07:00
|
|
|
|
2026-01-03 11:06:07 -05:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include <spdlog/sinks/msvc_sink.h>
|
2019-04-01 22:52:08 -07:00
|
|
|
#endif
|
|
|
|
|
|
2018-12-09 02:38:38 -07:00
|
|
|
namespace NLog
|
2018-12-08 23:44:41 -07:00
|
|
|
{
|
2025-12-17 13:44:30 -05:00
|
|
|
static bool gInitialized = false;
|
2019-05-24 21:49:56 -10:00
|
|
|
|
2026-02-06 11:55:17 -05:00
|
|
|
bool InitLog(const std::string& filename)
|
2018-12-08 23:44:41 -07:00
|
|
|
{
|
2026-01-03 11:06:07 -05:00
|
|
|
if (gInitialized)
|
|
|
|
|
return true;
|
2018-12-08 23:44:41 -07:00
|
|
|
|
2026-01-03 11:06:07 -05:00
|
|
|
try
|
2018-12-08 23:44:41 -07:00
|
|
|
{
|
2026-01-03 11:06:07 -05:00
|
|
|
std::vector<spdlog::sink_ptr> sinks{
|
|
|
|
|
std::make_shared<spdlog::sinks::stdout_color_sink_mt>(),
|
2026-02-06 11:55:17 -05:00
|
|
|
std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true),
|
2026-01-03 11:06:07 -05:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
std::make_shared<spdlog::sinks::msvc_sink_mt>(true)
|
|
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
auto logger = std::make_shared<spdlog::logger>("axio", sinks.begin(), sinks.end());
|
|
|
|
|
logger->set_level(spdlog::level::debug);
|
|
|
|
|
logger->info("Opened log file at: {:%m/%d/%y %H:%M:%S}", std::chrono::system_clock::now());
|
2018-12-08 23:44:41 -07:00
|
|
|
#ifdef APP_FULL_NAME
|
2026-01-03 11:06:07 -05:00
|
|
|
logger->info("{}", APP_FULL_NAME "\n");
|
2018-12-08 23:44:41 -07:00
|
|
|
#endif
|
2026-01-03 11:06:07 -05:00
|
|
|
logger->flush();
|
|
|
|
|
spdlog::register_logger(logger);
|
2018-12-08 23:44:41 -07:00
|
|
|
|
2026-01-03 11:06:07 -05:00
|
|
|
gInitialized = true;
|
|
|
|
|
return true;
|
2018-12-08 23:44:41 -07:00
|
|
|
}
|
2026-01-03 11:06:07 -05:00
|
|
|
catch (const spdlog::spdlog_ex& ex)
|
2019-04-01 22:52:08 -07:00
|
|
|
{
|
2026-01-03 11:06:07 -05:00
|
|
|
std::cerr << "Unable to initialize logger: " << ex.what() << '\n';
|
|
|
|
|
return false;
|
2019-04-01 22:52:08 -07:00
|
|
|
}
|
2018-12-08 23:44:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|