xref: /netbsd-src/external/bsd/ntp/dist/libntp/msyslog.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: msyslog.c,v 1.1.1.1 2009/12/13 16:55:04 kardel Exp $	*/
2 
3 /*
4  * msyslog - either send a message to the terminal or print it on
5  *	     the standard output.
6  *
7  * Converted to use varargs, much better ... jks
8  */
9 
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 
14 #include <sys/types.h>
15 #ifdef HAVE_UNISTD_H
16 # include <unistd.h>
17 #endif
18 
19 #include <stdio.h>
20 
21 #include "ntp_types.h"
22 #include "ntp_string.h"
23 #include "ntp_syslog.h"
24 #include "ntp_stdlib.h"
25 
26 #ifdef SYS_WINNT
27 # include <stdarg.h>
28 # include "..\ports\winnt\libntp\messages.h"
29 #endif
30 
31 int syslogit = 1;
32 
33 FILE *syslog_file = NULL;
34 
35 u_long ntp_syslogmask =  ~ (u_long) 0;
36 
37 #ifdef SYS_WINNT
38 static char separator = '\\';
39 #else
40 static char separator = '/';
41 #endif /* SYS_WINNT */
42 extern	char *progname;
43 
44 /* Declare the local functions */
45 void	addto_syslog	(int, char *);
46 void	format_errmsg   (char *, int, const char *, int);
47 
48 
49 /*
50  * This routine adds the contents of a buffer to the log
51  */
52 void
53 addto_syslog(int level, char * buf)
54 {
55 	char *prog;
56 	FILE *out_file = syslog_file;
57 
58 #if !defined(VMS) && !defined (SYS_VXWORKS)
59 	if (syslogit)
60 	    syslog(level, "%s", buf);
61 	else
62 #endif /* VMS  && SYS_VXWORKS*/
63 	{
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);
75 	}
76 #if DEBUG
77 	if (debug && out_file != stdout && out_file != stderr)
78 		printf("addto_syslog: %s\n", buf);
79 #endif
80 }
81 void
82 format_errmsg(char *nfmt, int lennfmt, const char *fmt, int errval)
83 {
84 	register char c;
85 	register char *n;
86 	register const char *f;
87 	size_t len;
88 	char *err;
89 
90 	n = nfmt;
91 	f = fmt;
92 	while ((c = *f++) != '\0' && n < (nfmt + lennfmt - 2)) {
93 		if (c != '%') {
94 			*n++ = c;
95 			continue;
96 		}
97 		if ((c = *f++) != 'm') {
98 			*n++ = '%';
99 			*n++ = c;
100 			continue;
101 		}
102 		err = strerror(errval);
103 		len = strlen(err);
104 
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;
109 		}
110 	}
111 #if !defined(VMS)
112 	if (!syslogit)
113 #endif /* VMS */
114 	    *n++ = '\n';
115 	*n = '\0';
116 }
117 
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) */
125 {
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;
134 
135 	/*
136 	 * Save the error value as soon as possible
137 	 */
138 	errval = errno;
139 
140 #ifdef SYS_WINNT
141 	errval = GetLastError();
142 	if (NO_ERROR == errval)
143 		errval = errno;
144 #endif /* SYS_WINNT */
145 
146 #if defined(__STDC__) || defined(HAVE_STDARG_H)
147 	va_start(ap, fmt);
148 #else
149 	va_start(ap);
150 
151 	level = va_arg(ap, int);
152 	fmt = va_arg(ap, char *);
153 #endif
154 	format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
155 
156 	vsnprintf(buf, sizeof(buf), nfmt, ap);
157 	addto_syslog(level, buf);
158 	va_end(ap);
159 }
160