1+ /*
2+ ===========================================================================
3+
4+ Daemon BSD Source Code
5+ Copyright (c) 2025 Daemon Developers
6+ All rights reserved.
7+
8+ This file is part of the Daemon BSD Source Code (Daemon Source Code).
9+
10+ Redistribution and use in source and binary forms, with or without
11+ modification, are permitted provided that the following conditions are met:
12+ * Redistributions of source code must retain the above copyright
13+ notice, this list of conditions and the following disclaimer.
14+ * Redistributions in binary form must reproduce the above copyright
15+ notice, this list of conditions and the following disclaimer in the
16+ documentation and/or other materials provided with the distribution.
17+ * Neither the name of the Daemon developers nor the
18+ names of its contributors may be used to endorse or promote products
19+ derived from this software without specific prior written permission.
20+
21+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+ DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
25+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+ ===========================================================================
33+ */
34+ // StackTrace.h
35+
36+ #ifndef STACKTRACE_H
37+ #define STACKTRACE_H
38+
39+ #include " CPPStandard.h"
40+ #include " String.h"
41+
42+ #include " Log.h"
43+
44+ #if defined( CPP_STACKTRACE )
45+
46+ #include < stacktrace>
47+
48+ inline std::string FormatStackTrace ( const std::stacktrace& stackTrace,
49+ const bool skipCurrent = false , const bool compact = false ) {
50+ std::string out;
51+
52+ bool skipped = !skipCurrent;
53+ bool addLineEnd = false ;
54+ for ( const std::stacktrace_entry& entry : stackTrace ) {
55+ if ( !skipped ) {
56+ skipped = true ;
57+ continue ;
58+ }
59+
60+ std::string file = entry.source_file ();
61+ if ( compact ) {
62+ const size_t pos = std::min (
63+ file.find ( " src/engine/renderer-vulkan" ),
64+ file.find ( " src\\ engine\\ renderer-vulkan" )
65+ );
66+
67+ if ( pos == std::string::npos ) {
68+ continue ;
69+ }
70+
71+ file = file.substr ( pos + 27 );
72+ }
73+
74+ if ( compact ) {
75+ out += Str::Format ( addLineEnd ? " \n %s:%u" : " %s:%u" , file, entry.source_line () );
76+ } else {
77+ out += Str::Format ( addLineEnd ? " \n %s:%u: %s" : " %s:%u: %s" , file, entry.source_line (), entry.description () );
78+ }
79+ addLineEnd = true ;
80+ }
81+
82+ return out;
83+ }
84+
85+ inline void PrintStackTrace ( const std::stacktrace& stackTrace = std::stacktrace::current() ) {
86+ Log::Warn ( " \n\n ====================\n StackTrace:\n %s\n ====================\n\n " , FormatStackTrace ( stackTrace ) );
87+ }
88+
89+ #else
90+
91+ inline void PrintStackTrace () {
92+ Log::Warn ( " StackTrace unavailable: CPP23 required" );
93+ }
94+
95+ #endif
96+
97+ #endif // STACKTRACE_H
0 commit comments