msyslog.c revision 106424
154359Sroberto/* 254359Sroberto * msyslog - either send a message to the terminal or print it on 354359Sroberto * the standard output. 454359Sroberto * 554359Sroberto * Converted to use varargs, much better ... jks 654359Sroberto */ 754359Sroberto 854359Sroberto#ifdef HAVE_CONFIG_H 954359Sroberto# include <config.h> 1054359Sroberto#endif 1154359Sroberto 1254359Sroberto#ifdef HAVE_SYS_TYPES_H 1354359Sroberto# include <sys/types.h> 1454359Sroberto#endif 1554359Sroberto#ifdef HAVE_UNISTD_H 1654359Sroberto# include <unistd.h> 1754359Sroberto#endif 1854359Sroberto 1954359Sroberto#include <stdio.h> 2054359Sroberto 2154359Sroberto#include "ntp_types.h" 2254359Sroberto#include "ntp_string.h" 2382498Sroberto#include "ntp_syslog.h" 2454359Sroberto#include "ntp_stdlib.h" 2554359Sroberto 2654359Sroberto#ifdef SYS_WINNT 2754359Sroberto# include "..\ports\winnt\libntp\log.h" 2854359Sroberto# include "..\ports\winnt\libntp\messages.h" 2954359Sroberto#endif 3054359Sroberto 3154359Srobertoint syslogit = 1; 3254359Sroberto 3354359SrobertoFILE *syslog_file = NULL; 3454359Sroberto 3554359Srobertou_long ntp_syslogmask = ~ (u_long) 0; 3654359Sroberto 3754359Sroberto#ifdef SYS_WINNT 3854359SrobertoHANDLE hEventSource; 3954359SrobertoLPTSTR lpszStrings[1]; 4054359Srobertostatic WORD event_type[] = { 4154359Sroberto EVENTLOG_ERROR_TYPE, EVENTLOG_ERROR_TYPE, EVENTLOG_ERROR_TYPE, EVENTLOG_ERROR_TYPE, 4254359Sroberto EVENTLOG_WARNING_TYPE, 4354359Sroberto EVENTLOG_INFORMATION_TYPE, EVENTLOG_INFORMATION_TYPE, EVENTLOG_INFORMATION_TYPE, 4454359Sroberto}; 4554359Sroberto#endif /* SYS_WINNT */ 4654359Srobertoextern char *progname; 4754359Sroberto 4854359Sroberto#if defined(__STDC__) || defined(HAVE_STDARG_H) 4954359Srobertovoid msyslog(int level, const char *fmt, ...) 5054359Sroberto#else /* defined(__STDC__) || defined(HAVE_STDARG_H) */ 5154359Sroberto /*VARARGS*/ 5254359Sroberto void msyslog(va_alist) 5354359Sroberto va_dcl 5454359Sroberto#endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */ 5554359Sroberto{ 5654359Sroberto#if defined(__STDC__) || defined(HAVE_STDARG_H) 5754359Sroberto#else 5854359Sroberto int level; 5954359Sroberto const char *fmt; 6054359Sroberto#endif 6154359Sroberto va_list ap; 6254359Sroberto char buf[1025], nfmt[256]; 6382498Sroberto#if defined(SYS_WINNT) 6454359Sroberto char xerr[50]; 6554359Sroberto#endif 6654359Sroberto register int c; 6754359Sroberto register char *n, *prog; 6854359Sroberto register const char *f; 6954359Sroberto int olderrno; 7054359Sroberto char *err; 7154359Sroberto 7254359Sroberto#if defined(__STDC__) || defined(HAVE_STDARG_H) 7354359Sroberto va_start(ap, fmt); 7454359Sroberto#else 7554359Sroberto va_start(ap); 7654359Sroberto 7754359Sroberto level = va_arg(ap, int); 7854359Sroberto fmt = va_arg(ap, char *); 7954359Sroberto#endif 8054359Sroberto 8154359Sroberto olderrno = errno; 8254359Sroberto n = nfmt; 8354359Sroberto f = fmt; 8454359Sroberto while ((c = *f++) != '\0' && c != '\n' && n < &nfmt[252]) { 8554359Sroberto if (c != '%') { 8654359Sroberto *n++ = c; 8754359Sroberto continue; 8854359Sroberto } 8954359Sroberto if ((c = *f++) != 'm') { 9054359Sroberto *n++ = '%'; 9154359Sroberto *n++ = c; 9254359Sroberto continue; 9354359Sroberto } 9454359Sroberto err = 0; 9582498Sroberto#if !defined(SYS_WINNT) 9654359Sroberto err = strerror(olderrno); 9754359Sroberto#else /* SYS_WINNT */ 9854359Sroberto err = xerr; 9954359Sroberto FormatMessage( 10054359Sroberto FORMAT_MESSAGE_FROM_SYSTEM, 10154359Sroberto NULL, 10254359Sroberto GetLastError(), 10354359Sroberto MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */ 10454359Sroberto (LPTSTR) err, 10554359Sroberto sizeof(xerr), 10654359Sroberto NULL); 10754359Sroberto 10882498Sroberto#endif /* SYS_WINNT */ 10954359Sroberto if ((n + strlen(err)) < &nfmt[254]) { 11054359Sroberto strcpy(n, err); 11154359Sroberto n += strlen(err); 11254359Sroberto } 11354359Sroberto } 11454359Sroberto#if !defined(VMS) 11554359Sroberto if (!syslogit) 11654359Sroberto#endif /* VMS */ 11754359Sroberto *n++ = '\n'; 11854359Sroberto *n = '\0'; 11954359Sroberto 120106424Sroberto vsnprintf(buf, sizeof(buf), nfmt, ap); 12154359Sroberto#if !defined(VMS) && !defined (SYS_VXWORKS) 12254359Sroberto if (syslogit) 12354359Sroberto#ifndef SYS_WINNT 12454359Sroberto syslog(level, "%s", buf); 12554359Sroberto#else 12654359Sroberto { 12754359Sroberto lpszStrings[0] = buf; 12854359Sroberto 12954359Sroberto switch (event_type[level]) 13054359Sroberto { 13154359Sroberto case EVENTLOG_ERROR_TYPE: 13254359Sroberto reportAnEEvent(NTP_ERROR,1,lpszStrings); 13354359Sroberto break; 13454359Sroberto case EVENTLOG_INFORMATION_TYPE: 13554359Sroberto reportAnIEvent(NTP_INFO,1,lpszStrings); 13654359Sroberto break; 13754359Sroberto case EVENTLOG_WARNING_TYPE: 13854359Sroberto reportAnWEvent(NTP_WARNING,1,lpszStrings); 13954359Sroberto break; 14054359Sroberto } /* switch end */ 14154359Sroberto 14254359Sroberto } 14354359Sroberto#endif /* SYS_WINNT */ 14454359Sroberto else 14554359Sroberto#endif /* VMS && SYS_VXWORKS*/ 14654359Sroberto { 14754359Sroberto FILE *out_file = syslog_file ? syslog_file 14854359Sroberto : level <= LOG_ERR ? stderr : stdout; 14954359Sroberto /* syslog() provides the timestamp, so if we're not using 15054359Sroberto syslog, we must provide it. */ 15154359Sroberto prog = strrchr(progname, '/'); 15254359Sroberto if (prog == NULL) 15354359Sroberto prog = progname; 15454359Sroberto else 15554359Sroberto prog++; 15654359Sroberto (void) fprintf(out_file, "%s ", humanlogtime ()); 15754359Sroberto (void) fprintf(out_file, "%s[%d]: %s", prog, (int)getpid(), buf); 15854359Sroberto fflush (out_file); 15954359Sroberto } 16054359Sroberto va_end(ap); 16154359Sroberto} 162