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\messages.h"
33 FILE *syslog_file = NULL;
35 u_long ntp_syslogmask = ~ (u_long) 0;
38 static char separator = '\\';
40 static char separator = '/';
41 #endif /* SYS_WINNT */
42 extern char *progname;
44 /* Declare the local functions */
45 void addto_syslog P((int, char *));
46 void format_errmsg P((char *, int, const char *, int));
50 * This routine adds the contents of a buffer to the log
53 addto_syslog(int level, char * buf)
56 FILE *out_file = syslog_file;
58 #if !defined(VMS) && !defined (SYS_VXWORKS)
60 syslog(level, "%s", buf);
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);
72 (void) fprintf(out_file, "%s ", humanlogtime ());
73 (void) fprintf(out_file, "%s[%d]: %s", prog, (int)getpid(), buf);
77 if (debug && out_file != stdout && out_file != stderr)
78 printf("addto_syslog: %s\n", buf);
82 format_errmsg(char *nfmt, int lennfmt, const char *fmt, int errval)
86 register const char *f;
92 while ((c = *f++) != '\0' && n < (nfmt+lennfmt - 2)) {
97 if ((c = *f++) != 'm') {
103 err = strerror(errval);
104 /* Make sure we have enough space for the error message */
105 if ((n + strlen(err)) < (nfmt + lennfmt -2)) {
118 * The externally called functions are defined here
119 * but share the internal function above to fetch
120 * any error message strings, This is done so that we can
121 * have two different functions to perform the logging
122 * since Windows gets it's error information from different
123 * places depending on whether or not it's network I/O.
124 * msyslog() is for general use while netsyslog() is for
125 * network I/O functions. They are virtually identical
129 #if defined(__STDC__) || defined(HAVE_STDARG_H)
130 void msyslog(int level, const char *fmt, ...)
131 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
133 void msyslog(va_alist)
135 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
137 #if defined(__STDC__) || defined(HAVE_STDARG_H)
143 char buf[1025], nfmt[256];
146 * Save the error value as soon as possible
149 int errval = GetLastError();
154 #if defined(__STDC__) || defined(HAVE_STDARG_H)
159 level = va_arg(ap, int);
160 fmt = va_arg(ap, char *);
162 format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
164 vsnprintf(buf, sizeof(buf), nfmt, ap);
165 addto_syslog(level, buf);
168 #if defined(__STDC__) || defined(HAVE_STDARG_H)
169 void netsyslog(int level, const char *fmt, ...)
170 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
172 void netsyslog(va_alist)
174 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
176 #if defined(__STDC__) || defined(HAVE_STDARG_H)
182 char buf[1025], nfmt[256];
185 * Save the error value as soon as possible
188 int errval = WSAGetLastError();
193 #if defined(__STDC__) || defined(HAVE_STDARG_H)
198 level = va_arg(ap, int);
199 fmt = va_arg(ap, char *);
201 format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
203 vsnprintf(buf, sizeof(buf), nfmt, ap);
204 addto_syslog(level, buf);