14
14
* \file mmfatl.h
15
15
* \brief supports generating of fatal error messages
16
16
*
17
- * part of the application's infrastructure
17
+ * In a sort of 3-tier architecture consisting of resource/configuration/data
18
+ * management, operation logic and algorithms, and presentation layer (or user
19
+ * interface), this code is interfacing the operating system on the
20
+ * lowest infrastructure (resource) layer.
18
21
*
19
22
* Rationale
20
23
* =========
21
24
*
22
25
* When a fatal error occurs, the internal structures of a program may be
23
26
* corrupted to the point that recovery is impossible. The program exits
24
- * immediately, but hopefully still displays a diagnostic message.
27
+ * immediately with a failure code, but hopefully still displays a diagnostic
28
+ * message.
25
29
*
26
30
* To display this final message, we restrict its code to very basic,
27
31
* self-contained routines, independent of the rest of the program to the
31
35
* in a corrupted or memory-tight environment is minimized. This is to the
32
36
* detriment of flexibility, in particular, support for dynamic behavior is
33
37
* limited. Many Standard C library functions like printf MUST NOT be called
34
- * when heap problems arise, since they use it internally. GNU tags such
38
+ * when heap problems arise, since they rely on it internally. GNU tags such
35
39
* functions as 'AS-Unsafe heap' in their documentation (libc.pdf).
36
40
*
37
41
* Often it is sensible to embed details in a diagnosis message. Placeholders
50
54
/*** Export basic features of the fatal error message processing ***/
51
55
52
56
53
- /*! the size a fatal error message including the terminating NUL character can
57
+ /*!
58
+ * \brief size of a text buffer used to construct a message
59
+ *
60
+ * the size a fatal error message including the terminating NUL character can
54
61
* assume without truncation. Must be in the range of an int.
55
62
*/
56
63
enum {
57
64
MMFATL_MAX_MSG_SIZE = 1024 ,
58
65
};
59
66
60
- /*! the character sequence appended to a truncated fatal error message due to
61
- * a buffer overflow, so its reader is aware a displayed text is incomplete.
62
- * The ellipsis is followed by a line feed to ensure even an overflown message
63
- * ends with one. This is to separate a message from the command prompt
64
- * following an exit.
67
+ /*!
68
+ * \brief ASCII text sequence indicating truncated text
69
+ *
70
+ * the character sequence appended to a truncated fatal error message due to a
71
+ * buffer overflow, so its reader is aware a displayed text is incomplete. The
72
+ * ellipsis is followed by a line feed to ensure even an overflown message ends
73
+ * with one. This is to separate a message from the command prompt following
74
+ * an exit.
65
75
*/
66
76
#define MMFATL_ELLIPSIS "...\n"
67
77
68
78
/*!
79
+ * \brief ASCII characters used for placeholder tokens, printf style
80
+ *
69
81
* supported value types of a two character placeholder token in a format
70
82
* string. The first character of a placeholder is always an escape
71
83
* character \ref MMFATL_PH_PREFIX, followed by one of the type characters
79
91
* in this particular case.
80
92
*/
81
93
enum fatalErrorPlaceholderType {
82
- //! escape character marking a placeholder
94
+ //! escape character marking a placeholder, followed by a type character
83
95
MMFATL_PH_PREFIX = '%' ,
84
96
//! type character marking a placeholder for a string
85
97
MMFATL_PH_STRING = 's' ,
@@ -152,13 +164,13 @@ extern void fatalErrorInit(void);
152
164
* correct placeholder token.
153
165
*
154
166
* NULL is equivalent to an empty format string, and supported both as a
155
- * \ref format string and as a parameter for a string placeholder, to
167
+ * \p format string and as a parameter for a string placeholder, to
156
168
* enhance robustness.
157
169
*
158
170
* It is recommended to let the message end with a LF character, so a command
159
171
* prompt following it is displayed on a new line. If it is missing
160
- * \ref fatalErrorPrintAndExit will supply one, but relying on this adds to
161
- * unnecessary steps under severe conditions.
172
+ * \ref fatalErrorPrintAndExit will supply one, but relying on this adds an
173
+ * unnecessary correction under severe conditions.
162
174
*
163
175
*
164
176
* The \p format is followed by a possibly empty list of paramaters substituted
@@ -190,7 +202,21 @@ extern bool fatalErrorPush(char const* format, ...);
190
202
* \brief display buffer contents and exit program with code EXIT_FAILURE.
191
203
*
192
204
* This function does not return.
193
- *
205
+ *
206
+ * A NUL terminated message has been prepared in an internal buffer using a
207
+ * sequence of \ref fatalErrorPush. This function writes this message to
208
+ * stderr (by default tied to the terminal screen like stdout), and exits the
209
+ * program afterwards, indicating a failure to the operating system.
210
+ *
211
+ * A line feed is appended to a prepared non-empty message if it is not its
212
+ * last character. This keeps the message and a command prompt following the
213
+ * exit on separare lines. It is recommended to avoid this corrective
214
+ * measure and supply a terminating line feed as part of the message.
215
+ *
216
+ * It is possible to call this function without preparing a message. In this
217
+ * case nothing is written to stderr, and the program just exits with a failure
218
+ * code.
219
+ *
194
220
* \pre \ref fatalErrorInit has initialized the internal error message
195
221
* buffer, possibly followed by a sequence of \ref fatalErrorPush
196
222
* filling it with a message.
@@ -203,28 +229,43 @@ extern bool fatalErrorPush(char const* format, ...);
203
229
* stderr to, say, a log file, the error message is displayed to the user on
204
230
* his terminal.
205
231
* \warning previous versions of Metamath returned the exit code 1. Many
206
- * implementations define EXIT_FAILURE to this very value, but that is not
207
- * mandated by the C11 standard. In fact, some systems may interpret 1 as a
208
- * success code, so EXIT_FAILURE is more appropriate.
232
+ * systemss define EXIT_FAILURE to this very value, but that is not mandated
233
+ * by the C11 standard. In fact, some systems may interpret 1 as a success
234
+ * code, so EXIT_FAILURE is more appropriate.
209
235
*/
210
236
extern void fatalErrorPrintAndExit (void );
211
237
212
238
/*!
213
- * convenience function, covering a sequence of \ref fatalErrorInit,
239
+ * \brief standard error reporting and program exit with failure code.
240
+ *
241
+ * Convenience function, covering a sequence of \ref fatalErrorInit,
214
242
* \ref fatalErrorPush and \ref fatalErrorPrintAndExit in succession. This
215
243
* function does not return.
216
244
*
245
+ * If an error location is given, it is printed first, followed by any
246
+ * non-empty message. A line feed is padded to the right of the message if it
247
+ * is missing. This is to keep a following command prompt on a new line. It
248
+ * is recommended to avoid this corrective measure and supply a line feed as
249
+ * part of the message.
250
+ *
251
+ * If all parameters are NULL or 0, no message is printed, not even the
252
+ * automatically supplied line feed, and this function just exits the program
253
+ * with an error code. If possible use \ref fatalErrorPrintAndExit directly
254
+ * instead.
255
+ *
217
256
* \param file [null] filename of code responsible for calling this function,
218
- * suitable for macro __FILE__. Part of an error location.
257
+ * suitable for macro __FILE__. Part of an error location. Ignored in case
258
+ * of NULL.
219
259
* \param line [unsigned] if greater 0, interpreted as a line number, where
220
260
* a call to this function is initiated, suitable for macro __LINE__.
221
261
* Part of an error location.
222
- * \param msgWithPlaceholders the error message to display. This message
223
- * may include placeholders, in which case it must be followed by more
224
- * parameters, corresponding to the values to replace the placeholders.
225
- * These values must match in type that of the placeholders, and their
226
- * number must be enough (can be more) to cover all placeholders. The
227
- * details of this process is explained in \ref fatalErrorPush.
262
+ * \param msgWithPlaceholders [null] the error message to display. This
263
+ * message may include placeholders in printf style, in which case it must be
264
+ * followed by more parameters, corresponding to the values replacing
265
+ * placeholders. These values must match in type that of the placeholders,
266
+ * and their number must be enough (can be more) to cover all placeholders.
267
+ * The details of this process is explained in \ref fatalErrorPush. Ignored
268
+ * if NULL.
228
269
* \post the program exits with EXIT_FAILURE return code, after writing the
229
270
* error location and message to stderr.
230
271
*/
0 commit comments