generated from SkyrimScripting/SKSE_Template_Logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_with_debugger_support.h
33 lines (28 loc) · 1.37 KB
/
logger_with_debugger_support.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#pragma once
// This is a snippet you can put at the top of all of your SKSE plugins!
#include <Windows.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/msvc_sink.h>
namespace logger = SKSE::log;
void SetupLog() {
auto logsFolder = SKSE::log::log_directory();
if (!logsFolder) SKSE::stl::report_and_fail("SKSE log_directory not provided, logs disabled.");
auto pluginName = SKSE::PluginDeclaration::GetSingleton()->GetName();
auto logFilePath = *logsFolder / std::format("{}.log", pluginName);
auto fileLoggerPtr = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logFilePath.string(), true);
std::shared_ptr<spdlog::logger> loggerPtr;
if (IsDebuggerPresent()) {
auto debugLoggerPtr = std::make_shared<spdlog::sinks::msvc_sink_mt>();
spdlog::sinks_init_list loggers{std::move(fileLoggerPtr), std::move(debugLoggerPtr)};
loggerPtr = std::make_shared<spdlog::logger>("log", loggers);
} else {
// If no debugger is attached, only log to the file.
loggerPtr = std::make_shared<spdlog::logger>("log", std::move(fileLoggerPtr));
}
spdlog::set_default_logger(std::move(loggerPtr));
spdlog::set_level(spdlog::level::trace);
spdlog::flush_on(spdlog::level::trace);
}
// Then just call SetupLog() in your SKSE plugin initialization
//
// ^---- don't forget to do this or your logs won't work :)