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);
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
)
81 if (asprintf(&fmt
, "%s\n", msg
) == -1)
83 if (vfprintf(log_file
, fmt
, ap
) == -1)
89 /* Log a warning with error string. */
91 log_warn(const char *msg
, ...)
97 if (asprintf(&fmt
, "%s: %s", msg
, strerror(errno
)) == -1)
106 log_warnx(const char *msg
, ...)
115 /* Log an informational message. */
117 log_info(const char *msg
, ...)
121 if (log_level
> -1) {
128 /* Log a debug message. */
130 log_debug(const char *msg
, ...)
141 /* Log a debug message at level 2. */
143 log_debug2(const char *msg
, ...)
154 /* Log a critical error, with error string if necessary, and die. */
156 log_vfatal(const char *msg
, va_list ap
)
161 if (asprintf(&fmt
, "fatal: %s: %s", msg
, strerror(errno
)) == -1)
165 if (asprintf(&fmt
, "fatal: %s", msg
) == -1)
174 /* Log a critical error, with error string, and die. */
175 __dead
void printflike1
176 log_fatal(const char *msg
, ...)
184 /* Log a critical error and die. */
185 __dead
void printflike1
186 log_fatalx(const char *msg
, ...)