Code

Added __attribute__((format(printf, ...))) where appropriate.
[sysdb.git] / src / include / utils / error.h
index 8b2a177160c08102632675331a931da881114f82..2c3df32ff48a98c2fe7f26e03fe1593c55acbb82 100644 (file)
  * will use its own memory region to store information about the last reported
  * error.
  * Once the error message has been passed to SysDB, it will log the entire
- * message at once. XXX: currently, SysDB only supports printing the error to
- * the standard error channel; support for other logging backends will be
- * added later in a modular fashion.
+ * message at once. The message will be sent to all registered log functions.
  */
 
 #ifndef SDB_UTILS_ERROR_H
 #define SDB_UTILS_ERROR_H 1
 
+#include <stddef.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-/* max length of any error message */
-#ifndef SDB_MAX_ERROR
-#      define SDB_MAX_ERROR 4096
-#endif /* ! SDB_MAX_ERROR */
-
 /* On Linux systems and possibly others, this should be the same as the LOG_
  * constants defined by syslog. */
 enum {
@@ -60,34 +55,61 @@ enum {
 };
 #define SDB_LOG_PRIO_TO_STRING(prio) \
        (((prio) == SDB_LOG_EMERG) ? "EMERG" \
-               : ((prio) == SDB_LOG_ERR) ? "ERR" \
+               : ((prio) == SDB_LOG_ERR) ? "ERROR" \
                : ((prio) == SDB_LOG_WARNING) ? "WARNING" \
                : ((prio) == SDB_LOG_NOTICE) ? "NOTICE" \
                : ((prio) == SDB_LOG_INFO) ? "INFO" \
                : ((prio) == SDB_LOG_DEBUG) ? "DEBUG" : "UNKNOWN")
 
 /*
- * sdb_error_set:
- * Set the current error message. The string will be formated in printf-style
+ * sdb_error_set_logger:
+ * Set the logging callback to be used for logging messages. By default (or
+ * when explicitely setting the logger to NULL), logs will be written to the
+ * stderr channel.
+ */
+void
+sdb_error_set_logger(int (*f)(int, const char *));
+
+/*
+ * sdb_log:
+ * Log the specified message. The string will be formatted in printf-style
  * using the specified format and arguments and logged with the specified
- * priority. XXX: SDB_LOG_EMERG might, at some point and/or depending on
- * configuration, try a clean shut-down of the process.
+ * priority. In addition, the error message will be stored as the current
+ * error message. This function is basically the same as calling sdb_error_set
+ * and sdb_error_log. XXX: SDB_LOG_EMERG might, at some point and/or depending
+ * on configuration, try a clean shut-down of the process.
  */
 int
-sdb_error_set(int prio, const char *fmt, ...);
+sdb_log(int prio, const char *fmt, ...)
+               __attribute__((format(printf, 2, 3)));
 
 /*
- * sdb_error_start, sdb_error_append, sdb_error_end:
- * Compose the current error message from multiple parts. The error message
- * will only be logged after calling sdb_error_finish().
- * See sdb_error_set for details.
+ * sdb_error_set, sdb_error_append:
+ * Compose the current error message. The string will be formatted in printf-
+ * style using the specified format and arguments. No automatic logging will
+ * be done.
  */
 int
-sdb_error_start(int prio, const char *fmt, ...);
+sdb_error_set(const char *fmt, ...)
+               __attribute__((format(printf, 1, 2)));
 int
-sdb_error_append(const char *fmt, ...);
+sdb_error_append(const char *fmt, ...)
+               __attribute__((format(printf, 1, 2)));
+
+/*
+ * sdb_error_chomp:
+ * Remove all consecutive newline characters at the end of the error message.
+ */
 int
-sdb_error_finish(void);
+sdb_error_chomp(void);
+
+/*
+ * sdb_error_log:
+ * Log the current error message with the specified priority. See sdb_log for
+ * more information.
+ */
+int
+sdb_error_log(int prio);
 
 /*
  * sdb_error_get:
@@ -100,12 +122,20 @@ sdb_error_get(void);
 
 /*
  * sdb_error_get_prio:
- * Get the priority of the current error message -- see the SDB_LOG_ constants
- * for details.
+ * Get the priority of the last logged error message -- see the SDB_LOG_
+ * constants for details.
  */
 int
 sdb_error_get_prio(void);
 
+/*
+ * sdb_strerror:
+ * This is a wrapper around the system's strerror function which ensures that
+ * a pointer to the formatted error message is returned.
+ */
+char *
+sdb_strerror(int errnum, char *strerrbuf, size_t buflen);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif