2 * msyslog - either send a message to the terminal or print it on
5 * Converted to use varargs, much better ... jks
12 #ifdef HAVE_SYS_TYPES_H
13 # include <sys/types.h>
21 #include "ntp_types.h"
22 #include "ntp_string.h"
23 #include "ntp_syslog.h"
24 #include "ntp_stdlib.h"
28 # include "..\ports\winnt\libntp\log.h"
29 # include "..\ports\winnt\libntp\messages.h"
34 FILE *syslog_file = NULL;
36 u_long ntp_syslogmask = ~ (u_long) 0;
39 static char separator = '\\';
41 static char separator = '/';
42 #endif /* SYS_WINNT */
43 extern char *progname;
45 /* Declare the local functions */
46 void addto_syslog P((int, char *));
47 void format_errmsg P((char *, int, const char *, int));
51 * This routine adds the contents of a buffer to the log
54 addto_syslog(int level, char * buf)
57 FILE *out_file = syslog_file;
59 #if !defined(VMS) && !defined (SYS_VXWORKS)
61 syslog(level, "%s", buf);
63 #endif /* VMS && SYS_VXWORKS*/
65 out_file = syslog_file ? syslog_file: level <= LOG_ERR ? stderr : stdout;
66 /* syslog() provides the timestamp, so if we're not using
67 syslog, we must provide it. */
68 prog = strrchr(progname, separator);
73 (void) fprintf(out_file, "%s ", humanlogtime ());
74 (void) fprintf(out_file, "%s[%d]: %s", prog, (int)getpid(), buf);
78 if (debug && out_file != stdout && out_file != stderr)
79 printf("addto_syslog: %s\n", buf);
83 format_errmsg(char *nfmt, int lennfmt, const char *fmt, int errval)
87 register const char *f;
93 while ((c = *f++) != '\0' && n < (nfmt+lennfmt - 2)) {
98 if ((c = *f++) != 'm') {
104 err = strerror(errval);
105 /* Make sure we have enough space for the error message */
106 if ((n + strlen(err)) < (nfmt + lennfmt -2)) {
119 * The externally called functions are defined here
120 * but share the internal function above to fetch
121 * any error message strings, This is done so that we can
122 * have two different functions to perform the logging
123 * since Windows gets it's error information from different
124 * places depending on whether or not it's network I/O.
125 * msyslog() is for general use while netsyslog() is for
126 * network I/O functions. They are virtually identical
130 #if defined(__STDC__) || defined(HAVE_STDARG_H)
131 void msyslog(int level, const char *fmt, ...)
132 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
134 void msyslog(va_alist)
136 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
138 #if defined(__STDC__) || defined(HAVE_STDARG_H)
144 char buf[1025], nfmt[256];
147 * Save the error value as soon as possible
150 int errval = GetLastError();
155 #if defined(__STDC__) || defined(HAVE_STDARG_H)
160 level = va_arg(ap, int);
161 fmt = va_arg(ap, char *);
163 format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
165 vsnprintf(buf, sizeof(buf), nfmt, ap);
166 addto_syslog(level, buf);
169 #if defined(__STDC__) || defined(HAVE_STDARG_H)
170 void netsyslog(int level, const char *fmt, ...)
171 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
173 void netsyslog(va_alist)
175 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
177 #if defined(__STDC__) || defined(HAVE_STDARG_H)
183 char buf[1025], nfmt[256];
186 * Save the error value as soon as possible
189 int errval = WSAGetLastError();
194 #if defined(__STDC__) || defined(HAVE_STDARG_H)
199 level = va_arg(ap, int);
200 fmt = va_arg(ap, char *);
202 format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
204 vsnprintf(buf, sizeof(buf), nfmt, ap);
205 addto_syslog(level, buf);