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