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 "..\ports\winnt\libntp\log.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 HANDLE hEventSource; 39 LPTSTR lpszStrings[1]; 40 static WORD event_type[] = { 41 EVENTLOG_ERROR_TYPE, EVENTLOG_ERROR_TYPE, EVENTLOG_ERROR_TYPE, EVENTLOG_ERROR_TYPE, 42 EVENTLOG_WARNING_TYPE, 43 EVENTLOG_INFORMATION_TYPE, EVENTLOG_INFORMATION_TYPE, EVENTLOG_INFORMATION_TYPE, 44 }; 45 #endif /* SYS_WINNT */ 46 extern char *progname; 47 48 #if defined(__STDC__) || defined(HAVE_STDARG_H) 49 void msyslog(int level, const char *fmt, ...) 50 #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */ 51 /*VARARGS*/ 52 void msyslog(va_alist) 53 va_dcl 54 #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */ 55 { 56 #if defined(__STDC__) || defined(HAVE_STDARG_H) 57 #else 58 int level; 59 const char *fmt; 60 #endif 61 va_list ap; 62 char buf[1025], nfmt[256]; 63 #if defined(SYS_WINNT) 64 char xerr[50]; 65 #endif 66 register int c; 67 register char *n, *prog; 68 register const char *f; 69 int olderrno; 70 char *err; 71 72 #if defined(__STDC__) || defined(HAVE_STDARG_H) 73 va_start(ap, fmt); 74 #else 75 va_start(ap); 76 77 level = va_arg(ap, int); 78 fmt = va_arg(ap, char *); 79 #endif 80 81 olderrno = errno; 82 n = nfmt; 83 f = fmt; 84 while ((c = *f++) != '\0' && c != '\n' && n < &nfmt[252]) { 85 if (c != '%') { 86 *n++ = c; 87 continue; 88 } 89 if ((c = *f++) != 'm') { 90 *n++ = '%'; 91 *n++ = c; 92 continue; 93 } 94 err = 0; 95 #if !defined(SYS_WINNT) 96 err = strerror(olderrno); 97 #else /* SYS_WINNT */ 98 err = xerr; 99 FormatMessage( 100 FORMAT_MESSAGE_FROM_SYSTEM, 101 NULL, 102 GetLastError(), 103 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */ 104 (LPTSTR) err, 105 sizeof(xerr), 106 NULL); 107 108 #endif /* SYS_WINNT */ 109 if ((n + strlen(err)) < &nfmt[254]) { 110 strcpy(n, err); 111 n += strlen(err); 112 } 113 } 114 #if !defined(VMS) 115 if (!syslogit) 116 #endif /* VMS */ 117 *n++ = '\n'; 118 *n = '\0'; 119 120 vsprintf(buf, nfmt, ap); 121 #if !defined(VMS) && !defined (SYS_VXWORKS) 122 if (syslogit) 123 #ifndef SYS_WINNT 124 syslog(level, "%s", buf); 125 #else 126 { 127 lpszStrings[0] = buf; 128 129 switch (event_type[level]) 130 { 131 case EVENTLOG_ERROR_TYPE: 132 reportAnEEvent(NTP_ERROR,1,lpszStrings); 133 break; 134 case EVENTLOG_INFORMATION_TYPE: 135 reportAnIEvent(NTP_INFO,1,lpszStrings); 136 break; 137 case EVENTLOG_WARNING_TYPE: 138 reportAnWEvent(NTP_WARNING,1,lpszStrings); 139 break; 140 } /* switch end */ 141 142 } 143 #endif /* SYS_WINNT */ 144 else 145 #endif /* VMS && SYS_VXWORKS*/ 146 { 147 FILE *out_file = syslog_file ? syslog_file 148 : level <= LOG_ERR ? stderr : stdout; 149 /* syslog() provides the timestamp, so if we're not using 150 syslog, we must provide it. */ 151 prog = strrchr(progname, '/'); 152 if (prog == NULL) 153 prog = progname; 154 else 155 prog++; 156 (void) fprintf(out_file, "%s ", humanlogtime ()); 157 (void) fprintf(out_file, "%s[%d]: %s", prog, (int)getpid(), buf); 158 fflush (out_file); 159 } 160 va_end(ap); 161 } 162