Annotation of embedaddon/ntp/ports/winnt/libntp/syslog.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (C) 2001 Internet Software Consortium.
3: *
4: * Permission to use, copy, modify, and distribute this software for any
5: * purpose with or without fee is hereby granted, provided that the above
6: * copyright notice and this permission notice appear in all copies.
7: *
8: * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
9: * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11: * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
12: * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13: * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
14: * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15: * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17:
18: /* From BIND 9 lib/isc/win32/: syslog.c,v 1.6 2002/08/03 01:34:14 mayer */
19:
20: #include <config.h>
21:
22: #include <stdio.h>
23: #include <windows.h>
24: #include <string.h>
25: #include <stdlib.h>
26: #include <syslog.h>
27:
28: #include <isc/strerror.h>
29:
30: #include "messages.h"
31:
32: static HANDLE hAppLog = NULL;
33: static FILE *log_stream;
34: static int debug_level = 0;
35: static char progname[51] = "NTP";
36: static int logmask = 0;
37:
38: static struct dsn_c_pvt_sfnt {
39: int val;
40: const char *strval;
41: } facilities[] = {
42: { LOG_KERN, "kern" },
43: { LOG_USER, "user" },
44: { LOG_MAIL, "mail" },
45: { LOG_DAEMON, "daemon" },
46: { LOG_AUTH, "auth" },
47: { LOG_SYSLOG, "syslog" },
48: { LOG_LPR, "lpr" },
49: #ifdef LOG_NEWS
50: { LOG_NEWS, "news" },
51: #endif
52: #ifdef LOG_UUCP
53: { LOG_UUCP, "uucp" },
54: #endif
55: #ifdef LOG_CRON
56: { LOG_CRON, "cron" },
57: #endif
58: #ifdef LOG_AUTHPRIV
59: { LOG_AUTHPRIV, "authpriv" },
60: #endif
61: #ifdef LOG_FTP
62: { LOG_FTP, "ftp" },
63: #endif
64: { LOG_LOCAL0, "local0"},
65: { LOG_LOCAL1, "local1"},
66: { LOG_LOCAL2, "local2"},
67: { LOG_LOCAL3, "local3"},
68: { LOG_LOCAL4, "local4"},
69: { LOG_LOCAL5, "local5"},
70: { LOG_LOCAL6, "local6"},
71: { LOG_LOCAL7, "local7"},
72: { 0, NULL }
73: };
74:
75: #if 0
76: BOOL
77: isc_syslog_facilityfromstring(const char *str, int *facilityp) {
78: int i;
79:
80: REQUIRE(str != NULL);
81: REQUIRE(facilityp != NULL);
82:
83: for (i = 0 ; facilities[i].strval != NULL ; i++) {
84: if (strcasecmp(facilities[i].strval, str) == 0) {
85: *facilityp = facilities[i].val;
86: return (TRUE);
87: }
88: }
89: return (FALSE);
90: }
91: #endif
92: /*
93: * Log to the NT Event Log
94: */
95: void
96: syslog(int level, const char *fmt, ...) {
97: va_list ap;
98: char buf[1024];
99: char *str[1];
100:
101: str[0] = buf;
102:
103: va_start(ap, fmt);
104: vsprintf(buf, fmt, ap);
105: va_end(ap);
106:
107: /* Make sure that the channel is open to write the event */
108: if (hAppLog == NULL) {
109: openlog(progname, LOG_PID);
110: }
111: switch (level) {
112: case LOG_INFO:
113: case LOG_NOTICE:
114: case LOG_DEBUG:
115: ReportEvent(hAppLog, EVENTLOG_INFORMATION_TYPE, 0,
116: NTP_INFO, NULL, 1, 0, str, NULL);
117: break;
118: case LOG_WARNING:
119: ReportEvent(hAppLog, EVENTLOG_WARNING_TYPE, 0,
120: NTP_WARNING, NULL, 1, 0, str, NULL);
121: break;
122: default:
123: ReportEvent(hAppLog, EVENTLOG_ERROR_TYPE, 0,
124: NTP_ERROR, NULL, 1, 0, str, NULL);
125: break;
126: }
127: }
128:
129: /*
130: * Initialize event logging
131: */
132: void
133: openlog(const char *name, int flags, ...) {
134: /* Get a handle to the Application event log */
135: hAppLog = RegisterEventSource(NULL, progname);
136: strncpy(progname, name, sizeof(progname));
137: progname[sizeof(progname) - 1] = 0;
138: }
139:
140: /*
141: * Close the Handle to the application Event Log
142: * We don't care whether or not we succeeded so ignore return values
143: * In fact if we failed then we would have nowhere to put the message
144: */
145: void
146: closelog() {
147: DeregisterEventSource(hAppLog);
148: }
149:
150: /*
151: * Keep event logging synced with the current debug level
152: */
153: void
154: ModifyLogLevel(int level) {
155: debug_level = level;
156: }
157: /*
158: * Set the log priority mask to the given value.
159: * Return the previous priority mask
160: * Note that this setting is ignored in Win32
161: */
162: int
163: setlogmask(int maskpri) {
164: int temp = logmask;
165: logmask = maskpri;
166: return (temp);
167: }
168:
169: /*
170: * Initialize logging for the port section of libbind.
171: * Piggyback onto stream given.
172: */
173: void
174: InitNTLogging(FILE *stream, int debug) {
175: log_stream = stream;
176: ModifyLogLevel(debug);
177: }
178: /*
179: * This function is for reporting errors to the application
180: * event log in case the regular syslog is not available
181: * mainly during startup. It should not be used under normal
182: * circumstances.
183: */
184: void
185: NTReportError(const char *name, const char *str) {
186: HANDLE hNTAppLog = NULL;
187: const char *buf[1];
188:
189: buf[0] = str;
190:
191: hNTAppLog = RegisterEventSource(NULL, name);
192:
193: ReportEvent(hNTAppLog, EVENTLOG_ERROR_TYPE, 0,
194: NTP_ERROR, NULL, 1, 0, buf, NULL);
195:
196: DeregisterEventSource(hNTAppLog);
197: }
198:
199:
200: /*
201: * ntp_strerror() - provide strerror()-compatible wrapper for libisc's
202: * isc__strerror(), which knows about Windows as well as
203: * C runtime error messages.
204: */
205:
206: char *
207: ntp_strerror(
208: int code
209: )
210: {
211: static char msgbuf[128];
212:
213: isc__strerror(code, msgbuf, sizeof(msgbuf));
214:
215: return msgbuf;
216: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>