8b2a177160c08102632675331a931da881114f82
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. XXX: currently, SysDB only supports printing the error to
35 * the standard error channel; support for other logging backends will be
36 * added later in a modular fashion.
37 */
39 #ifndef SDB_UTILS_ERROR_H
40 #define SDB_UTILS_ERROR_H 1
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
46 /* max length of any error message */
47 #ifndef SDB_MAX_ERROR
48 # define SDB_MAX_ERROR 4096
49 #endif /* ! SDB_MAX_ERROR */
51 /* On Linux systems and possibly others, this should be the same as the LOG_
52 * constants defined by syslog. */
53 enum {
54 SDB_LOG_EMERG = 0,
55 SDB_LOG_ERR = 3,
56 SDB_LOG_WARNING = 4,
57 SDB_LOG_NOTICE = 5,
58 SDB_LOG_INFO = 6,
59 SDB_LOG_DEBUG = 7,
60 };
61 #define SDB_LOG_PRIO_TO_STRING(prio) \
62 (((prio) == SDB_LOG_EMERG) ? "EMERG" \
63 : ((prio) == SDB_LOG_ERR) ? "ERR" \
64 : ((prio) == SDB_LOG_WARNING) ? "WARNING" \
65 : ((prio) == SDB_LOG_NOTICE) ? "NOTICE" \
66 : ((prio) == SDB_LOG_INFO) ? "INFO" \
67 : ((prio) == SDB_LOG_DEBUG) ? "DEBUG" : "UNKNOWN")
69 /*
70 * sdb_error_set:
71 * Set the current error message. The string will be formated in printf-style
72 * using the specified format and arguments and logged with the specified
73 * priority. XXX: SDB_LOG_EMERG might, at some point and/or depending on
74 * configuration, try a clean shut-down of the process.
75 */
76 int
77 sdb_error_set(int prio, const char *fmt, ...);
79 /*
80 * sdb_error_start, sdb_error_append, sdb_error_end:
81 * Compose the current error message from multiple parts. The error message
82 * will only be logged after calling sdb_error_finish().
83 * See sdb_error_set for details.
84 */
85 int
86 sdb_error_start(int prio, const char *fmt, ...);
87 int
88 sdb_error_append(const char *fmt, ...);
89 int
90 sdb_error_finish(void);
92 /*
93 * sdb_error_get:
94 * Get the current error message. The string returned by this function is
95 * owned by SysDB and might point to static memory -- do not modify or free
96 * it.
97 */
98 const char *
99 sdb_error_get(void);
101 /*
102 * sdb_error_get_prio:
103 * Get the priority of the current error message -- see the SDB_LOG_ constants
104 * for details.
105 */
106 int
107 sdb_error_get_prio(void);
109 #ifdef __cplusplus
110 } /* extern "C" */
111 #endif
113 #endif /* ! SDB_UTILS_ERROR_H */
115 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */