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+ bool skipped = !skipCurrent;
52+ bool addLineEnd = false ;
53+
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+
62+ #if defined( _MSC_VER )
63+ size_t pos = file.find ( " src\\ n" );
64+ #else
65+ size_t pos = file.find ( " src/" );
66+ #endif
67+
68+ if ( pos != std::string::npos ) {
69+ file = file.substr ( pos + 4 );
70+ }
71+
72+ if ( compact ) {
73+ #if defined( _MSC_VER )
74+ pos = file.find ( " engine\\ renderer-vulkan" );
75+ #else
76+ pos = file.find ( " engine/renderer-vulkan" );
77+ #endif
78+
79+ if ( pos == std::string::npos ) {
80+ continue ;
81+ }
82+
83+ file = file.substr ( pos + 23 );
84+ }
85+
86+ if ( compact ) {
87+ out += Str::Format ( addLineEnd ? " \n %s:%u" : " %s:%u" , file, entry.source_line () );
88+ } else {
89+ out += Str::Format ( addLineEnd ? " \n %s:%u: %s" : " %s:%u: %s" , file, entry.source_line (), entry.description () );
90+ }
91+ addLineEnd = true ;
92+ }
93+
94+ return out;
95+ }
96+
97+ inline void PrintStackTrace ( const std::stacktrace& stackTrace = std::stacktrace::current() ) {
98+ Log::Warn ( " \n\n ====================\n StackTrace:\n %s\n ====================\n\n " , FormatStackTrace ( stackTrace ) );
99+ }
100+
101+ #else
102+
103+ inline void PrintStackTrace () {
104+ Log::Warn ( " StackTrace unavailable: CPP23 required" );
105+ }
106+
107+ #endif
108+
109+ #endif // STACKTRACE_H
0 commit comments