1 /* $OpenBSD: log.c,v 1.7 2005/03/31 12:14:01 henning Exp $ */
2
3 /*
4 * Copyright (c) 2007, 2013 Thorsten Glaser <tg@mirbsd.de>
5 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <syslog.h>
26 #include <time.h>
27
28 #include "ntpd.h"
29
30 __RCSID("$MirOS: src/usr.sbin/ntpd/log.c,v 1.7 2013/10/31 20:07:28 tg Exp $");
31
32 int debug;
33
34 void logit(int, const char *, ...)
35 __attribute__((__format__(__syslog__, 2, 3)))
36 __attribute__((__format__(__printf__, 2, 3)))
37 __attribute__((__nonnull__(2)));
38
39 void
log_init(int n_debug)40 log_init(int n_debug)
41 {
42 extern char *__progname;
43
44 debug = n_debug;
45
46 if (!debug)
47 openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
48
49 tzset();
50 }
51
52 void
logit(int pri,const char * fmt,...)53 logit(int pri, const char *fmt, ...)
54 {
55 va_list ap;
56
57 va_start(ap, fmt);
58 vlog(pri, fmt, ap);
59 va_end(ap);
60 }
61
62 void
vlog(int pri,const char * fmt,va_list ap)63 vlog(int pri, const char *fmt, va_list ap)
64 {
65 char *nfmt;
66
67 if (debug) {
68 /* best effort in out of mem situations */
69 if (asprintf(&nfmt, "%s\n", fmt) == -1) {
70 vfprintf(stderr, fmt, ap);
71 fprintf(stderr, "\n");
72 } else {
73 vfprintf(stderr, nfmt, ap);
74 free(nfmt);
75 }
76 fflush(stderr);
77 } else
78 vsyslog(pri, fmt, ap);
79 }
80
81
82 void
log_warn(const char * emsg,...)83 log_warn(const char *emsg, ...)
84 {
85 char *nfmt;
86 va_list ap;
87
88 /* best effort to even work in out of memory situations */
89 if (emsg == NULL)
90 logit(LOG_CRIT, "%s", strerror(errno));
91 else {
92 va_start(ap, emsg);
93
94 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
95 /* we tried it... */
96 vlog(LOG_CRIT, emsg, ap);
97 logit(LOG_CRIT, "%s", strerror(errno));
98 } else {
99 vlog(LOG_CRIT, nfmt, ap);
100 free(nfmt);
101 }
102 va_end(ap);
103 }
104 }
105
106 void
log_warnx(const char * emsg,...)107 log_warnx(const char *emsg, ...)
108 {
109 va_list ap;
110
111 va_start(ap, emsg);
112 vlog(LOG_CRIT, emsg, ap);
113 va_end(ap);
114 }
115
116 void
log_info(const char * emsg,...)117 log_info(const char *emsg, ...)
118 {
119 va_list ap;
120
121 va_start(ap, emsg);
122 vlog(LOG_INFO, emsg, ap);
123 va_end(ap);
124 }
125
126 void
log_debug(const char * emsg,...)127 log_debug(const char *emsg, ...)
128 {
129 va_list ap;
130
131 if (debug) {
132 va_start(ap, emsg);
133 vlog(LOG_DEBUG, emsg, ap);
134 va_end(ap);
135 }
136 }
137
138 void
fatal(const char * emsg)139 fatal(const char *emsg)
140 {
141 if (emsg == NULL)
142 logit(LOG_CRIT, "fatal: %s", strerror(errno));
143 else
144 if (errno)
145 logit(LOG_CRIT, "fatal: %s: %s",
146 emsg, strerror(errno));
147 else
148 logit(LOG_CRIT, "fatal: %s", emsg);
149
150 exit(1);
151 }
152
153 void
fatalx(const char * emsg)154 fatalx(const char *emsg)
155 {
156 errno = 0;
157 fatal(emsg);
158 }
159