Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / libntp / msyslog.c
blob672798dcae773a62565d57a188be383ad8550881
1 /* $NetBSD$ */
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 #include <sys/types.h>
15 #ifdef HAVE_UNISTD_H
16 # include <unistd.h>
17 #endif
19 #include <stdio.h>
21 #include "ntp_types.h"
22 #include "ntp_string.h"
23 #include "ntp_syslog.h"
24 #include "ntp_stdlib.h"
26 #ifdef SYS_WINNT
27 # include <stdarg.h>
28 # include "..\ports\winnt\libntp\messages.h"
29 #endif
31 int syslogit = 1;
33 FILE *syslog_file = NULL;
35 u_long ntp_syslogmask = ~ (u_long) 0;
37 #ifdef SYS_WINNT
38 static char separator = '\\';
39 #else
40 static char separator = '/';
41 #endif /* SYS_WINNT */
42 extern char *progname;
44 /* Declare the local functions */
45 void addto_syslog (int, char *);
46 void format_errmsg (char *, int, const char *, int);
50 * This routine adds the contents of a buffer to the log
52 void
53 addto_syslog(int level, char * buf)
55 char *prog;
56 FILE *out_file = syslog_file;
58 #if !defined(VMS) && !defined (SYS_VXWORKS)
59 if (syslogit)
60 syslog(level, "%s", buf);
61 else
62 #endif /* VMS && SYS_VXWORKS*/
64 out_file = syslog_file ? syslog_file: level <= LOG_ERR ? stderr : stdout;
65 /* syslog() provides the timestamp, so if we're not using
66 syslog, we must provide it. */
67 prog = strrchr(progname, separator);
68 if (prog == NULL)
69 prog = progname;
70 else
71 prog++;
72 (void) fprintf(out_file, "%s ", humanlogtime ());
73 (void) fprintf(out_file, "%s[%d]: %s", prog, (int)getpid(), buf);
74 fflush (out_file);
76 #if DEBUG
77 if (debug && out_file != stdout && out_file != stderr)
78 printf("addto_syslog: %s\n", buf);
79 #endif
81 void
82 format_errmsg(char *nfmt, int lennfmt, const char *fmt, int errval)
84 register char c;
85 register char *n;
86 register const char *f;
87 size_t len;
88 char *err;
90 n = nfmt;
91 f = fmt;
92 while ((c = *f++) != '\0' && n < (nfmt + lennfmt - 2)) {
93 if (c != '%') {
94 *n++ = c;
95 continue;
97 if ((c = *f++) != 'm') {
98 *n++ = '%';
99 *n++ = c;
100 continue;
102 err = strerror(errval);
103 len = strlen(err);
105 /* Make sure we have enough space for the error message */
106 if ((n + len) < (nfmt + lennfmt - 2)) {
107 memcpy(n, err, len);
108 n += len;
111 #if !defined(VMS)
112 if (!syslogit)
113 #endif /* VMS */
114 *n++ = '\n';
115 *n = '\0';
118 #if defined(__STDC__) || defined(HAVE_STDARG_H)
119 void msyslog(int level, const char *fmt, ...)
120 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
121 /*VARARGS*/
122 void msyslog(va_alist)
123 va_dcl
124 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
126 #if defined(__STDC__) || defined(HAVE_STDARG_H)
127 #else
128 int level;
129 const char *fmt;
130 #endif
131 va_list ap;
132 char buf[1025], nfmt[256];
133 int errval;
136 * Save the error value as soon as possible
138 errval = errno;
140 #ifdef SYS_WINNT
141 errval = GetLastError();
142 if (NO_ERROR == errval)
143 errval = errno;
144 #endif /* SYS_WINNT */
146 #if defined(__STDC__) || defined(HAVE_STDARG_H)
147 va_start(ap, fmt);
148 #else
149 va_start(ap);
151 level = va_arg(ap, int);
152 fmt = va_arg(ap, char *);
153 #endif
154 format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
156 vsnprintf(buf, sizeof(buf), nfmt, ap);
157 addto_syslog(level, buf);
158 va_end(ap);