b9cbfec373e060059d8c65ed7be5b5751527853c
1 extern char *dss_errlist[];
2 extern char *dss_error_txt;
4 __printf_2_3 void dss_log(int ll, const char* fmt,...);
6 /**
7 * This bit indicates whether a number is considered a system error number
8 * If yes, the system errno is just the result of clearing this bit from
9 * the given number.
10 */
11 #define SYSTEM_ERROR_BIT 30
13 /** Check whether the system error bit is set. */
14 #define IS_SYSTEM_ERROR(num) (!!((num) & (1 << SYSTEM_ERROR_BIT)))
16 /** Set the system error bit for the given number. */
17 #define ERRNO_TO_DSS_ERROR(num) ((num) | (1 << SYSTEM_ERROR_BIT))
19 /** Check whether a given number is a system error number.
20 *
21 * \param num The value to be checked.
22 * \param _errno The system error number.
23 *
24 * \return True if \a num is dss' representation of the system
25 * error identified by \a _errno.
26 */
27 static inline int is_errno(int num, int _errno)
28 {
29 assert(num > 0 && _errno > 0);
30 return ERRNO_TO_DSS_ERROR(_errno) == num;
31 }
33 /**
34 * dss' version of strerror(3).
35 *
36 * \param num The error number.
37 *
38 * \return The error text of \a num.
39 */
40 static inline char *dss_strerror(int num)
41 {
42 assert(num > 0);
43 if (IS_SYSTEM_ERROR(num))
44 return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
45 else
46 return dss_errlist[num];
47 }
49 static inline void log_err_msg(int loglevel, int num)
50 {
51 dss_log(loglevel, "%s (%s)\n", dss_error_txt, dss_strerror(num));
52 }
54 #define DSS_ERRORS \
55 DSS_ERROR(SUCCESS, "success") \
56 DSS_ERROR(SYNTAX, "syntax error") \
57 DSS_ERROR(INVALID_COMMAND, "invalid command") \
58 DSS_ERROR(ATOI_OVERFLOW, "value too large") \
59 DSS_ERROR(STRTOLL, "unknown strtoll error") \
60 DSS_ERROR(ATOI_NO_DIGITS, "no digits found in string") \
61 DSS_ERROR(ATOI_JUNK_AT_END, "further characters after number") \
62 DSS_ERROR(INVALID_NUMBER, "invalid number") \
63 DSS_ERROR(STRFTIME, "strftime() failed") \
64 DSS_ERROR(LOCALTIME, "localtime() failed") \
65 DSS_ERROR(NULL_OPEN, "can not open /dev/null") \
66 DSS_ERROR(DUP_PIPE, "exec error: can not create pipe") \
67 DSS_ERROR(INVOLUNTARY_EXIT, "unexpected termination cause") \
68 DSS_ERROR(BAD_EXIT_CODE, "unexpected exit code") \
69 DSS_ERROR(SIGNAL_SIG_ERR, "signal() returned SIG_ERR")
71 /**
72 * This is temporarily defined to expand to its first argument (prefixed by
73 * 'E_') and gets later redefined to expand to the error text only
74 */
75 #define DSS_ERROR(err, msg) E_ ## err,
77 enum dss_error_codes {
78 DSS_ERRORS
79 };
80 #undef DSS_ERROR
81 #define DSS_ERROR(err, msg) msg,
82 #define DEFINE_DSS_ERRLIST char *dss_errlist[] = {DSS_ERRORS}