Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libntp / msyslog.c
blobf99f52cdd5fd6ab77dff6ed229c1a2f449ada0d0
1 /* $NetBSD: msyslog.c,v 1.3 2003/12/04 16:23:37 drochner Exp $ */
3 /*
4 * msyslog - either send a message to the terminal or print it on
5 * the standard output.
7 * Converted to use varargs, much better ... jks
8 */
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
14 #ifdef HAVE_SYS_TYPES_H
15 # include <sys/types.h>
16 #endif
17 #ifdef HAVE_UNISTD_H
18 # include <unistd.h>
19 #endif
21 #include <stdio.h>
23 #include "ntp_types.h"
24 #include "ntp_string.h"
25 #include "ntp_syslog.h"
26 #include "ntp_stdlib.h"
28 #ifdef SYS_WINNT
29 # include <stdarg.h>
30 # include "..\ports\winnt\libntp\messages.h"
31 #endif
33 int syslogit = 1;
35 FILE *syslog_file = NULL;
37 u_long ntp_syslogmask = ~ (u_long) 0;
39 #ifdef SYS_WINNT
40 static char separator = '\\';
41 #else
42 static char separator = '/';
43 #endif /* SYS_WINNT */
44 extern char *progname;
46 /* Declare the local functions */
47 void addto_syslog P((int, char *));
48 void format_errmsg P((char *, int, const char *, int));
52 * This routine adds the contents of a buffer to the log
54 void
55 addto_syslog(int level, char * buf)
57 char *prog;
58 FILE *out_file = syslog_file;
60 #if !defined(VMS) && !defined (SYS_VXWORKS)
61 if (syslogit)
62 syslog(level, "%s", buf);
63 else
64 #endif /* VMS && SYS_VXWORKS*/
66 out_file = syslog_file ? syslog_file: level <= LOG_ERR ? stderr : stdout;
67 /* syslog() provides the timestamp, so if we're not using
68 syslog, we must provide it. */
69 prog = strrchr(progname, separator);
70 if (prog == NULL)
71 prog = progname;
72 else
73 prog++;
74 (void) fprintf(out_file, "%s ", humanlogtime ());
75 (void) fprintf(out_file, "%s[%d]: %s", prog, (int)getpid(), buf);
76 fflush (out_file);
78 #if DEBUG
79 if (debug && out_file != stdout && out_file != stderr)
80 printf("addto_syslog: %s\n", buf);
81 #endif
83 void
84 format_errmsg(char *nfmt, int lennfmt, const char *fmt, int errval)
86 register char c;
87 register char *n;
88 register const char *f;
90 char *err;
92 n = nfmt;
93 f = fmt;
94 while ((c = *f++) != '\0' && n < (nfmt+lennfmt - 2)) {
95 if (c != '%') {
96 *n++ = c;
97 continue;
99 if ((c = *f++) != 'm') {
100 *n++ = '%';
101 *n++ = c;
102 continue;
104 err = 0;
105 err = strerror(errval);
106 /* Make sure we have enough space for the error message */
107 if ((n + strlen(err)) < (nfmt + lennfmt -2)) {
108 strcpy(n, err);
109 n += strlen(err);
112 #if !defined(VMS)
113 if (!syslogit)
114 #endif /* VMS */
115 *n++ = '\n';
116 *n = '\0';
120 * The externally called functions are defined here
121 * but share the internal function above to fetch
122 * any error message strings, This is done so that we can
123 * have two different functions to perform the logging
124 * since Windows gets it's error information from different
125 * places depending on whether or not it's network I/O.
126 * msyslog() is for general use while netsyslog() is for
127 * network I/O functions. They are virtually identical
128 * in implementation.
131 #if defined(__STDC__) || defined(HAVE_STDARG_H)
132 void msyslog(int level, const char *fmt, ...)
133 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
134 /*VARARGS*/
135 void msyslog(va_alist)
136 va_dcl
137 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
139 #if defined(__STDC__) || defined(HAVE_STDARG_H)
140 #else
141 int level;
142 const char *fmt;
143 #endif
144 va_list ap;
145 char buf[1025], nfmt[256];
148 * Save the error value as soon as possible
150 #ifdef SYS_WINNT
151 int errval = GetLastError();
152 #else
153 int errval = errno;
154 #endif
156 #if defined(__STDC__) || defined(HAVE_STDARG_H)
157 va_start(ap, fmt);
158 #else
159 va_start(ap);
161 level = va_arg(ap, int);
162 fmt = va_arg(ap, char *);
163 #endif
164 format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
166 vsnprintf(buf, sizeof(buf), nfmt, ap);
167 addto_syslog(level, buf);
168 va_end(ap);
170 #if defined(__STDC__) || defined(HAVE_STDARG_H)
171 void netsyslog(int level, const char *fmt, ...)
172 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
173 /*VARARGS*/
174 void netsyslog(va_alist)
175 va_dcl
176 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
178 #if defined(__STDC__) || defined(HAVE_STDARG_H)
179 #else
180 int level;
181 const char *fmt;
182 #endif
183 va_list ap;
184 char buf[1025], nfmt[256];
187 * Save the error value as soon as possible
189 #ifdef SYS_WINNT
190 int errval = WSAGetLastError();
191 #else
192 int errval = errno;
193 #endif
195 #if defined(__STDC__) || defined(HAVE_STDARG_H)
196 va_start(ap, fmt);
197 #else
198 va_start(ap);
200 level = va_arg(ap, int);
201 fmt = va_arg(ap, char *);
202 #endif
203 format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
205 vsnprintf(buf, sizeof(buf), nfmt, ap);
206 addto_syslog(level, buf);
207 va_end(ap);