1 /* $NetBSD: msyslog.c,v 1.3 2003/12/04 16:23:37 drochner Exp $ */
4 * msyslog - either send a message to the terminal or print it on
7 * Converted to use varargs, much better ... jks
14 #ifdef HAVE_SYS_TYPES_H
15 # include <sys/types.h>
23 #include "ntp_types.h"
24 #include "ntp_string.h"
25 #include "ntp_syslog.h"
26 #include "ntp_stdlib.h"
30 # include "..\ports\winnt\libntp\messages.h"
35 FILE *syslog_file
= NULL
;
37 u_long ntp_syslogmask
= ~ (u_long
) 0;
40 static char separator
= '\\';
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
55 addto_syslog(int level
, char * buf
)
58 FILE *out_file
= syslog_file
;
60 #if !defined(VMS) && !defined (SYS_VXWORKS)
62 syslog(level
, "%s", buf
);
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
);
74 (void) fprintf(out_file
, "%s ", humanlogtime ());
75 (void) fprintf(out_file
, "%s[%d]: %s", prog
, (int)getpid(), buf
);
79 if (debug
&& out_file
!= stdout
&& out_file
!= stderr
)
80 printf("addto_syslog: %s\n", buf
);
84 format_errmsg(char *nfmt
, int lennfmt
, const char *fmt
, int errval
)
88 register const char *f
;
94 while ((c
= *f
++) != '\0' && n
< (nfmt
+lennfmt
- 2)) {
99 if ((c
= *f
++) != 'm') {
105 err
= strerror(errval
);
106 /* Make sure we have enough space for the error message */
107 if ((n
+ strlen(err
)) < (nfmt
+ lennfmt
-2)) {
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
131 #if defined(__STDC__) || defined(HAVE_STDARG_H)
132 void msyslog(int level
, const char *fmt
, ...)
133 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
135 void msyslog(va_alist
)
137 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
139 #if defined(__STDC__) || defined(HAVE_STDARG_H)
145 char buf
[1025], nfmt
[256];
148 * Save the error value as soon as possible
151 int errval
= GetLastError();
156 #if defined(__STDC__) || defined(HAVE_STDARG_H)
161 level
= va_arg(ap
, int);
162 fmt
= va_arg(ap
, char *);
164 format_errmsg(nfmt
, sizeof(nfmt
), fmt
, errval
);
166 vsnprintf(buf
, sizeof(buf
), nfmt
, ap
);
167 addto_syslog(level
, buf
);
170 #if defined(__STDC__) || defined(HAVE_STDARG_H)
171 void netsyslog(int level
, const char *fmt
, ...)
172 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
174 void netsyslog(va_alist
)
176 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
178 #if defined(__STDC__) || defined(HAVE_STDARG_H)
184 char buf
[1025], nfmt
[256];
187 * Save the error value as soon as possible
190 int errval
= WSAGetLastError();
195 #if defined(__STDC__) || defined(HAVE_STDARG_H)
200 level
= va_arg(ap
, int);
201 fmt
= va_arg(ap
, char *);
203 format_errmsg(nfmt
, sizeof(nfmt
), fmt
, errval
);
205 vsnprintf(buf
, sizeof(buf
), nfmt
, ap
);
206 addto_syslog(level
, buf
);