aboutsummaryrefslogtreecommitdiff
path: root/src/zencore
diff options
context:
space:
mode:
Diffstat (limited to 'src/zencore')
-rw-r--r--src/zencore/include/zencore/logging/tracesink.h4
-rw-r--r--src/zencore/include/zencore/sentryintegration.h1
-rw-r--r--src/zencore/include/zencore/system.h2
-rw-r--r--src/zencore/logging/tracesink.cpp4
-rw-r--r--src/zencore/sentryintegration.cpp19
-rw-r--r--src/zencore/system.cpp60
-rw-r--r--src/zencore/testutils.cpp2
7 files changed, 87 insertions, 5 deletions
diff --git a/src/zencore/include/zencore/logging/tracesink.h b/src/zencore/include/zencore/logging/tracesink.h
index e63d838b4..785c51e10 100644
--- a/src/zencore/include/zencore/logging/tracesink.h
+++ b/src/zencore/include/zencore/logging/tracesink.h
@@ -6,6 +6,8 @@
namespace zen::logging {
+#if ZEN_WITH_TRACE
+
/**
* A logging sink that forwards log messages to the trace system.
*
@@ -20,4 +22,6 @@ public:
void SetFormatter(std::unique_ptr<Formatter> InFormatter) override;
};
+#endif
+
} // namespace zen::logging
diff --git a/src/zencore/include/zencore/sentryintegration.h b/src/zencore/include/zencore/sentryintegration.h
index a4e33d69e..27e5a8a82 100644
--- a/src/zencore/include/zencore/sentryintegration.h
+++ b/src/zencore/include/zencore/sentryintegration.h
@@ -40,6 +40,7 @@ public:
};
void Initialize(const Config& Conf, const std::string& CommandLine);
+ void Close();
void LogStartupInformation();
static void ClearCaches();
diff --git a/src/zencore/include/zencore/system.h b/src/zencore/include/zencore/system.h
index fecbe2dbe..a67999e52 100644
--- a/src/zencore/include/zencore/system.h
+++ b/src/zencore/include/zencore/system.h
@@ -14,6 +14,7 @@ class CbWriter;
std::string GetMachineName();
std::string_view GetOperatingSystemName();
+std::string GetOperatingSystemVersion();
std::string_view GetRuntimePlatformName(); // "windows", "wine", "linux", or "macos"
std::string_view GetCpuName();
@@ -28,6 +29,7 @@ struct SystemMetrics
uint64_t AvailVirtualMemoryMiB = 0;
uint64_t PageFileMiB = 0;
uint64_t AvailPageFileMiB = 0;
+ uint64_t UptimeSeconds = 0;
};
/// Extended metrics that include CPU usage percentage, which requires
diff --git a/src/zencore/logging/tracesink.cpp b/src/zencore/logging/tracesink.cpp
index e3533327b..8a6f4e40c 100644
--- a/src/zencore/logging/tracesink.cpp
+++ b/src/zencore/logging/tracesink.cpp
@@ -7,6 +7,8 @@
#include <zencore/timer.h>
#include <zencore/trace.h>
+#if ZEN_WITH_TRACE
+
namespace zen::logging {
UE_TRACE_CHANNEL_DEFINE(LogChannel)
@@ -86,3 +88,5 @@ TraceSink::SetFormatter(std::unique_ptr<Formatter> /*InFormatter*/)
}
} // namespace zen::logging
+
+#endif
diff --git a/src/zencore/sentryintegration.cpp b/src/zencore/sentryintegration.cpp
index e39b8438d..8d087e8c6 100644
--- a/src/zencore/sentryintegration.cpp
+++ b/src/zencore/sentryintegration.cpp
@@ -128,6 +128,8 @@ namespace zen {
# if ZEN_USE_SENTRY
ZEN_DEFINE_LOG_CATEGORY_STATIC(LogSentry, "sentry-sdk");
+static std::atomic<bool> s_SentryLogEnabled{true};
+
static void
SentryLogFunction(sentry_level_t Level, const char* Message, va_list Args, [[maybe_unused]] void* Userdata)
{
@@ -147,14 +149,15 @@ SentryLogFunction(sentry_level_t Level, const char* Message, va_list Args, [[may
}
// SentryLogFunction can be called before the logging system is initialized
- // (during sentry_init which runs before InitializeLogging). Fall back to
- // console logging when the category logger is not yet available.
+ // (during sentry_init which runs before InitializeLogging), or after it has
+ // been shut down (during sentry_close on a background worker thread). Fall
+ // back to console logging when the category logger is not available.
//
// Since we want to default to WARN level but this runs before logging has
// been configured, we ignore the callbacks for DEBUG/INFO explicitly here
// which means users don't see every possible log message if they're trying
// to configure the levels using --log-debug=sentry-sdk
- if (!TheDefaultLogger)
+ if (!TheDefaultLogger || !s_SentryLogEnabled.load(std::memory_order_acquire))
{
switch (Level)
{
@@ -212,11 +215,21 @@ SentryIntegration::SentryIntegration()
SentryIntegration::~SentryIntegration()
{
+ Close();
+}
+
+void
+SentryIntegration::Close()
+{
if (m_IsInitialized && m_SentryErrorCode == 0)
{
logging::SetErrorLog("");
m_SentryAssert.reset();
+ // Disable spdlog forwarding before sentry_close() since its background
+ // worker thread may still log during shutdown via SentryLogFunction
+ s_SentryLogEnabled.store(false, std::memory_order_release);
sentry_close();
+ m_IsInitialized = false;
}
}
diff --git a/src/zencore/system.cpp b/src/zencore/system.cpp
index 833d3c04b..141450b84 100644
--- a/src/zencore/system.cpp
+++ b/src/zencore/system.cpp
@@ -4,6 +4,7 @@
#include <zencore/compactbinarybuilder.h>
#include <zencore/except.h>
+#include <zencore/fmtutils.h>
#include <zencore/memory/memory.h>
#include <zencore/string.h>
@@ -135,6 +136,8 @@ GetSystemMetrics()
Metrics.AvailPageFileMiB = MemStatus.ullAvailPageFile / 1024 / 1024;
}
+ Metrics.UptimeSeconds = GetTickCount64() / 1000;
+
return Metrics;
}
#elif ZEN_PLATFORM_LINUX
@@ -226,6 +229,17 @@ GetSystemMetrics()
Metrics.VirtualMemoryMiB = Metrics.SystemMemoryMiB;
Metrics.AvailVirtualMemoryMiB = Metrics.AvailSystemMemoryMiB;
+ // System uptime
+ if (FILE* UptimeFile = fopen("/proc/uptime", "r"))
+ {
+ double UptimeSec = 0;
+ if (fscanf(UptimeFile, "%lf", &UptimeSec) == 1)
+ {
+ Metrics.UptimeSeconds = static_cast<uint64_t>(UptimeSec);
+ }
+ fclose(UptimeFile);
+ }
+
// Parse /proc/meminfo for swap/page file information
Metrics.PageFileMiB = 0;
Metrics.AvailPageFileMiB = 0;
@@ -318,6 +332,18 @@ GetSystemMetrics()
Metrics.PageFileMiB = SwapUsage.xsu_total / 1024 / 1024;
Metrics.AvailPageFileMiB = (SwapUsage.xsu_total - SwapUsage.xsu_used) / 1024 / 1024;
+ // System uptime via boot time
+ {
+ struct timeval BootTime
+ {
+ };
+ Size = sizeof(BootTime);
+ if (sysctlbyname("kern.boottime", &BootTime, &Size, nullptr, 0) == 0)
+ {
+ Metrics.UptimeSeconds = static_cast<uint64_t>(time(nullptr) - BootTime.tv_sec);
+ }
+ }
+
return Metrics;
}
#else
@@ -574,6 +600,38 @@ GetOperatingSystemName()
return ZEN_PLATFORM_NAME;
}
+std::string
+GetOperatingSystemVersion()
+{
+#if ZEN_PLATFORM_WINDOWS
+ // Use RtlGetVersion to avoid the compatibility shim that GetVersionEx applies
+ using RtlGetVersionFn = LONG(WINAPI*)(PRTL_OSVERSIONINFOW);
+ RTL_OSVERSIONINFOW OsVer{.dwOSVersionInfoSize = sizeof(OsVer)};
+ if (auto Fn = (RtlGetVersionFn)GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlGetVersion"))
+ {
+ Fn(&OsVer);
+ }
+ return fmt::format("Windows {}.{} Build {}", OsVer.dwMajorVersion, OsVer.dwMinorVersion, OsVer.dwBuildNumber);
+#elif ZEN_PLATFORM_LINUX
+ struct utsname Info
+ {
+ };
+ if (uname(&Info) == 0)
+ {
+ return fmt::format("{} {}", Info.sysname, Info.release);
+ }
+ return "Linux";
+#elif ZEN_PLATFORM_MAC
+ char OsVersion[64] = "";
+ size_t Size = sizeof(OsVersion);
+ if (sysctlbyname("kern.osproductversion", OsVersion, &Size, nullptr, 0) == 0)
+ {
+ return fmt::format("macOS {}", OsVersion);
+ }
+ return "macOS";
+#endif
+}
+
std::string_view
GetRuntimePlatformName()
{
@@ -608,7 +666,7 @@ Describe(const SystemMetrics& Metrics, CbWriter& Writer)
Writer << "cpu_count" << Metrics.CpuCount << "core_count" << Metrics.CoreCount << "lp_count" << Metrics.LogicalProcessorCount
<< "total_memory_mb" << Metrics.SystemMemoryMiB << "avail_memory_mb" << Metrics.AvailSystemMemoryMiB << "total_virtual_mb"
<< Metrics.VirtualMemoryMiB << "avail_virtual_mb" << Metrics.AvailVirtualMemoryMiB << "total_pagefile_mb" << Metrics.PageFileMiB
- << "avail_pagefile_mb" << Metrics.AvailPageFileMiB;
+ << "avail_pagefile_mb" << Metrics.AvailPageFileMiB << "uptime_seconds" << Metrics.UptimeSeconds;
}
void
diff --git a/src/zencore/testutils.cpp b/src/zencore/testutils.cpp
index 5bc2841ae..0cd3f8121 100644
--- a/src/zencore/testutils.cpp
+++ b/src/zencore/testutils.cpp
@@ -46,7 +46,7 @@ ScopedTemporaryDirectory::~ScopedTemporaryDirectory()
IoBuffer
CreateRandomBlob(uint64_t Size)
{
- static FastRandom Rand{.Seed = 0x7CEBF54E45B9F5D1};
+ thread_local FastRandom Rand{.Seed = 0x7CEBF54E45B9F5D1};
return CreateRandomBlob(Rand, Size);
};