Merge commit 'meins/master'
[adu.git] / error.h
1 extern char *__errlist[];
2 extern char *__error_txt;
3
4 //__printf_2_3 void __log(int ll, const char* fmt,...);
5
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
12
13 /** Check whether the system error bit is set. */
14 #define IS_SYSTEM_ERROR(num) (!!((num) & (1 << SYSTEM_ERROR_BIT)))
15
16 /** Set the system error bit for the given number. */
17 #define ERRNO_TO_ERROR(num) ((num) | (1 << SYSTEM_ERROR_BIT))
18
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 the 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_ERROR(_errno) == num;
31 }
32
33 /**
34 * 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 *error_txt(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 __errlist[num];
47 }
48
49 #define ALL_ERRORS \
50 _ERROR(SUCCESS, "success") \
51 _ERROR(BAD_DB_DIR, "invalid database directory") \
52 _ERROR(NO_COLUMN_DESC, "missing column description") \
53 _ERROR(BAD_NAME, "invalid name for a column/table") \
54 _ERROR(BAD_STORAGE_TYPE, "invalid storage type") \
55 _ERROR(BAD_STORAGE_FLAGS, "invalid storage flags") \
56 _ERROR(NO_COLUMN_NAME, "missing column name") \
57 _ERROR(NO_COLUMNS, "at least one column required") \
58 _ERROR(BAD_COLUMN_NAME, "invalid name for a table column") \
59 _ERROR(NO_UNIQUE_RBTREE_COLUMN, "need at least one mapped column with OSL_UNIQE and OSL_RBTREE OSL") \
60 _ERROR(NO_RBTREE_COL, "at least one column needs an rbtree") \
61 _ERROR(DUPLICATE_COL_NAME, "column name given twice") \
62 _ERROR(BAD_STORAGE_SIZE, "invalid storage size") \
63 _ERROR(NO_COMPARE_FUNC, "missing compare function") \
64 _ERROR(BAD_DATA_SIZE, "wrong data size for fixed-size column") \
65 _ERROR(NOT_MAPPED, "file not mapped") \
66 _ERROR(ALREADY_MAPPED, "file already mapped") \
67 _ERROR(BAD_SIZE, "invalid size specified") \
68 _ERROR(TRUNC, "failed to truncate file") \
69 _ERROR(BAD_TABLE, "table not open") \
70 _ERROR(BAD_TABLE_DESC, "invalid table description") \
71 _ERROR(RB_KEY_EXISTS, "key already exists in rbtree") \
72 _ERROR(RB_KEY_NOT_FOUND, "key not found in rbtree") \
73 _ERROR(BAD_ROW_NUM, "invalid row number") \
74 _ERROR(INDEX_CORRUPTION, "index corruption detected") \
75 _ERROR(INVALID_OBJECT, "reference to invalid object") \
76 _ERROR(STAT, "can not stat file") \
77 _ERROR(WRITE, "write error") \
78 _ERROR(LSEEK, "lseek error") \
79 _ERROR(BUSY, "table is busy") \
80 _ERROR(SHORT_TABLE, "table too short") \
81 _ERROR(NO_MAGIC, "missing table header magic") \
82 _ERROR(VERSION_MISMATCH, "table version not supported") \
83 _ERROR(BAD_COLUMN_NUM, "invalid column number") \
84 _ERROR(BAD_TABLE_FLAGS, "invalid flags in table description") \
85 _ERROR(BAD_ROW, "invalid row") \
86 _ERROR(ATOI_OVERFLOW, "value too large") \
87 _ERROR(STRTOLL, "unknown strtoll error") \
88 _ERROR(ATOI_NO_DIGITS, "no digits found in string") \
89 _ERROR(ATOI_JUNK_AT_END, "further characters after number") \
90 _ERROR(FGETS, "fgets error") \
91 _ERROR(EMPTY, "file empty") \
92 _ERROR(MMAP, "mmap error") \
93 _ERROR(SYNTAX, "syntax error") \
94 _ERROR(LOOP_COMPLETE, "loop complete") \
95 _ERROR(HASH_TABLE_OVERFLOW, "hash table too small") \
96 _ERROR(BAD_UID, "uid not found in hash table")
97
98
99 /**
100 * This is temporarily defined to expand to its first argument (prefixed by
101 * 'E_') and gets later redefined to expand to the error text only
102 */
103 #define _ERROR(err, msg) E_ ## err,
104
105 enum error_codes {
106 ALL_ERRORS
107 };
108 #undef _ERROR
109 #define _ERROR(err, msg) msg,
110 #define DEFINE_ERRLIST char *__errlist[] = {ALL_ERRORS}