Merge from vendor branch GCC:
[dragonfly.git] / contrib / ntpd / log.c
1 /*      $OpenBSD: src/usr.sbin/ntpd/log.c,v 1.7 2005/03/31 12:14:01 henning 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 <errno.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <syslog.h>
25 #include <time.h>
26
27 #include "ntpd.h"
28
29 int      debug;
30
31 void     logit(int, const char *, ...);
32
33 void
34 log_init(int n_debug)
35 {
36         extern char     *__progname;
37
38         debug = n_debug;
39
40         if (!debug)
41                 openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
42
43         tzset();
44 }
45
46 void
47 logit(int pri, const char *fmt, ...)
48 {
49         va_list ap;
50
51         va_start(ap, fmt);
52         vlog(pri, fmt, ap);
53         va_end(ap);
54 }
55
56 void
57 vlog(int pri, const char *fmt, va_list ap)
58 {
59         char    *nfmt;
60
61         if (debug) {
62                 /* best effort in out of mem situations */
63                 if (asprintf(&nfmt, "%s\n", fmt) == -1) {
64                         vfprintf(stderr, fmt, ap);
65                         fprintf(stderr, "\n");
66                 } else {
67                         vfprintf(stderr, nfmt, ap);
68                         free(nfmt);
69                 }
70                 fflush(stderr);
71         } else
72                 vsyslog(pri, fmt, ap);
73 }
74
75
76 void
77 log_warn(const char *emsg, ...)
78 {
79         char    *nfmt;
80         va_list  ap;
81
82         /* best effort to even work in out of memory situations */
83         if (emsg == NULL)
84                 logit(LOG_CRIT, "%s", strerror(errno));
85         else {
86                 va_start(ap, emsg);
87
88                 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
89                         /* we tried it... */
90                         vlog(LOG_CRIT, emsg, ap);
91                         logit(LOG_CRIT, "%s", strerror(errno));
92                 } else {
93                         vlog(LOG_CRIT, nfmt, ap);
94                         free(nfmt);
95                 }
96                 va_end(ap);
97         }
98 }
99
100 void
101 log_warnx(const char *emsg, ...)
102 {
103         va_list  ap;
104
105         va_start(ap, emsg);
106         vlog(LOG_CRIT, emsg, ap);
107         va_end(ap);
108 }
109
110 void
111 log_info(const char *emsg, ...)
112 {
113         va_list  ap;
114
115         va_start(ap, emsg);
116         vlog(LOG_INFO, emsg, ap);
117         va_end(ap);
118 }
119
120 void
121 log_debug(const char *emsg, ...)
122 {
123         va_list  ap;
124
125         if (debug) {
126                 va_start(ap, emsg);
127                 vlog(LOG_DEBUG, emsg, ap);
128                 va_end(ap);
129         }
130 }
131
132 void
133 fatal(const char *emsg)
134 {
135         if (emsg == NULL)
136                 logit(LOG_CRIT, "fatal: %s", strerror(errno));
137         else
138                 if (errno)
139                         logit(LOG_CRIT, "fatal: %s: %s",
140                             emsg, strerror(errno));
141                 else
142                         logit(LOG_CRIT, "fatal: %s", emsg);
143
144         exit(1);
145 }
146
147 void
148 fatalx(const char *emsg)
149 {
150         errno = 0;
151         fatal(emsg);
152 }
153
154 const char *
155 log_sockaddr(struct sockaddr *sa)
156 {
157         static char     buf[NI_MAXHOST];
158
159         if (getnameinfo(sa, SA_LEN(sa), buf, sizeof(buf), NULL, 0,
160             NI_NUMERICHOST))
161                 return ("(unknown)");
162         else
163                 return (buf);
164 }