... | ... |
@@ -11,38 +11,40 @@ |
11 | 11 |
#define REPORT_ERROR 1 |
12 | 12 |
#define REPORT_FATAL 2 |
13 | 13 |
|
14 |
-#define report_info( ...) report(REPORT_INFO, __VA_ARGS__) |
|
15 |
-#define report_error(...) report(REPORT_ERROR, __VA_ARGS__) |
|
16 |
-#define report_fatal(...) report(REPORT_FATAL, __VA_ARGS__) |
|
17 |
- |
|
18 | 14 |
#ifndef REPORT_STREAM_INFO |
19 | 15 |
#define REPORT_STREAM_INFO stderr |
20 | 16 |
#endif |
21 | 17 |
#ifndef REPORT_STREAM_ERROR |
22 | 18 |
#define REPORT_STREAM_ERROR stderr |
23 | 19 |
#endif |
20 |
+#ifndef REPORT_STREAM_FATAL |
|
21 |
+#define REPORT_STREAM_FATAL stderr |
|
22 |
+#endif |
|
24 | 23 |
|
25 |
-#ifndef REPORT_FORMAT_PREFIX_INFO |
|
26 |
-#define REPORT_FORMAT_PREFIX_INFO "Info: " |
|
24 |
+#ifndef REPORT_PREFIX_INFO |
|
25 |
+#define REPORT_PREFIX_INFO "Info: " |
|
27 | 26 |
#endif |
28 |
-#ifndef REPORT_FORMAT_PREFIX_ERROR |
|
29 |
-#define REPORT_FORMAT_PREFIX_ERROR "Error: " |
|
27 |
+#ifndef REPORT_PREFIX_ERROR |
|
28 |
+#define REPORT_PREFIX_ERROR "Error: " |
|
30 | 29 |
#endif |
31 |
-#ifndef REPORT_FORMAT_PREFIX_FATAL |
|
32 |
-#define REPORT_FORMAT_PREFIX_FATAL "Fatal: " |
|
30 |
+#ifndef REPORT_PREFIX_FATAL |
|
31 |
+#define REPORT_PREFIX_FATAL "Fatal: " |
|
33 | 32 |
#endif |
34 | 33 |
|
34 |
+#define report_info( ...) report(REPORT_STREAM_INFO, REPORT_PREFIX_INFO, 0, __VA_ARGS__) |
|
35 |
+#define report_error(...) report(REPORT_STREAM_ERROR, REPORT_PREFIX_ERROR, 0, __VA_ARGS__) |
|
36 |
+#define report_fatal(...) report(REPORT_STREAM_FATAL, REPORT_PREFIX_FATAL, 1, __VA_ARGS__) |
|
37 |
+ |
|
35 | 38 |
|
36 |
-static int report(int report, int report_errno, char const * format, ...) { |
|
37 |
- FILE * stream = REPORT_STREAM_INFO; |
|
38 |
- if (report >= REPORT_ERROR) |
|
39 |
- stream = REPORT_STREAM_ERROR; |
|
40 |
- fprintf(stream, "%s", |
|
41 |
- report == REPORT_INFO ? REPORT_FORMAT_PREFIX_INFO : |
|
42 |
- report == REPORT_ERROR ? REPORT_FORMAT_PREFIX_ERROR : |
|
43 |
- report == REPORT_FATAL ? REPORT_FORMAT_PREFIX_FATAL : |
|
44 |
- "" |
|
45 |
- ); |
|
39 |
+static int report( |
|
40 |
+ FILE * stream, |
|
41 |
+ char const * prefix, |
|
42 |
+ int fatal, |
|
43 |
+ int report_errno, |
|
44 |
+ char const * format, |
|
45 |
+ ... |
|
46 |
+) { |
|
47 |
+ fprintf(stream, "%s", prefix); |
|
46 | 48 |
{ |
47 | 49 |
va_list ap; |
48 | 50 |
va_start(ap, format); |
... | ... |
@@ -52,7 +54,7 @@ static int report(int report, int report_errno, char const * format, ...) { |
52 | 54 |
if (report_errno) |
53 | 55 |
fprintf(stream, ": %s", strerror(report_errno)); |
54 | 56 |
fprintf(stream, "\n"); |
55 |
- if (report >= REPORT_FATAL) |
|
57 |
+ if (fatal) |
|
56 | 58 |
exit(EXIT_FAILURE); |
57 |
- return report; |
|
59 |
+ return EXIT_FAILURE; |
|
58 | 60 |
} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,58 @@ |
1 |
+// TODO: Document that `perror(3)` is standard but does not take a printf-style |
|
2 |
+// format argument. The ones that do are nonstandard, `err(3)` (BSD), `error` |
|
3 |
+// (GNU). |
|
4 |
+#include <stdlib.h> |
|
5 |
+#include <stdio.h> |
|
6 |
+#include <stdarg.h> |
|
7 |
+#include <errno.h> |
|
8 |
+ |
|
9 |
+ |
|
10 |
+#define REPORT_INFO 0 |
|
11 |
+#define REPORT_ERROR 1 |
|
12 |
+#define REPORT_FATAL 2 |
|
13 |
+ |
|
14 |
+#define report_info( ...) report(REPORT_INFO, __VA_ARGS__) |
|
15 |
+#define report_error(...) report(REPORT_ERROR, __VA_ARGS__) |
|
16 |
+#define report_fatal(...) report(REPORT_FATAL, __VA_ARGS__) |
|
17 |
+ |
|
18 |
+#ifndef REPORT_STREAM_INFO |
|
19 |
+#define REPORT_STREAM_INFO stderr |
|
20 |
+#endif |
|
21 |
+#ifndef REPORT_STREAM_ERROR |
|
22 |
+#define REPORT_STREAM_ERROR stderr |
|
23 |
+#endif |
|
24 |
+ |
|
25 |
+#ifndef REPORT_FORMAT_PREFIX_INFO |
|
26 |
+#define REPORT_FORMAT_PREFIX_INFO "Info: " |
|
27 |
+#endif |
|
28 |
+#ifndef REPORT_FORMAT_PREFIX_ERROR |
|
29 |
+#define REPORT_FORMAT_PREFIX_ERROR "Error: " |
|
30 |
+#endif |
|
31 |
+#ifndef REPORT_FORMAT_PREFIX_FATAL |
|
32 |
+#define REPORT_FORMAT_PREFIX_FATAL "Fatal: " |
|
33 |
+#endif |
|
34 |
+ |
|
35 |
+ |
|
36 |
+static int report(int report, int report_errno, char const * format, ...) { |
|
37 |
+ FILE * stream = REPORT_STREAM_INFO; |
|
38 |
+ if (report >= REPORT_ERROR) |
|
39 |
+ stream = REPORT_STREAM_ERROR; |
|
40 |
+ fprintf(stream, "%s", |
|
41 |
+ report == REPORT_INFO ? REPORT_FORMAT_PREFIX_INFO : |
|
42 |
+ report == REPORT_ERROR ? REPORT_FORMAT_PREFIX_ERROR : |
|
43 |
+ report == REPORT_FATAL ? REPORT_FORMAT_PREFIX_FATAL : |
|
44 |
+ "" |
|
45 |
+ ); |
|
46 |
+ { |
|
47 |
+ va_list ap; |
|
48 |
+ va_start(ap, format); |
|
49 |
+ vfprintf(stream, format, ap); |
|
50 |
+ va_end(ap); |
|
51 |
+ } |
|
52 |
+ if (report_errno) |
|
53 |
+ fprintf(stream, ": %s", strerror(report_errno)); |
|
54 |
+ fprintf(stream, "\n"); |
|
55 |
+ if (report >= REPORT_FATAL) |
|
56 |
+ exit(EXIT_FAILURE); |
|
57 |
+ return report; |
|
58 |
+} |