4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
30 /* Log file, if needed. */
36 void log_event_cb(int, const char *);
37 void log_vwrite(const char *, va_list) __printflike(1, 0);
38 __dead
void log_vfatal(const char *, va_list);
40 /* Log callback for libevent. */
42 log_event_cb(unused
int severity
, const char *msg
)
47 /* Open logging to file. */
49 log_open(int level
, const char *path
)
51 log_file
= fopen(path
, "w");
57 event_set_log_callback(log_event_cb
);
69 event_set_log_callback(NULL
);
72 /* Write a log message. */
74 log_vwrite(const char *msg
, va_list ap
)
79 if (vfprintf(log_file
, msg
, ap
) == -1)
81 if (fprintf(log_file
, "\n") == -1)
86 /* Log a warning with error string. */
87 #if __GNUC_PREREQ__(4, 6) || defined(__clang__)
88 #pragma GCC diagnostic push
90 #if __GNUC_PREREQ__(4, 5) || defined(__clang__)
91 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
94 log_warn(const char *msg
, ...)
100 if (asprintf(&fmt
, "%s: %s", msg
, strerror(errno
)) == -1)
106 #if __GNUC_PREREQ__(4, 6) || defined(__clang__)
107 #pragma GCC diagnostic push
112 log_warnx(const char *msg
, ...)
121 /* Log an informational message. */
123 log_info(const char *msg
, ...)
127 if (log_level
> -1) {
134 /* Log a debug message. */
136 log_debug(const char *msg
, ...)
147 /* Log a debug message at level 2. */
149 log_debug2(const char *msg
, ...)
160 /* Log a critical error, with error string if necessary, and die. */
162 log_vfatal(const char *msg
, va_list ap
)
167 if (asprintf(&fmt
, "fatal: %s: %s", msg
, strerror(errno
)) == -1)
171 if (asprintf(&fmt
, "fatal: %s", msg
) == -1)
180 /* Log a critical error, with error string, and die. */
181 __dead
void printflike1
182 log_fatal(const char *msg
, ...)
190 /* Log a critical error and die. */
191 __dead
void printflike1
192 log_fatalx(const char *msg
, ...)