|
|
@ -1,4 +1,4 @@ |
|
|
|
/* $OpenBSD: log.c,v 1.12 2015/12/19 13:58:08 reyk Exp $ */ |
|
|
|
/* $OpenBSD: log.c,v 1.13 2015/12/19 17:55:29 reyk Exp $ */ |
|
|
|
|
|
|
|
/* |
|
|
|
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> |
|
|
@ -11,49 +11,65 @@ |
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER |
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
|
|
*/ |
|
|
|
|
|
|
|
#include <sys/types.h> |
|
|
|
#include <sys/queue.h> |
|
|
|
#include <sys/tree.h> |
|
|
|
#include <sys/socket.h> |
|
|
|
|
|
|
|
#include <errno.h> |
|
|
|
#include <pwd.h> |
|
|
|
#include <stdarg.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdarg.h> |
|
|
|
#include <string.h> |
|
|
|
#include <syslog.h> |
|
|
|
#include <errno.h> |
|
|
|
#include <time.h> |
|
|
|
|
|
|
|
#include "log.h" |
|
|
|
|
|
|
|
#define TRACE_DEBUG 0x1 |
|
|
|
|
|
|
|
static int foreground; |
|
|
|
static int verbose; |
|
|
|
|
|
|
|
void vlog(int, const char *, va_list); |
|
|
|
void logit(int, const char *, ...) |
|
|
|
__attribute__((format (printf, 2, 3))); |
|
|
|
|
|
|
|
int debug; |
|
|
|
int verbose; |
|
|
|
const char *log_procname; |
|
|
|
|
|
|
|
void log_init(int, int); |
|
|
|
void log_procinit(const char *); |
|
|
|
void log_verbose(int); |
|
|
|
void log_warn(const char *, ...) |
|
|
|
__attribute__((__format__ (printf, 1, 2))); |
|
|
|
void log_warnx(const char *, ...) |
|
|
|
__attribute__((__format__ (printf, 1, 2))); |
|
|
|
void log_info(const char *, ...) |
|
|
|
__attribute__((__format__ (printf, 1, 2))); |
|
|
|
void log_debug(const char *, ...) |
|
|
|
__attribute__((__format__ (printf, 1, 2))); |
|
|
|
void logit(int, const char *, ...) |
|
|
|
__attribute__((__format__ (printf, 2, 3))); |
|
|
|
void vlog(int, const char *, va_list) |
|
|
|
__attribute__((__format__ (printf, 2, 0))); |
|
|
|
__dead void fatal(const char *, ...) |
|
|
|
__attribute__((__format__ (printf, 1, 2))); |
|
|
|
__dead void fatalx(const char *, ...) |
|
|
|
__attribute__((__format__ (printf, 1, 2))); |
|
|
|
|
|
|
|
void |
|
|
|
log_init(int n_foreground) |
|
|
|
log_init(int n_debug, int facility) |
|
|
|
{ |
|
|
|
extern char *__progname; |
|
|
|
|
|
|
|
foreground = n_foreground; |
|
|
|
if (! foreground) |
|
|
|
openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); |
|
|
|
debug = n_debug; |
|
|
|
verbose = n_debug; |
|
|
|
log_procinit(__progname); |
|
|
|
|
|
|
|
if (!debug) |
|
|
|
openlog(__progname, LOG_PID | LOG_NDELAY, facility); |
|
|
|
|
|
|
|
tzset(); |
|
|
|
} |
|
|
|
|
|
|
|
void |
|
|
|
log_procinit(const char *procname) |
|
|
|
{ |
|
|
|
if (procname != NULL) |
|
|
|
log_procname = procname; |
|
|
|
} |
|
|
|
|
|
|
|
void |
|
|
|
log_verbose(int v) |
|
|
|
{ |
|
|
@ -75,7 +91,7 @@ vlog(int pri, const char *fmt, va_list ap) |
|
|
|
{ |
|
|
|
char *nfmt; |
|
|
|
|
|
|
|
if (foreground) { |
|
|
|
if (debug) { |
|
|
|
/* best effort in out of mem situations */ |
|
|
|
if (asprintf(&nfmt, "%s\n", fmt) == -1) { |
|
|
|
vfprintf(stderr, fmt, ap); |
|
|
@ -139,19 +155,7 @@ log_debug(const char *emsg, ...) |
|
|
|
{ |
|
|
|
va_list ap; |
|
|
|
|
|
|
|
if (verbose & TRACE_DEBUG) { |
|
|
|
va_start(ap, emsg); |
|
|
|
vlog(LOG_DEBUG, emsg, ap); |
|
|
|
va_end(ap); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void |
|
|
|
log_trace(int mask, const char *emsg, ...) |
|
|
|
{ |
|
|
|
va_list ap; |
|
|
|
|
|
|
|
if (verbose & mask) { |
|
|
|
if (verbose > 1) { |
|
|
|
va_start(ap, emsg); |
|
|
|
vlog(LOG_DEBUG, emsg, ap); |
|
|
|
va_end(ap); |
|
|
@ -159,23 +163,23 @@ log_trace(int mask, const char *emsg, ...) |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
fatal_arg(const char *emsg, va_list ap) |
|
|
|
vfatal(const char *emsg, va_list ap) |
|
|
|
{ |
|
|
|
#define FATALBUFSIZE 1024 |
|
|
|
static char ebuffer[FATALBUFSIZE]; |
|
|
|
|
|
|
|
if (emsg == NULL) |
|
|
|
(void)strlcpy(ebuffer, strerror(errno), sizeof ebuffer); |
|
|
|
else { |
|
|
|
if (errno) { |
|
|
|
(void)vsnprintf(ebuffer, sizeof ebuffer, emsg, ap); |
|
|
|
(void)strlcat(ebuffer, ": ", sizeof ebuffer); |
|
|
|
(void)strlcat(ebuffer, strerror(errno), sizeof ebuffer); |
|
|
|
} |
|
|
|
else |
|
|
|
(void)vsnprintf(ebuffer, sizeof ebuffer, emsg, ap); |
|
|
|
static char s[BUFSIZ]; |
|
|
|
const char *sep; |
|
|
|
|
|
|
|
if (emsg != NULL) { |
|
|
|
(void)vsnprintf(s, sizeof(s), emsg, ap); |
|
|
|
sep = ": "; |
|
|
|
} else { |
|
|
|
s[0] = '\0'; |
|
|
|
sep = ""; |
|
|
|
} |
|
|
|
logit(LOG_CRIT, "fatal: %s", ebuffer); |
|
|
|
if (errno) |
|
|
|
logit(LOG_CRIT, "%s: %s%s%s", |
|
|
|
log_procname, s, sep, strerror(errno)); |
|
|
|
else |
|
|
|
logit(LOG_CRIT, "%s%s%s", log_procname, sep, s); |
|
|
|
} |
|
|
|
|
|
|
|
void |
|
|
@ -184,7 +188,7 @@ fatal(const char *emsg, ...) |
|
|
|
va_list ap; |
|
|
|
|
|
|
|
va_start(ap, emsg); |
|
|
|
fatal_arg(emsg, ap); |
|
|
|
vfatal(emsg, ap); |
|
|
|
va_end(ap); |
|
|
|
exit(1); |
|
|
|
} |
|
|
@ -196,7 +200,7 @@ fatalx(const char *emsg, ...) |
|
|
|
|
|
|
|
errno = 0; |
|
|
|
va_start(ap, emsg); |
|
|
|
fatal_arg(emsg, ap); |
|
|
|
vfatal(emsg, ap); |
|
|
|
va_end(ap); |
|
|
|
exit(1); |
|
|
|
} |