xref: /minix/external/bsd/bind/dist/lib/isc/win32/syslog.c (revision fb9c64b2)
1 /*	$NetBSD: syslog.c,v 1.5 2014/12/10 04:38:01 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2007, 2014  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2001-2003  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: syslog.c,v 1.10 2007/06/19 23:47:19 tbox Exp  */
21 
22 #include <config.h>
23 
24 #include <stdio.h>
25 #include <windows.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <syslog.h>
29 
30 #include <isc/bindevt.h>
31 #include <isc/result.h>
32 #include <isc/syslog.h>
33 #include <isc/util.h>
34 
35 static HANDLE hAppLog = NULL;
36 static FILE *log_stream;
37 static int debug_level = 0;
38 
39 static struct dsn_c_pvt_sfnt {
40 	int val;
41 	const char *strval;
42 } facilities[] = {
43 	{ LOG_KERN,		"kern" },
44 	{ LOG_USER,		"user" },
45 	{ LOG_MAIL,		"mail" },
46 	{ LOG_DAEMON,		"daemon" },
47 	{ LOG_AUTH,		"auth" },
48 	{ LOG_SYSLOG,		"syslog" },
49 	{ LOG_LPR,		"lpr" },
50 #ifdef LOG_NEWS
51 	{ LOG_NEWS,		"news" },
52 #endif
53 #ifdef LOG_UUCP
54 	{ LOG_UUCP,		"uucp" },
55 #endif
56 #ifdef LOG_CRON
57 	{ LOG_CRON,		"cron" },
58 #endif
59 #ifdef LOG_AUTHPRIV
60 	{ LOG_AUTHPRIV,		"authpriv" },
61 #endif
62 #ifdef LOG_FTP
63 	{ LOG_FTP,		"ftp" },
64 #endif
65 	{ LOG_LOCAL0,		"local0"},
66 	{ LOG_LOCAL1,		"local1"},
67 	{ LOG_LOCAL2,		"local2"},
68 	{ LOG_LOCAL3,		"local3"},
69 	{ LOG_LOCAL4,		"local4"},
70 	{ LOG_LOCAL5,		"local5"},
71 	{ LOG_LOCAL6,		"local6"},
72 	{ LOG_LOCAL7,		"local7"},
73 	{ 0,			NULL }
74 };
75 
76 isc_result_t
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 (ISC_R_SUCCESS);
87 		}
88 	}
89 	return (ISC_R_NOTFOUND);
90 }
91 
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 		switch (level) {
110 		case LOG_INFO:
111 		case LOG_NOTICE:
112 		case LOG_DEBUG:
113 			ReportEvent(hAppLog, EVENTLOG_INFORMATION_TYPE, 0,
114 				    BIND_INFO_MSG, NULL, 1, 0, str, NULL);
115 			break;
116 		case LOG_WARNING:
117 			ReportEvent(hAppLog, EVENTLOG_WARNING_TYPE, 0,
118 				    BIND_WARN_MSG, NULL, 1, 0, str, NULL);
119 			break;
120 		default:
121 			ReportEvent(hAppLog, EVENTLOG_ERROR_TYPE, 0,
122 				    BIND_ERR_MSG, NULL, 1, 0, str, NULL);
123 			break;
124 		}
125 	}
126 }
127 
128 /*
129  * Initialize event logging
130  */
131 void
132 openlog(const char *name, int flags, ...) {
133 	/* Get a handle to the Application event log */
134 	hAppLog = RegisterEventSource(NULL, name);
135 }
136 
137 /*
138  * Close the Handle to the application Event Log
139  * We don't care whether or not we succeeded so ignore return values
140  * In fact if we failed then we would have nowhere to put the message
141  */
142 void
143 closelog(void) {
144 	DeregisterEventSource(hAppLog);
145 }
146 
147 /*
148  * Keep event logging synced with the current debug level
149  */
150 void
151 ModifyLogLevel(int level) {
152 	debug_level = level;
153 }
154 
155 /*
156  * Initialize logging for the port section of libbind.
157  * Piggyback onto stream given.
158  */
159 void
160 InitNTLogging(FILE *stream, int debug) {
161 	log_stream = stream;
162 	ModifyLogLevel(debug);
163 }
164 /*
165  * This function is for reporting errors to the application
166  * event log in case the regular syslog is not available
167  * mainly during startup. It should not be used under normal
168  * circumstances.
169  */
170 void
171 NTReportError(const char *name, const char *str) {
172 	HANDLE hNTAppLog = NULL;
173 	const char *buf[1];
174 
175 	buf[0] = str;
176 
177 	hNTAppLog = RegisterEventSource(NULL, name);
178 
179 	ReportEvent(hNTAppLog, EVENTLOG_ERROR_TYPE, 0,
180 		    BIND_ERR_MSG, NULL, 1, 0, buf, NULL);
181 
182 	DeregisterEventSource(hNTAppLog);
183 }
184