xref: /freebsd/contrib/ntp/libntp/msyslog.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * msyslog - either send a message to the terminal or print it on
3  *	     the standard output.
4  *
5  * Converted to use varargs, much better ... jks
6  */
7 
8 #ifdef HAVE_CONFIG_H
9 # include <config.h>
10 #endif
11 
12 #ifdef HAVE_SYS_TYPES_H
13 # include <sys/types.h>
14 #endif
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\log.h"
29 # include "..\ports\winnt\libntp\messages.h"
30 #endif
31 
32 int syslogit = 1;
33 
34 FILE *syslog_file = NULL;
35 
36 u_long ntp_syslogmask =  ~ (u_long) 0;
37 
38 #ifdef SYS_WINNT
39 static char separator = '\\';
40 #else
41 static char separator = '/';
42 #endif /* SYS_WINNT */
43 extern	char *progname;
44 
45 /* Declare the local functions */
46 void	addto_syslog	P((int, char *));
47 void	format_errmsg   P((char *, int, const char *, int));
48 
49 
50 /*
51  * This routine adds the contents of a buffer to the log
52  */
53 void
54 addto_syslog(int level, char * buf)
55 {
56 	char *prog;
57 	FILE *out_file = syslog_file;
58 
59 #if !defined(VMS) && !defined (SYS_VXWORKS)
60 	if (syslogit)
61 	    syslog(level, "%s", buf);
62 	else
63 #endif /* VMS  && SYS_VXWORKS*/
64 	{
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);
69 		if (prog == NULL)
70 		    prog = progname;
71 		else
72 		    prog++;
73 		(void) fprintf(out_file, "%s ", humanlogtime ());
74 		(void) fprintf(out_file, "%s[%d]: %s", prog, (int)getpid(), buf);
75 		fflush (out_file);
76 	}
77 #if DEBUG
78 	if (debug && out_file != stdout && out_file != stderr)
79 		printf("addto_syslog: %s\n", buf);
80 #endif
81 }
82 void
83 format_errmsg(char *nfmt, int lennfmt, const char *fmt, int errval)
84 {
85 	register char c;
86 	register char *n;
87 	register const char *f;
88 
89 	char *err;
90 
91 	n = nfmt;
92 	f = fmt;
93 	while ((c = *f++) != '\0' && n < (nfmt+lennfmt - 2)) {
94 		if (c != '%') {
95 			*n++ = c;
96 			continue;
97 		}
98 		if ((c = *f++) != 'm') {
99 			*n++ = '%';
100 			*n++ = c;
101 			continue;
102 		}
103 		err = 0;
104 		err = strerror(errval);
105 		/* Make sure we have enough space for the error message */
106 		if ((n + strlen(err)) < (nfmt + lennfmt -2)) {
107 			strcpy(n, err);
108 			n += strlen(err);
109 		}
110 	}
111 #if !defined(VMS)
112 	if (!syslogit)
113 #endif /* VMS */
114 	    *n++ = '\n';
115 	*n = '\0';
116 }
117 
118 /*
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
127  * in implementation.
128  */
129 
130 #if defined(__STDC__) || defined(HAVE_STDARG_H)
131 void msyslog(int level, const char *fmt, ...)
132 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
133      /*VARARGS*/
134      void msyslog(va_alist)
135      va_dcl
136 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
137 {
138 #if defined(__STDC__) || defined(HAVE_STDARG_H)
139 #else
140 	int level;
141 	const char *fmt;
142 #endif
143 	va_list ap;
144 	char buf[1025], nfmt[256];
145 
146 	/*
147 	 * Save the error value as soon as possible
148 	 */
149 #ifdef SYS_WINNT
150 	int errval = GetLastError();
151 #else
152 	int errval = errno;
153 #endif
154 
155 #if defined(__STDC__) || defined(HAVE_STDARG_H)
156 	va_start(ap, fmt);
157 #else
158 	va_start(ap);
159 
160 	level = va_arg(ap, int);
161 	fmt = va_arg(ap, char *);
162 #endif
163 	format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
164 
165 	vsnprintf(buf, sizeof(buf), nfmt, ap);
166 	addto_syslog(level, buf);
167 	va_end(ap);
168 }
169 #if defined(__STDC__) || defined(HAVE_STDARG_H)
170 void netsyslog(int level, const char *fmt, ...)
171 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
172      /*VARARGS*/
173      void netsyslog(va_alist)
174      va_dcl
175 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
176 {
177 #if defined(__STDC__) || defined(HAVE_STDARG_H)
178 #else
179 	int level;
180 	const char *fmt;
181 #endif
182 	va_list ap;
183 	char buf[1025], nfmt[256];
184 
185 	/*
186 	 * Save the error value as soon as possible
187 	 */
188 #ifdef SYS_WINNT
189 	int errval = WSAGetLastError();
190 #else
191 	int errval = errno;
192 #endif
193 
194 #if defined(__STDC__) || defined(HAVE_STDARG_H)
195 	va_start(ap, fmt);
196 #else
197 	va_start(ap);
198 
199 	level = va_arg(ap, int);
200 	fmt = va_arg(ap, char *);
201 #endif
202 	format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
203 
204 	vsnprintf(buf, sizeof(buf), nfmt, ap);
205 	addto_syslog(level, buf);
206 	va_end(ap);
207 }
208