Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Fatal.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#include "Stroika/Foundation/Characters/SDKChar.h"
9#include "Stroika/Foundation/Debug/Debugger.h"
11
12#include "Fatal.h"
13
14using namespace Stroika::Foundation;
16using namespace Stroika::Foundation::Debug;
17using namespace Stroika::Foundation::Execution;
18
19/*
20 ********************************************************************************
21 ************************ Debug::DefaultFatalErrorHandler ***********************
22 ********************************************************************************
23 */
24void Debug::DefaultFatalErrorHandler ([[maybe_unused]] const SDKChar* msg) noexcept
25{
26 DbgTrace ("Fatal Error {} encountered"_f, String::FromSDKString (msg));
27 if (auto exc = current_exception ()) {
28 DbgTrace ("Uncaught exception: {}"_f, exc);
29 }
30#if qStroika_Foundation_Debug_DefaultTracingOn
31 {
32 wstring tmp{Debug::BackTrace::Capture ()};
33 if (not tmp.empty ()) {
34 DbgTrace ("BackTrace: {}"_f, tmp);
35 }
36 }
37#endif
38 Debug::DropIntoDebuggerIfPresent ();
39 abort ();
40}
41
42/*
43 ********************************************************************************
44 ******************* Debug::RegisterDefaultFatalErrorHandlers *******************
45 ********************************************************************************
46 */
47namespace {
48 void (*sFatalErrorHandler_) (const SDKChar* msg) noexcept = nullptr; // our handlers can never get called until RegisterDefaultFatalErrorHandlers () is called, so nullptr better (BSS storage in case never used)
49 void TerminateHandler_ ()
50 {
51 (sFatalErrorHandler_) (SDKSTR ("std::terminate () called"));
52 }
53#if qStroika_Foundation_Common_Platform_Windows
54 void PurecallHandler_ ()
55 {
56 (sFatalErrorHandler_) (SDKSTR ("purecall_handler_ () called"));
57 }
58#endif
59}
60
61void Debug::RegisterDefaultFatalErrorHandlers (void (*fatalErrorHandler) (const SDKChar* msg) noexcept)
62{
63 RequireNotNull (fatalErrorHandler);
64 sFatalErrorHandler_ = fatalErrorHandler;
65 set_terminate (TerminateHandler_);
66#if qStroika_Foundation_Common_Platform_Windows
67 // Not C++ standard - just msvc error call
68 (void)_set_purecall_handler (PurecallHandler_);
69#endif
70}
#define RequireNotNull(p)
Definition Assertions.h:347
#define DbgTrace
Definition Trace.h:309
conditional_t< qTargetPlatformSDKUseswchar_t, wchar_t, char > SDKChar
Definition SDKChar.h:71