]>
Commit | Line | Data |
---|---|---|
43c02e7b VB |
1 | /* $OpenBSD: log.c,v 1.11 2007/12/07 17:17:00 reyk 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 "lldpd.h" | |
20 | ||
21 | #include <sys/types.h> | |
22 | ||
23 | #include <errno.h> | |
24 | #include <stdarg.h> | |
25 | #include <stdio.h> | |
26 | #include <syslog.h> | |
27 | #include <time.h> | |
28 | ||
8888d191 | 29 | static int debug; |
43c02e7b | 30 | |
8888d191 VB |
31 | static void vlog(int, const char *, va_list); |
32 | static void logit(int, const char *, ...); | |
43c02e7b VB |
33 | |
34 | void | |
6bb9c4e0 | 35 | log_init(int n_debug, const char *progname) |
43c02e7b | 36 | { |
43c02e7b VB |
37 | debug = n_debug; |
38 | ||
39 | if (!debug) | |
6bb9c4e0 | 40 | openlog(progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); |
43c02e7b VB |
41 | |
42 | tzset(); | |
43 | } | |
44 | ||
8888d191 | 45 | static void |
43c02e7b VB |
46 | logit(int pri, const char *fmt, ...) |
47 | { | |
48 | va_list ap; | |
49 | ||
50 | va_start(ap, fmt); | |
51 | vlog(pri, fmt, ap); | |
52 | va_end(ap); | |
53 | } | |
54 | ||
8888d191 | 55 | static void |
43c02e7b VB |
56 | vlog(int pri, const char *fmt, va_list ap) |
57 | { | |
58 | char *nfmt; | |
59 | ||
60 | if (debug) { | |
61 | /* best effort in out of mem situations */ | |
62 | if (asprintf(&nfmt, "%s\n", fmt) == -1) { | |
63 | vfprintf(stderr, fmt, ap); | |
64 | fprintf(stderr, "\n"); | |
65 | } else { | |
66 | vfprintf(stderr, nfmt, ap); | |
67 | free(nfmt); | |
68 | } | |
69 | fflush(stderr); | |
70 | } else | |
71 | vsyslog(pri, fmt, ap); | |
72 | } | |
73 | ||
74 | ||
75 | void | |
76 | log_warn(const char *emsg, ...) | |
77 | { | |
78 | char *nfmt; | |
79 | va_list ap; | |
80 | ||
81 | /* best effort to even work in out of memory situations */ | |
82 | if (emsg == NULL) | |
83 | logit(LOG_CRIT, "%s", strerror(errno)); | |
84 | else { | |
85 | va_start(ap, emsg); | |
86 | ||
87 | if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { | |
88 | /* we tried it... */ | |
89 | vlog(LOG_CRIT, emsg, ap); | |
90 | logit(LOG_CRIT, "%s", strerror(errno)); | |
91 | } else { | |
92 | vlog(LOG_CRIT, nfmt, ap); | |
93 | free(nfmt); | |
94 | } | |
95 | va_end(ap); | |
96 | } | |
97 | } | |
98 | ||
99 | void | |
100 | log_warnx(const char *emsg, ...) | |
101 | { | |
102 | va_list ap; | |
103 | ||
104 | va_start(ap, emsg); | |
105 | vlog(LOG_CRIT, emsg, ap); | |
106 | va_end(ap); | |
107 | } | |
108 | ||
109 | void | |
110 | log_info(const char *emsg, ...) | |
111 | { | |
112 | va_list ap; | |
113 | ||
114 | va_start(ap, emsg); | |
115 | vlog(LOG_INFO, emsg, ap); | |
116 | va_end(ap); | |
117 | } | |
118 | ||
119 | void | |
120 | log_debug(const char *emsg, ...) | |
121 | { | |
122 | va_list ap; | |
123 | ||
124 | if (debug > 1) { | |
125 | va_start(ap, emsg); | |
126 | vlog(LOG_DEBUG, emsg, ap); | |
127 | va_end(ap); | |
128 | } | |
129 | } | |
130 | ||
131 | void | |
132 | fatal(const char *emsg) | |
133 | { | |
134 | if (emsg == NULL) | |
135 | logit(LOG_CRIT, "fatal: %s", strerror(errno)); | |
136 | else | |
137 | if (errno) | |
138 | logit(LOG_CRIT, "fatal: %s: %s", | |
139 | emsg, strerror(errno)); | |
140 | else | |
141 | logit(LOG_CRIT, "fatal: %s", emsg); | |
142 | ||
143 | exit(1); | |
144 | } | |
145 | ||
146 | void | |
147 | fatalx(const char *emsg) | |
148 | { | |
149 | errno = 0; | |
150 | fatal(emsg); | |
151 | } |