Code

Merged branch 'master' of git://git.tokkee.org/sysdb.git.
[sysdb.git] / src / include / core / error.h
1 /*
2  * SysDB - src/include/core/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_CORE_ERROR_H
40 #define SDB_CORE_ERROR_H 1
42 #include <stddef.h>
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
48 /* max length of any error message */
49 #ifndef SDB_MAX_ERROR
50 #       define SDB_MAX_ERROR 4096
51 #endif /* ! SDB_MAX_ERROR */
53 /* On Linux systems and possibly others, this should be the same as the LOG_
54  * constants defined by syslog. */
55 enum {
56         SDB_LOG_EMERG   = 0,
57         SDB_LOG_ERR     = 3,
58         SDB_LOG_WARNING = 4,
59         SDB_LOG_NOTICE  = 5,
60         SDB_LOG_INFO    = 6,
61         SDB_LOG_DEBUG   = 7,
62 };
63 #define SDB_LOG_PRIO_TO_STRING(prio) \
64         (((prio) == SDB_LOG_EMERG) ? "EMERG" \
65                 : ((prio) == SDB_LOG_ERR) ? "ERROR" \
66                 : ((prio) == SDB_LOG_WARNING) ? "WARNING" \
67                 : ((prio) == SDB_LOG_NOTICE) ? "NOTICE" \
68                 : ((prio) == SDB_LOG_INFO) ? "INFO" \
69                 : ((prio) == SDB_LOG_DEBUG) ? "DEBUG" : "UNKNOWN")
71 /*
72  * sdb_log:
73  * Log the specified message. The string will be formatted in printf-style
74  * using the specified format and arguments and logged with the specified
75  * priority. In addition, the error message will be stored as the current
76  * error message. This function is basically the same as calling sdb_error_set
77  * and sdb_error_log. XXX: SDB_LOG_EMERG might, at some point and/or depending
78  * on configuration, try a clean shut-down of the process.
79  */
80 int
81 sdb_log(int prio, const char *fmt, ...);
83 /*
84  * sdb_error_set, sdb_error_append:
85  * Compose the current error message. The string will be formatted in printf-
86  * style using the specified format and arguments. No automatic logging will
87  * be done.
88  */
89 int
90 sdb_error_set(const char *fmt, ...);
91 int
92 sdb_error_append(const char *fmt, ...);
94 /*
95  * sdb_error_chomp:
96  * Remove all consecutive newline characters at the end of the error message.
97  */
98 int
99 sdb_error_chomp(void);
101 /*
102  * sdb_error_log:
103  * Log the current error message with the specified priority. See sdb_log for
104  * more information.
105  */
106 int
107 sdb_error_log(int prio);
109 /*
110  * sdb_error_get:
111  * Get the current error message. The string returned by this function is
112  * owned by SysDB and might point to static memory -- do not modify or free
113  * it.
114  */
115 const char *
116 sdb_error_get(void);
118 /*
119  * sdb_error_get_prio:
120  * Get the priority of the last logged error message -- see the SDB_LOG_
121  * constants for details.
122  */
123 int
124 sdb_error_get_prio(void);
126 /*
127  * sdb_strerror:
128  * This is a wrapper around the system's strerror function which ensures that
129  * a pointer to the formatted error message is returned.
130  */
131 char *
132 sdb_strerror(int errnum, char *strerrbuf, size_t buflen);
134 #ifdef __cplusplus
135 } /* extern "C" */
136 #endif
138 #endif /* ! SDB_CORE_ERROR_H */
140 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */