1 /*
2 * SysDB - src/include/utils/error.h
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 /*
29 * SysDB error handling:
30 * Error handling in SysDB is done on a by-thread basis, that is, each thread
31 * will use its own memory region to store information about the last reported
32 * error.
33 * Once the error message has been passed to SysDB, it will log the entire
34 * message at once. The message will be sent to all registered log functions.
35 */
37 #ifndef SDB_UTILS_ERROR_H
38 #define SDB_UTILS_ERROR_H 1
40 #include <stdarg.h>
41 #include <stddef.h>
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
47 /* On Linux systems and possibly others, this should be the same as the LOG_
48 * constants defined by syslog. */
49 enum {
50 SDB_LOG_EMERG = 0,
51 SDB_LOG_ERR = 3,
52 SDB_LOG_WARNING = 4,
53 SDB_LOG_NOTICE = 5,
54 SDB_LOG_INFO = 6,
55 SDB_LOG_DEBUG = 7,
56 };
57 #define SDB_LOG_PRIO_TO_STRING(prio) \
58 (((prio) == SDB_LOG_EMERG) ? "EMERG" \
59 : ((prio) == SDB_LOG_ERR) ? "ERROR" \
60 : ((prio) == SDB_LOG_WARNING) ? "WARNING" \
61 : ((prio) == SDB_LOG_NOTICE) ? "NOTICE" \
62 : ((prio) == SDB_LOG_INFO) ? "INFO" \
63 : ((prio) == SDB_LOG_DEBUG) ? "DEBUG" : "UNKNOWN")
65 #ifndef SDB_DEFAULT_LOGLEVEL
66 # define SDB_DEFAULT_LOGLEVEL SDB_LOG_INFO
67 #endif
69 /*
70 * sdb_error_set_logger:
71 * Set the logging callback to be used for logging messages. By default (or
72 * when explicitely setting the logger to NULL), logs will be written to the
73 * stderr channel.
74 */
75 void
76 sdb_error_set_logger(int (*f)(int, const char *));
78 /*
79 * sdb_log, sdb_vlog:
80 * Log the specified message. The string will be formatted in printf-style
81 * using the specified format and arguments and logged with the specified
82 * priority. In addition, the error message will be stored as the current
83 * error message. This function is basically the same as calling sdb_error_set
84 * and sdb_error_log. XXX: SDB_LOG_EMERG might, at some point and/or depending
85 * on configuration, try a clean shut-down of the process.
86 */
87 int
88 sdb_log(int prio, const char *fmt, ...)
89 __attribute__((format(printf, 2, 3)));
90 int
91 sdb_vlog(int prio, const char *fmt, va_list ap);
93 /*
94 * sdb_error_set, sdb_error_append:
95 * Compose the current error message. The string will be formatted in printf-
96 * style using the specified format and arguments. No automatic logging will
97 * be done.
98 */
99 int
100 sdb_error_set(const char *fmt, ...)
101 __attribute__((format(printf, 1, 2)));
102 int
103 sdb_error_append(const char *fmt, ...)
104 __attribute__((format(printf, 1, 2)));
106 /*
107 * sdb_error_chomp:
108 * Remove all consecutive newline characters at the end of the error message.
109 */
110 int
111 sdb_error_chomp(void);
113 /*
114 * sdb_error_log:
115 * Log the current error message with the specified priority. See sdb_log for
116 * more information.
117 */
118 int
119 sdb_error_log(int prio);
121 /*
122 * sdb_error_get:
123 * Get the current error message. The string returned by this function is
124 * owned by SysDB and might point to static memory -- do not modify or free
125 * it.
126 */
127 const char *
128 sdb_error_get(void);
130 /*
131 * sdb_error_get_prio:
132 * Get the priority of the last logged error message -- see the SDB_LOG_
133 * constants for details.
134 */
135 int
136 sdb_error_get_prio(void);
138 /*
139 * sdb_error_parse_priority:
140 * Parse the name of a log priority.
141 *
142 * Returns:
143 * - the numeric log priority on success
144 * - a negative value else
145 */
146 int
147 sdb_error_parse_priority(char *prio);
149 /*
150 * sdb_strerror:
151 * This is a wrapper around the system's strerror function which ensures that
152 * a pointer to the formatted error message is returned.
153 */
154 char *
155 sdb_strerror(int errnum, char *strerrbuf, size_t buflen);
157 #ifdef __cplusplus
158 } /* extern "C" */
159 #endif
161 #endif /* ! SDB_UTILS_ERROR_H */
163 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */