1+ #pragma once
2+
3+ namespace ZEngine ::CrashHandlers
4+ {
5+ struct CrashHandler
6+ {
7+ using PreCrashFn = void (*)(void * ctx);
8+ using cstring = const char *;
9+
10+ // Install the platform crash handler.
11+ // app_name — e.g. "GameName"
12+ // version — e.g. "1.0.0"
13+ // crash_log_dir — directory where crash logs and .dmp files are written.
14+ // Created if it does not exist.
15+ //
16+ // Call once, before any engine subsystem is initialized.
17+ // No-op if already installed.
18+ static void Install (cstring app_name, cstring version, cstring crash_log_dir);
19+
20+ // Remove the platform crash handler and restore default OS behavior.
21+ // Call at engine shutdown. Safe to call if Install() was never called.
22+ static void Uninstall ();
23+
24+ // Set a callback to be called just before the crash dump is written.
25+ // This is useful for flushing logs, saving game state, etc.
26+ //
27+ // The callback must complete within a defined timeout (default 2 seconds) or the crash handler will proceed to write the dump anyway.
28+ // On Windows, a helper thread's timeout fires
29+ // On Linux, macOS SIGALRM is raised on the callback thread
30+ //
31+ // Only one callback can be set at a time. Setting a new callback replaces the previous one.
32+ // It must not allocate memory on the ZEngine heap, or call ZENGINE_VALIDATE_ASSERT.
33+ static void SetPreCrashCallback (PreCrashFn fn, void * ctx = nullptr );
34+
35+ // Called by the platform handler (SEH filter on Windows, signal handler
36+ // on POSIX) to perform the cross-platform crash response.
37+ // signal_or_exception — human-readable description, e.g.
38+ // "Access Violation at 0x0000000000000000"
39+ // context — platform-specific context:
40+ // Windows: EXCEPTION_POINTERS*
41+ // POSIX: ucontext_t*
42+ //
43+ // This function does not return under normal conditions.
44+ [[noreturn]] static void OnCrash (cstring signal_or_exception, void * ctx = nullptr );
45+
46+ // Called by ZENGINE_VALIDATE_ASSERT to produce a crash report for
47+ // assertion failures in Release/RelWithDebInfo builds.
48+ // file — __FILE__
49+ // line — __LINE__
50+ // message — the assertion condition and user message string
51+ [[noreturn]] static void OnAssertionFailure (cstring file, int line, cstring message);
52+
53+ private:
54+ CrashHandler () = delete ;
55+
56+ static void StoreMetadata (cstring app_name, cstring version, cstring crash_log_dir);
57+ };
58+ } // namespace ZEngine::CrashHandlers
0 commit comments