Debian Codegraph @

common/misc/error.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19 /*
20 *
21 * Error handling/printing/exiting code
22 *
23 */
24
25 #include <cstdlib>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <string.h>
29
30 #include "messagebox.h"
31 #include "pstypes.h"
32 #include "console.h"
33 #include "dxxerror.h"
34
35 namespace dcx {
36
37 #define MAX_MSG_LEN 2048
38
39 #if !DXX_USE_EDITOR || (!(defined(WIN32) || defined(__APPLE__) || defined(__MACH__)))
40 static void warn_printf(const std::span<const char> s)
41 {
42 con_puts(CON_URGENT, s);
43 }
44 #endif
45
46 #if DXX_USE_EDITOR
47 static void (*warn_func)(std::span<const char> s) =
48 #if defined(WIN32) || defined(__APPLE__) || defined(__MACH__)
49 msgbox_warning
50 #else
51 warn_printf
52 #endif
53 ;
54
55 //provides a function to call with warning messages
56 void set_warn_func(void (*f)(std::span<const char> s))
57 {
58 warn_func = f;
59 }
60
61 #if !(defined(WIN32) || defined(__APPLE__) || defined(__MACH__))
62 //uninstall warning function - install default printf
63 void clear_warn_func()
64 {
65 warn_func = warn_printf;
66 }
67 #endif
68 #else
69 constexpr auto warn_func = &warn_printf;
70 #endif
71
72 namespace {
73
74 static void print_exit_message(const std::span<const char> exit_message)
75 {
76 con_puts(CON_CRITICAL, exit_message);
77 msgbox_error(exit_message.data());
78 }
79
80 [[noreturn]]
81 static void abort_print_exit_message(const std::span<const char> exit_message)
82 {
83 print_exit_message(exit_message);
84 d_debugbreak();
85 std::abort();
86 }
87
88 [[noreturn]]
89 static void graceful_print_exit_message(const std::span<const char> exit_message)
90 {
91 print_exit_message(exit_message);
92 exit(1);
93 }
94
95 }
96
97 void (Error_puts)(const char *filename, const unsigned line, const char *func, const char *str)
98 {
99 char exit_message[MAX_MSG_LEN]; // don't put the new line in for dialog output
100 const auto len = snprintf(exit_message, sizeof(exit_message), "%s:%u: %s: error: %s", filename, line, func, str);
101 abort_print_exit_message({exit_message, len > 0 ? static_cast<std::size_t>(len) : 0});
102 }
103
104 void (UserError_puts)(const char *str, std::size_t len)
105 {
106 graceful_print_exit_message({str, len});
107 }
108
109 //terminates with error code 1, printing message
110 void (Error)(const char *filename, const unsigned line, const char *func, const char *fmt,...)
111 {
112 char exit_message[MAX_MSG_LEN]; // don't put the new line in for dialog output
113 va_list arglist;
114
115 const std::size_t leader = std::max(snprintf(exit_message, sizeof(exit_message), "%s:%u: %s: error: ", filename, line, func), 0);
116 va_start(arglist,fmt);
117 const std::size_t len = std::max(vsnprintf(exit_message + leader, sizeof(exit_message) - leader, fmt, arglist), 0);
118 va_end(arglist);
119 abort_print_exit_message({exit_message, leader + len});
120 }
121
122 void (UserError)(const char *fmt,...)
123 {
124 char exit_message[MAX_MSG_LEN]; // don't put the new line in for dialog output
125 va_list arglist;
126 va_start(arglist,fmt);
127 const std::size_t len = std::max(vsnprintf(exit_message, sizeof(exit_message), fmt, arglist), 0);
128 va_end(arglist);
129 graceful_print_exit_message({exit_message, len});
130 }
131
132 void Warning_puts(const char *str)
133 {
134 const auto warn{warn_func};
135 #if DXX_USE_EDITOR
136 if (warn == NULL)
137 return;
138 #endif
139 std::array<char, MAX_MSG_LEN> warn_message;
140 const auto written = snprintf(std::data(warn_message), std::size(warn_message), "Warning: %s", str);
141 (*warn)(std::span<const char>(warn_message.data(), written));
142 }
143
144 //print out warning message to user
145 void (Warning)(const char *fmt,...)
146 {
147 va_list arglist;
148
149 const auto warn{warn_func};
150 #if DXX_USE_EDITOR
151 if (warn == NULL)
152 return;
153 #endif
154
155 std::array<char, MAX_MSG_LEN> warn_message;
156 strcpy(warn_message.data(), "Warning: ");
157
158 va_start(arglist,fmt);
159 const auto written = vsnprintf(std::next(warn_message.begin(), 9), std::size(warn_message) - 9, fmt, arglist);
160 va_end(arglist);
161
162 (*warn)(std::span<const char>(warn_message.data(), written + 9));
163 }
164
165 }