aboutsummaryrefslogtreecommitdiff
path: root/zenserver/diag/logging.cpp
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2022-08-26 13:52:28 +0200
committerGitHub <[email protected]>2022-08-26 04:52:28 -0700
commit8cba9329f772f8516f76e02c72dc00b61728380e (patch)
tree9f12bc42249fcb302c28b797a66f1259761889d0 /zenserver/diag/logging.cpp
parentUse "\\?\" prefixed paths and fix hardcoded path delimiters (#149) (diff)
downloadzen-8cba9329f772f8516f76e02c72dc00b61728380e.tar.xz
zen-8cba9329f772f8516f76e02c72dc00b61728380e.zip
Add sentry log sink to capture error/critical log statements (#147)
Attach log file to sentry error reports Log ERROR when std::terminate() is called
Diffstat (limited to 'zenserver/diag/logging.cpp')
-rw-r--r--zenserver/diag/logging.cpp62
1 files changed, 56 insertions, 6 deletions
diff --git a/zenserver/diag/logging.cpp b/zenserver/diag/logging.cpp
index 887b0421d..a5620a642 100644
--- a/zenserver/diag/logging.cpp
+++ b/zenserver/diag/logging.cpp
@@ -4,6 +4,15 @@
#include "config.h"
+#if !defined(ZEN_USE_SENTRY)
+# if ZEN_PLATFORM_MAC && ZEN_ARCH_ARM64
+// vcpkg's sentry-native port does not support Arm on Mac.
+# define ZEN_USE_SENTRY 0
+# else
+# define ZEN_USE_SENTRY 1
+# endif
+#endif
+
ZEN_THIRD_PARTY_INCLUDES_START
#include <spdlog/async.h>
#include <spdlog/async_logger.h>
@@ -14,6 +23,9 @@ ZEN_THIRD_PARTY_INCLUDES_START
#include <spdlog/sinks/msvc_sink.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
+#if ZEN_USE_SENTRY
+# include <sentry.h>
+#endif
ZEN_THIRD_PARTY_INCLUDES_END
#include <zencore/filesystem.h>
@@ -195,6 +207,38 @@ EnableVTMode()
return true;
}
+#if ZEN_USE_SENTRY
+
+class sentry_sink final : public spdlog::sinks::base_sink<spdlog::details::null_mutex>
+{
+public:
+ sentry_sink() {}
+
+protected:
+ static constexpr sentry_level_t MapToSentryLevel[spdlog::level::level_enum::n_levels] = {SENTRY_LEVEL_DEBUG,
+ SENTRY_LEVEL_DEBUG,
+ SENTRY_LEVEL_INFO,
+ SENTRY_LEVEL_WARNING,
+ SENTRY_LEVEL_ERROR,
+ SENTRY_LEVEL_FATAL,
+ SENTRY_LEVEL_DEBUG};
+
+ void sink_it_(const spdlog::details::log_msg& msg) override
+ {
+ if (msg.level >= SPDLOG_LEVEL_ERROR && msg.level <= SPDLOG_LEVEL_CRITICAL)
+ {
+ sentry_value_t event = sentry_value_new_message_event(
+ /* level */ MapToSentryLevel[msg.level],
+ /* logger */ nullptr,
+ /* message */ std::string(msg.payload.data(), msg.payload.size()).c_str());
+ sentry_event_value_add_stacktrace(event, NULL, 0);
+ sentry_capture_event(event);
+ }
+ }
+ void flush_() override {}
+};
+#endif
+
void
InitializeLogging(const ZenServerOptions& GlobalOptions)
{
@@ -202,9 +246,6 @@ InitializeLogging(const ZenServerOptions& GlobalOptions)
EnableVTMode();
- std::filesystem::path LogPath =
- !GlobalOptions.AbsLogFile.empty() ? GlobalOptions.AbsLogFile : GlobalOptions.DataDir / "logs" / "zenserver.log";
-
bool IsAsync = true;
spdlog::level::level_enum LogLevel = spdlog::level::info;
@@ -235,21 +276,27 @@ InitializeLogging(const ZenServerOptions& GlobalOptions)
auto ConsoleSink = std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>();
// spdlog can't create directories that starts with `\\?\` so we make sure the folder exists before creating the logger instance
- zen::CreateDirectories(LogPath.parent_path());
+ zen::CreateDirectories(GlobalOptions.AbsLogFile.parent_path());
#if 0
- auto FileSink = std::make_shared<spdlog::sinks::daily_file_sink_mt>(zen::PathToUtf8(LogPath),
+ auto FileSink = std::make_shared<spdlog::sinks::daily_file_sink_mt>(zen::PathToUtf8(GlobalOptions.AbsLogFile),
0,
0,
/* truncate */ false,
uint16_t(/* max files */ 14));
#else
- auto FileSink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(zen::PathToUtf8(LogPath),
+ auto FileSink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(zen::PathToUtf8(GlobalOptions.AbsLogFile),
/* max size */ 128 * 1024 * 1024,
/* max files */ 16,
/* rotate on open */ true);
#endif
+#if ZEN_USE_SENTRY
+ auto SentrySink = std::make_shared<sentry_sink>();
+#endif
+
+ std::set_terminate([]() { ZEN_CRITICAL("Program exited abnormally via std::terminate()"); });
+
// Default
auto& DefaultLogger = zen::logging::Default();
@@ -258,6 +305,9 @@ InitializeLogging(const ZenServerOptions& GlobalOptions)
Sinks.clear();
Sinks.push_back(ConsoleSink);
Sinks.push_back(FileSink);
+#if ZEN_USE_SENTRY
+ Sinks.push_back(SentrySink);
+#endif
#if ZEN_PLATFORM_WINDOWS
if (zen::IsDebuggerPresent() && GlobalOptions.IsDebug)