4 * msyslog - either send a message to the terminal or print it on
7 * Converted to use varargs, much better ... jks
14 #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 (int, char *);
46 void format_errmsg (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') {
102 err
= strerror(errval
);
105 /* Make sure we have enough space for the error message */
106 if ((n
+ len
) < (nfmt
+ lennfmt
- 2)) {
118 #if defined(__STDC__) || defined(HAVE_STDARG_H)
119 void msyslog(int level
, const char *fmt
, ...)
120 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
122 void msyslog(va_alist
)
124 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
126 #if defined(__STDC__) || defined(HAVE_STDARG_H)
132 char buf
[1025], nfmt
[256];
136 * Save the error value as soon as possible
141 errval
= GetLastError();
142 if (NO_ERROR
== errval
)
144 #endif /* SYS_WINNT */
146 #if defined(__STDC__) || defined(HAVE_STDARG_H)
151 level
= va_arg(ap
, int);
152 fmt
= va_arg(ap
, char *);
154 format_errmsg(nfmt
, sizeof(nfmt
), fmt
, errval
);
156 vsnprintf(buf
, sizeof(buf
), nfmt
, ap
);
157 addto_syslog(level
, buf
);