Skip to content

Commit

Permalink
feat: add option to set Crashlog output directory
Browse files Browse the repository at this point in the history
To set, edit the `Crashlog Directory` setting in the ini file.
  • Loading branch information
alandtse committed Nov 18, 2023
1 parent 845d4c8 commit 4a5e802
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
3 changes: 3 additions & 0 deletions contrib/Distribution/Config/CrashLogger.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ Wait for Debugger for Crash = false

;Local symbol cache directory.
Symcache Directory = c:\symcache

;Crashlog output directory. Defaults to "Documents\my games\[Skyrim]\SKSE"
Crashlog Directory = ""
12 changes: 6 additions & 6 deletions src/Crash/CrashHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,7 @@ namespace Crash
{
[[nodiscard]] std::shared_ptr<spdlog::logger> get_log()
{
auto path = logger::log_directory();
if (!path) {
util::report_and_fail("failed to find standard log directory"sv);
}

std::optional<std::filesystem::path> path = crashPath;
const auto time = std::time(nullptr);
std::tm localTime{};
if (gmtime_s(&localTime, &time) != 0) {
Expand Down Expand Up @@ -555,13 +551,17 @@ namespace Crash
}
} // namespace

void Install()
void Install(std::string a_crashPath)
{
const auto success =
::AddVectoredExceptionHandler(1, reinterpret_cast<::PVECTORED_EXCEPTION_HANDLER>(&VectoredExceptions));
if (success == nullptr) {
util::report_and_fail("failed to install vectored exception handler"sv);
}
logger::info("installed crash handlers"sv);
if (!a_crashPath.empty()) {
crashPath = a_crashPath;
logger::info("Crash Logs will be written to {}", crashPath);
}
}
} // namespace Crash
3 changes: 2 additions & 1 deletion src/Crash/CrashHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ namespace Crash
std::span<const boost::stacktrace::frame> _frames;
};

void Install();
void Install(std::string a_crashPath);
static std::string crashPath;
}
2 changes: 2 additions & 0 deletions src/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ void Settings::Debug::Load(CSimpleIniA& a_ini)
get_value(a_ini, flushLevel, section, "Flush Level", ";Log level to force messages to print from buffer.");
get_value(a_ini, waitForDebugger, section, "Wait for Debugger for Crash", ";Enable if using VisualStudio to debug CrashLogger; Set false otherwise because Crashlogger will not produce a crash until the debugger is detected.");
get_value(a_ini, symcache, section, "Symcache Directory", ";Local symbol cache directory.");
std::string crashDirectoryComment = std::format("; Crashlog output directory. If blank, defaults to \"Documents\\my games\\{}\\SKSE\\\"", !REL::Module::IsVR() ? "Skyrim Special Edition" : "Skyrim VR");
get_value(a_ini, crashDirectory, section, "Crashlog Directory", crashDirectoryComment.c_str());
}

const Settings::Debug& Settings::GetDebug() const
Expand Down
1 change: 1 addition & 0 deletions src/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Settings
spdlog::level::level_enum flushLevel{ spdlog::level::level_enum::trace };
bool waitForDebugger{ false };
std::string symcache{ "" };
std::string crashDirectory{ "" };
};

[[nodiscard]] static Settings* GetSingleton();
Expand Down
29 changes: 28 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ namespace
logger::info("Log Level: {}", spdlog::level::to_string_view(spdlog::get_level()));
}

std::string SetupCrashLogPath()
{
std::optional<std::filesystem::path> path;
const auto& debugConfig = Settings::GetSingleton()->GetDebug();
if (!debugConfig.crashDirectory.empty()) {
path.emplace(debugConfig.crashDirectory);
} else {
if (!logger::log_directory()) {
util::report_and_fail("failed to find standard log directory"sv);
}
path = logger::log_directory();
}
try {
if (!std::filesystem::exists(path.value())) {
std::filesystem::create_directories(path.value());
}
}
catch (const std::filesystem::filesystem_error& e) {
auto errorString = std::format("Unable to create Crashlog output directory: {}", e.what());
util::report_and_fail(errorString);
}
if (path.has_value()) {
return path.value().string();
}
return "";
}

/**
* Initialize the SKSE cosave system for our plugin.
*
Expand Down Expand Up @@ -197,7 +224,7 @@ SKSEPluginLoad(const LoadInterface* skse)
log::info("NOTE: This is not a crashlog. Crashlogs have the name crash-[TIMESTAMP].log");
log::info("{} {} {} {} is loading...", plugin->GetName(), version, __DATE__, __TIME__);
Init(skse);
Crash::Install();
Crash::Install(SetupCrashLogPath());
//InitializeMessaging();
//InitializeSerialization();
//InitializePapyrus();
Expand Down

0 comments on commit 4a5e802

Please sign in to comment.