diff options
author | roberto <roberto@FreeBSD.org> | 2004-07-20 15:01:56 +0000 |
---|---|---|
committer | roberto <roberto@FreeBSD.org> | 2004-07-20 15:01:56 +0000 |
commit | 118e757284cbb8fc4f43a713e892b41504b50a5f (patch) | |
tree | 528d12dda44ebdc3ffcc38050f159ac553a69c17 /contrib/ntp/libntp/msyslog.c | |
parent | a85d9ae25e8e8696677bc30feb6eaf7fc150e529 (diff) | |
download | FreeBSD-src-118e757284cbb8fc4f43a713e892b41504b50a5f.zip FreeBSD-src-118e757284cbb8fc4f43a713e892b41504b50a5f.tar.gz |
Virgin import of ntpd 4.2.0
Diffstat (limited to 'contrib/ntp/libntp/msyslog.c')
-rw-r--r-- | contrib/ntp/libntp/msyslog.c | 226 |
1 files changed, 136 insertions, 90 deletions
diff --git a/contrib/ntp/libntp/msyslog.c b/contrib/ntp/libntp/msyslog.c index 52da608..f0e8872 100644 --- a/contrib/ntp/libntp/msyslog.c +++ b/contrib/ntp/libntp/msyslog.c @@ -24,6 +24,7 @@ #include "ntp_stdlib.h" #ifdef SYS_WINNT +# include <stdarg.h> # include "..\ports\winnt\libntp\log.h" # include "..\ports\winnt\libntp\messages.h" #endif @@ -35,53 +36,61 @@ FILE *syslog_file = NULL; u_long ntp_syslogmask = ~ (u_long) 0; #ifdef SYS_WINNT -HANDLE hEventSource; -LPTSTR lpszStrings[1]; -static WORD event_type[] = { - EVENTLOG_ERROR_TYPE, EVENTLOG_ERROR_TYPE, EVENTLOG_ERROR_TYPE, EVENTLOG_ERROR_TYPE, - EVENTLOG_WARNING_TYPE, - EVENTLOG_INFORMATION_TYPE, EVENTLOG_INFORMATION_TYPE, EVENTLOG_INFORMATION_TYPE, -}; +static char separator = '\\'; +#else +static char separator = '/'; #endif /* SYS_WINNT */ extern char *progname; -#if defined(__STDC__) || defined(HAVE_STDARG_H) -void msyslog(int level, const char *fmt, ...) -#else /* defined(__STDC__) || defined(HAVE_STDARG_H) */ - /*VARARGS*/ - void msyslog(va_alist) - va_dcl -#endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */ +/* Declare the local functions */ +void addto_syslog P((int, char *)); +void format_errmsg P((char *, int, const char *, int)); + + +/* + * This routine adds the contents of a buffer to the log + */ +void +addto_syslog(int level, char * buf) { -#if defined(__STDC__) || defined(HAVE_STDARG_H) -#else - int level; - const char *fmt; -#endif - va_list ap; - char buf[1025], nfmt[256]; -#if defined(SYS_WINNT) - char xerr[50]; + char *prog; + FILE *out_file = syslog_file; + +#if !defined(VMS) && !defined (SYS_VXWORKS) + if (syslogit) + syslog(level, "%s", buf); + else +#endif /* VMS && SYS_VXWORKS*/ + { + out_file = syslog_file ? syslog_file: level <= LOG_ERR ? stderr : stdout; + /* syslog() provides the timestamp, so if we're not using + syslog, we must provide it. */ + prog = strrchr(progname, separator); + if (prog == NULL) + prog = progname; + else + prog++; + (void) fprintf(out_file, "%s ", humanlogtime ()); + (void) fprintf(out_file, "%s[%d]: %s", prog, (int)getpid(), buf); + fflush (out_file); + } +#if DEBUG + if (debug && out_file != stdout && out_file != stderr) + printf("addto_syslog: %s\n", buf); #endif - register int c; - register char *n, *prog; +} +void +format_errmsg(char *nfmt, int lennfmt, const char *fmt, int errval) +{ + register char c; + register char *n; register const char *f; - int olderrno; - char *err; - -#if defined(__STDC__) || defined(HAVE_STDARG_H) - va_start(ap, fmt); -#else - va_start(ap); - level = va_arg(ap, int); - fmt = va_arg(ap, char *); -#endif + char *err; - olderrno = errno; n = nfmt; f = fmt; - while ((c = *f++) != '\0' && c != '\n' && n < &nfmt[252]) { + while ((c = *f++) != '\0' && n < (nfmt+lennfmt - 2)) { if (c != '%') { *n++ = c; continue; @@ -92,21 +101,9 @@ void msyslog(int level, const char *fmt, ...) continue; } err = 0; -#if !defined(SYS_WINNT) - err = strerror(olderrno); -#else /* SYS_WINNT */ - err = xerr; - FormatMessage( - FORMAT_MESSAGE_FROM_SYSTEM, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */ - (LPTSTR) err, - sizeof(xerr), - NULL); - -#endif /* SYS_WINNT */ - if ((n + strlen(err)) < &nfmt[254]) { + err = strerror(errval); + /* Make sure we have enough space for the error message */ + if ((n + strlen(err)) < (nfmt + lennfmt -2)) { strcpy(n, err); n += strlen(err); } @@ -116,46 +113,95 @@ void msyslog(int level, const char *fmt, ...) #endif /* VMS */ *n++ = '\n'; *n = '\0'; +} + +/* + * The externally called functions are defined here + * but share the internal function above to fetch + * any error message strings, This is done so that we can + * have two different functions to perform the logging + * since Windows gets it's error information from different + * places depending on whether or not it's network I/O. + * msyslog() is for general use while netsyslog() is for + * network I/O functions. They are virtually identical + * in implementation. + */ + +#if defined(__STDC__) || defined(HAVE_STDARG_H) +void msyslog(int level, const char *fmt, ...) +#else /* defined(__STDC__) || defined(HAVE_STDARG_H) */ + /*VARARGS*/ + void msyslog(va_alist) + va_dcl +#endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */ +{ +#if defined(__STDC__) || defined(HAVE_STDARG_H) +#else + int level; + const char *fmt; +#endif + va_list ap; + char buf[1025], nfmt[256]; + + /* + * Save the error value as soon as possible + */ +#ifdef SYS_WINNT + int errval = GetLastError(); +#else + int errval = errno; +#endif + +#if defined(__STDC__) || defined(HAVE_STDARG_H) + va_start(ap, fmt); +#else + va_start(ap); + + level = va_arg(ap, int); + fmt = va_arg(ap, char *); +#endif + format_errmsg(nfmt, sizeof(nfmt), fmt, errval); vsnprintf(buf, sizeof(buf), nfmt, ap); -#if !defined(VMS) && !defined (SYS_VXWORKS) - if (syslogit) -#ifndef SYS_WINNT - syslog(level, "%s", buf); + addto_syslog(level, buf); + va_end(ap); +} +#if defined(__STDC__) || defined(HAVE_STDARG_H) +void netsyslog(int level, const char *fmt, ...) +#else /* defined(__STDC__) || defined(HAVE_STDARG_H) */ + /*VARARGS*/ + void netsyslog(va_alist) + va_dcl +#endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */ +{ +#if defined(__STDC__) || defined(HAVE_STDARG_H) #else - { - lpszStrings[0] = buf; - - switch (event_type[level]) - { - case EVENTLOG_ERROR_TYPE: - reportAnEEvent(NTP_ERROR,1,lpszStrings); - break; - case EVENTLOG_INFORMATION_TYPE: - reportAnIEvent(NTP_INFO,1,lpszStrings); - break; - case EVENTLOG_WARNING_TYPE: - reportAnWEvent(NTP_WARNING,1,lpszStrings); - break; - } /* switch end */ - - } -#endif /* SYS_WINNT */ - else -#endif /* VMS && SYS_VXWORKS*/ - { - FILE *out_file = syslog_file ? syslog_file - : level <= LOG_ERR ? stderr : stdout; - /* syslog() provides the timestamp, so if we're not using - syslog, we must provide it. */ - prog = strrchr(progname, '/'); - if (prog == NULL) - prog = progname; - else - prog++; - (void) fprintf(out_file, "%s ", humanlogtime ()); - (void) fprintf(out_file, "%s[%d]: %s", prog, (int)getpid(), buf); - fflush (out_file); - } + int level; + const char *fmt; +#endif + va_list ap; + char buf[1025], nfmt[256]; + + /* + * Save the error value as soon as possible + */ +#ifdef SYS_WINNT + int errval = WSAGetLastError(); +#else + int errval = errno; +#endif + +#if defined(__STDC__) || defined(HAVE_STDARG_H) + va_start(ap, fmt); +#else + va_start(ap); + + level = va_arg(ap, int); + fmt = va_arg(ap, char *); +#endif + format_errmsg(nfmt, sizeof(nfmt), fmt, errval); + + vsnprintf(buf, sizeof(buf), nfmt, ap); + addto_syslog(level, buf); va_end(ap); } |