CLibs
Loading...
Searching...
No Matches
errors.h
Go to the documentation of this file.
1/*
2 * Utility functions for handling errors.
3 *
4 * Includes (among others):
5 * return values
6 * better warn
7 * stack trace printing
8 *
9 *
10 * Created by MacBook on 30.10.2024.
11 */
12
13#ifndef CLIBS_ERRORS_H
14#define CLIBS_ERRORS_H
15
16/* for this header */
17#include "attributes.h" /* PrintfLike, LibraryDefined */
18#include "filenames.h" /* get_prog_name(), __FILE_NAME__ */
19#include "terminal_colors.h" /* COLORs, PrintInColor */
20
21#include <errno.h> /* for WarnUniversal + include */
22#include <stddef.h> /* ptrdiff_t */
23#include <string.h> /* strerror() */
24
25/* for user */
26#include <err.h> /* include */
27
28
29#define ERRNO_OK 0 /* No error */
30
31
32#define RV_EXCEPTION ( -2 ) /* Non-fatal, recoverable error e.g. OOB index */
33#define RV_ERROR ( -1 ) /* Fatal, un-recoverable error */
34#define RV_SUCCESS 0 /* Success */
35
36
43LibraryDefined const char *rv_to_string( const int rv )
44{
45 switch ( rv )
46 {
47 case RV_SUCCESS:
48 return "Success";
49 case RV_EXCEPTION:
50 return "Exception";
51 case RV_ERROR:
52 return "Error";
53
54 default:
55 return "Unknown error";
56 }
57}
58
59
60#define on_fail( func_call ) if ( ( func_call ) != RV_SUCCESS )
61#define on_error( func_call ) if ( ( func_call ) == RV_ERROR )
62
69#define return_on_fail( func_call ) \
70 do \
71 { \
72 int func_call_retval = ( func_call ); \
73 on_fail ( func_call_retval ) \
74 { \
75 ( void ) f_stack_trace( NULL ); \
76 return func_call_retval; \
77 } \
78 } \
79 while ( 0 )
80
87#define return_on_error( func_call ) \
88 do \
89 { \
90 int func_call_retval = ( func_call ); \
91 on_error ( func_call_retval ) \
92 { \
93 ( void ) f_stack_trace( 0 ); \
94 return func_call_retval; \
95 } \
96 } \
97 while ( 0 )
98
99#define goto_on_fail( GOTO_LABEL, func_call ) \
100 do \
101 { \
102 on_fail ( func_call ) \
103 goto GOTO_LABEL; \
104 } \
105 while ( 0 )
106
107#define goto_on_error( GOTO_LABEL, func_call ) \
108 do \
109 { \
110 on_error ( func_call ) \
111 goto GOTO_LABEL; \
112 } \
113 while ( 0 )
114
115
116#ifndef COLOR_WARNING
117#define COLOR_WARNING FOREGROUND_RED
118#endif //COLOR_WARNING
119
120LibraryDefined PrintfLike( 7, 8 ) Cold ptrdiff_t
121WarnUniversal( bool PrintProgName,
122 const char *__restrict FileName,
123 const char *__restrict FunctionName,
124 int LineNumber,
125 int err_no,
126 ptrdiff_t return_value,
127 const char *__restrict format,
128 ... );
129
160ptrdiff_t WarnUniversal( bool PrintProgName,
161 const char *__restrict FileName,
162 const char *__restrict FunctionName,
163 int LineNumber,
164 int err_no,
165 ptrdiff_t return_value,
166 const char *__restrict format,
167 ... )
168{
169#ifndef SUPPRESS_WARNINGS
171
172 if ( PrintProgName )
173 fprintf( stderr, "%s", get_prog_name() );
174 if ( FileName != NULL )
175 fprintf( stderr, ": %s", FileName );
176 if ( FunctionName != NULL )
177 fprintf( stderr, ": %s", FunctionName );
178 if ( LineNumber > 0 )
179 fprintf( stderr, " @ %i", LineNumber );
180
181 if ( PrintProgName || FileName != NULL || FunctionName != NULL || LineNumber > 0 )
182 fprintf( stderr, ": " );
183
184 va_list vaList;
185 va_start( vaList, format );
186 vfprintf( stderr, format, vaList );
187 va_end( vaList );
188
189 if ( err_no >= 0 )
190 fprintf( stderr, ": %s", strerror( err_no ) );
191
193
194 fprintf( stderr, "\n" );
195
196#else // defined( SUPPRESS_WARNINGS )
197 ( void ) ( PrintProgName );
198 ( void ) ( FileName );
199 ( void ) ( FunctionName );
200 ( void ) ( LineNumber );
201 ( void ) ( err_no );
202 ( void ) ( format );
203#endif //SUPPRESS_WARNINGS
204
205 return return_value;
206}
207
241#define f_stack_trace( RETVAL ) \
242 WarnUniversal( false, NULL, NULL, -1, -1, ( ptrdiff_t ) RETVAL, "\tin %s", __func__ )
243
248#define ffl_stack_trace( RETVAL ) \
249 WarnUniversal( false, \
250 NULL, \
251 NULL, \
252 -1, \
253 -1, \
254 ( ptrdiff_t ) RETVAL, \
255 "\tin %s: %s @ %d", \
256 __FILE_NAME__, \
257 __func__, \
258 __LINE__ )
259
260
262#define warn_ret( RETVAL, ... ) \
263 WarnUniversal( true, NULL, NULL, -1, errno, ( ptrdiff_t ) RETVAL, __VA_ARGS__ )
264#define warnx_ret( RETVAL, ... ) \
265 WarnUniversal( true, NULL, NULL, -1, -1, ( ptrdiff_t ) RETVAL, __VA_ARGS__ )
266
268#define fwarn( ... ) \
269 ( void ) WarnUniversal( true, NULL, __func__, -1, errno, -1, __VA_ARGS__ )
270#define fwarnx( ... ) \
271 ( void ) WarnUniversal( true, NULL, __func__, -1, -1, -1, __VA_ARGS__ )
272#define fwarn_ret( RETVAL, ... ) \
273 WarnUniversal( true, NULL, __func__, -1, errno, ( ptrdiff_t ) RETVAL, __VA_ARGS__ )
274#define fwarnx_ret( RETVAL, ... ) \
275 WarnUniversal( true, NULL, __func__, -1, -1, ( ptrdiff_t ) RETVAL, __VA_ARGS__ )
276
277#define fflwarn( ... ) \
278 ( void ) WarnUniversal( \
279 true, __FILE_NAME__, __func__, __LINE__, errno, -1, __VA_ARGS__ )
280#define fflwarnx( ... ) \
281 ( void ) WarnUniversal( true, __FILE_NAME__, __func__, __LINE__, -1, -1, __VA_ARGS__ )
282#define fflwarn_ret( RETVAL, ... ) \
283 WarnUniversal( true, \
284 __FILE_NAME__, \
285 __func__, \
286 __LINE__, \
287 errno, \
288 ( ptrdiff_t ) RETVAL, \
289 __VA_ARGS__ )
290#define fflwarnx_ret( RETVAL, ... ) \
291 WarnUniversal( true, \
292 __FILE_NAME__, \
293 __func__, \
294 __LINE__, \
295 -1, \
296 ( ptrdiff_t ) RETVAL, \
297 __VA_ARGS__ )
298
299
300#endif //CLIBS_ERRORS_H
Function and variable attributes.
#define LibraryDefined
Definition attributes.h:107
#define Cold
Definition attributes.h:59
#define PrintfLike(FORMAT_STRING, FIRST_VAR_ARG)
Tells the compiler which varargs correspond to a format string.
Definition attributes.h:39
LibraryDefined PrintfLike Cold ptrdiff_t WarnUniversal(bool PrintProgName, const char *__restrict FileName, const char *__restrict FunctionName, int LineNumber, int err_no, ptrdiff_t return_value, const char *__restrict format,...)
Definition errors.h:160
LibraryDefined const char * rv_to_string(const int rv)
Definition errors.h:43
#define COLOR_WARNING
Definition errors.h:117
#define RV_SUCCESS
Definition errors.h:34
#define RV_EXCEPTION
Definition errors.h:32
#define RV_ERROR
Definition errors.h:33
LibraryDefined const char * get_prog_name(void)
Definition filenames.h:81
#define COLOR_DEFAULT
Definition terminal_colors.h:33
LibraryDefined bool SetTerminalColor(FILE *stream, const char *Color)
Definition terminal_colors.h:79