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\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 P((int, char *)); 46 void format_errmsg P((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 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 = 0; 103 err = strerror(errval); 104 /* Make sure we have enough space for the error message */ 105 if ((n + strlen(err)) < (nfmt + lennfmt -2)) { 106 strcpy(n, err); 107 n += strlen(err); 108 } 109 } 110 #if !defined(VMS) 111 if (!syslogit) 112 #endif /* VMS */ 113 *n++ = '\n'; 114 *n = '\0'; 115 } 116 117 /* 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 126 * in implementation. 127 */ 128 129 #if defined(__STDC__) || defined(HAVE_STDARG_H) 130 void msyslog(int level, const char *fmt, ...) 131 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */ 132 /*VARARGS*/ 133 void msyslog(va_alist) 134 va_dcl 135 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */ 136 { 137 #if defined(__STDC__) || defined(HAVE_STDARG_H) 138 #else 139 int level; 140 const char *fmt; 141 #endif 142 va_list ap; 143 char buf[1025], nfmt[256]; 144 145 /* 146 * Save the error value as soon as possible 147 */ 148 #ifdef SYS_WINNT 149 int errval = GetLastError(); 150 #else 151 int errval = errno; 152 #endif 153 154 #if defined(__STDC__) || defined(HAVE_STDARG_H) 155 va_start(ap, fmt); 156 #else 157 va_start(ap); 158 159 level = va_arg(ap, int); 160 fmt = va_arg(ap, char *); 161 #endif 162 format_errmsg(nfmt, sizeof(nfmt), fmt, errval); 163 164 vsnprintf(buf, sizeof(buf), nfmt, ap); 165 addto_syslog(level, buf); 166 va_end(ap); 167 } 168 #if defined(__STDC__) || defined(HAVE_STDARG_H) 169 void netsyslog(int level, const char *fmt, ...) 170 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */ 171 /*VARARGS*/ 172 void netsyslog(va_alist) 173 va_dcl 174 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */ 175 { 176 #if defined(__STDC__) || defined(HAVE_STDARG_H) 177 #else 178 int level; 179 const char *fmt; 180 #endif 181 va_list ap; 182 char buf[1025], nfmt[256]; 183 184 /* 185 * Save the error value as soon as possible 186 */ 187 #ifdef SYS_WINNT 188 int errval = WSAGetLastError(); 189 #else 190 int errval = errno; 191 #endif 192 193 #if defined(__STDC__) || defined(HAVE_STDARG_H) 194 va_start(ap, fmt); 195 #else 196 va_start(ap); 197 198 level = va_arg(ap, int); 199 fmt = va_arg(ap, char *); 200 #endif 201 format_errmsg(nfmt, sizeof(nfmt), fmt, errval); 202 203 vsnprintf(buf, sizeof(buf), nfmt, ap); 204 addto_syslog(level, buf); 205 va_end(ap); 206 } 207