1 |
|
|
/* $OpenBSD: log.c,v 1.4 2017/03/21 12:06:56 bluhm Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> |
5 |
|
|
* |
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. |
9 |
|
|
* |
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. |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include <sys/types.h> |
20 |
|
|
#include <sys/socket.h> |
21 |
|
|
#include <netinet/in.h> |
22 |
|
|
|
23 |
|
|
#include <errno.h> |
24 |
|
|
#include <netdb.h> |
25 |
|
|
#include <stdarg.h> |
26 |
|
|
#include <stdio.h> |
27 |
|
|
#include <stdlib.h> |
28 |
|
|
#include <string.h> |
29 |
|
|
#include <syslog.h> |
30 |
|
|
#include <time.h> |
31 |
|
|
|
32 |
|
|
#include "debugutil.h" |
33 |
|
|
#include "log.h" |
34 |
|
|
|
35 |
|
|
int debug; |
36 |
|
|
extern int debugsyslog; |
37 |
|
|
|
38 |
|
|
void |
39 |
|
|
log_init(int n_debug) |
40 |
|
|
{ |
41 |
|
|
extern char *__progname; |
42 |
|
|
|
43 |
|
|
debug = n_debug; |
44 |
|
|
|
45 |
|
|
if (!debug) |
46 |
|
|
openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); |
47 |
|
|
|
48 |
|
|
tzset(); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
void |
52 |
|
|
logit(int pri, const char *fmt, ...) |
53 |
|
|
{ |
54 |
|
|
va_list ap; |
55 |
|
|
|
56 |
|
|
va_start(ap, fmt); |
57 |
|
|
vlog(pri, fmt, ap); |
58 |
|
|
va_end(ap); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
void |
62 |
|
|
vlog(int pri, const char *fmt, va_list ap) |
63 |
|
|
{ |
64 |
|
|
vlog_printf(pri, fmt, ap); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
|
68 |
|
|
void |
69 |
|
|
log_warn(const char *emsg, ...) |
70 |
|
|
{ |
71 |
|
|
char *nfmt; |
72 |
|
|
va_list ap; |
73 |
|
|
|
74 |
|
|
/* best effort to even work in out of memory situations */ |
75 |
|
|
if (emsg == NULL) |
76 |
|
|
logit(LOG_ERR, "%s", strerror(errno)); |
77 |
|
|
else { |
78 |
|
|
va_start(ap, emsg); |
79 |
|
|
|
80 |
|
|
if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { |
81 |
|
|
/* we tried it... */ |
82 |
|
|
vlog(LOG_ERR, emsg, ap); |
83 |
|
|
logit(LOG_ERR, "%s", strerror(errno)); |
84 |
|
|
} else { |
85 |
|
|
vlog(LOG_ERR, nfmt, ap); |
86 |
|
|
free(nfmt); |
87 |
|
|
} |
88 |
|
|
va_end(ap); |
89 |
|
|
} |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
void |
93 |
|
|
log_warnx(const char *emsg, ...) |
94 |
|
|
{ |
95 |
|
|
va_list ap; |
96 |
|
|
|
97 |
|
|
va_start(ap, emsg); |
98 |
|
|
vlog(LOG_ERR, emsg, ap); |
99 |
|
|
va_end(ap); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
void |
103 |
|
|
log_info(const char *emsg, ...) |
104 |
|
|
{ |
105 |
|
|
va_list ap; |
106 |
|
|
|
107 |
|
|
va_start(ap, emsg); |
108 |
|
|
vlog(LOG_INFO, emsg, ap); |
109 |
|
|
va_end(ap); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
void |
113 |
|
|
log_debug(const char *emsg, ...) |
114 |
|
|
{ |
115 |
|
|
va_list ap; |
116 |
|
|
|
117 |
|
|
if (debug || debugsyslog) { |
118 |
|
|
va_start(ap, emsg); |
119 |
|
|
vlog(LOG_DEBUG, emsg, ap); |
120 |
|
|
va_end(ap); |
121 |
|
|
} |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
void |
125 |
|
|
fatal(const char *emsg) |
126 |
|
|
{ |
127 |
|
|
if (emsg == NULL) |
128 |
|
|
logit(LOG_CRIT, "fatal: %s", strerror(errno)); |
129 |
|
|
else |
130 |
|
|
if (errno) |
131 |
|
|
logit(LOG_CRIT, "fatal: %s: %s", |
132 |
|
|
emsg, strerror(errno)); |
133 |
|
|
else |
134 |
|
|
logit(LOG_CRIT, "fatal: %s", emsg); |
135 |
|
|
|
136 |
|
|
exit(1); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
void |
140 |
|
|
fatalx(const char *emsg) |
141 |
|
|
{ |
142 |
|
|
errno = 0; |
143 |
|
|
fatal(emsg); |
144 |
|
|
} |