forked from zensquare/evtsys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
222 lines (180 loc) · 5.72 KB
/
log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
This code is a modification of the original Eventlog to Syslog Script written by
Curtis Smith of Purdue University. The original copyright notice can be found below.
The original program was modified by Sherwin Faria for Rochester Institute of Technology
in July 2009 to provide bug fixes and add several new features. Additions include
the ability to ignore specific events, add the event timestamp to outgoing messages,
a service status file, and compatibility with the new Vista/2k8 Windows Events service.
Sherwin Faria
Rochester Institute of Technology
Information & Technology Services Bldg. 10
1 Lomb Memorial Drive
Rochester, NY 14623 U.S.A.
Send all comments, suggestions, or bug reports to:
*/
/*
Copyright (c) 1998-2007, Purdue University
All rights reserved.
Redistribution and use in source and binary forms are permitted provided
that:
(1) source distributions retain this entire copyright notice and comment,
and
(2) distributions including binaries display the following acknowledgement:
"This product includes software developed by Purdue University."
in the documentation or other materials provided with the distribution
and in all advertising materials mentioning features or use of this
software.
The name of the University may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
This software was developed by:
Curtis Smith
Purdue University
Engineering Computer Network
465 Northwestern Avenue
West Lafayette, Indiana 47907-2035 U.S.A.
Send all comments, suggestions, or bug reports to:
*/
/* Include files */
#include "main.h"
#include "log.h"
#include "syslog.h"
#include "wsock.h"
#include <io.h>
/* Indicate if interactive logging is available */
int LogInteractive = 0;
/* Indicate if eventlog is initialized */
static HANDLE LogSource = NULL;
/* Start using eventlog */
int LogStart()
{
/* Indicate if interactive logging is available */
LogInteractive = _isatty(_fileno(stdout));
/* Open connection to event logger */
LogSource = RegisterEventSource(NULL, "EvtSys");
if (LogSource == NULL) {
Log(LOG_ERROR|LOG_SYS, "Cannot register source for event logging");
return 1;
}
/* Success */
return 0;
}
/* Stop using eventlog */
void LogStop()
{
/* Check indicator */
if (LogSource != NULL) {
/* Deregister source */
DeregisterEventSource(LogSource);
/* Reset indicators */
LogSource = NULL;
}
}
/* Send a message to the eventlog */
static int LogSend(WORD level, char * message)
{
char * messages[1];
/* Check that the event log is open */
if (LogSource) {
/* Set up array */
messages[0] = message;
/* Process event */
if (ReportEvent(LogSource, level, 0, 1, NULL, COUNT_OF(messages), 0, messages, NULL) == FALSE)
return 1;
}
/* Success */
return 0;
}
/* Print out an error message */
void Log(int level, char * message, ...)
{
WORD eventlog_priority;
char hostname[HOSTNAME_SZ];
char windows_message[ERRMSG_SZ];
char error_message[SYSLOG_DEF_SZ-17];
char tstamped_message[SYSLOG_DEF_SZ];
int syslog_level;
va_list args;
static BOOL logging = FALSE;
/* This prevents recursive errors */
if (logging)
return;
logging = TRUE;
/* Format and output system message */
if (level & LOG_SYS)
GetError(GetLastError(), windows_message, sizeof(windows_message));
/* Format and output message */
va_start(args, message);
vsnprintf_s(error_message, sizeof(error_message), _TRUNCATE, message, args);
va_end(args);
/* Append system error message */
if (level & LOG_SYS) {
/* Remove bit */
level &= ~LOG_SYS;
/* Add windows error message */
strncat_s(error_message, sizeof(error_message), ": ", _TRUNCATE);
strncat_s(error_message, sizeof(error_message), windows_message, _TRUNCATE);
}
/* Convert local level to eventlog or syslog priority */
switch (level) {
case LOG_ERROR:
eventlog_priority = EVENTLOG_ERROR_TYPE;
syslog_level = SYSLOG_BUILD(SyslogFacility, SYSLOG_ERR);
break;
case LOG_WARNING:
eventlog_priority = EVENTLOG_WARNING_TYPE;
syslog_level = SYSLOG_BUILD(SyslogFacility, SYSLOG_WARNING);
break;
case LOG_INFO:
eventlog_priority = EVENTLOG_INFORMATION_TYPE;
syslog_level = SYSLOG_BUILD(SyslogFacility, SYSLOG_NOTICE);
break;
}
/* Add hostname for RFC compliance (RFC 3164) */
if (ProgramUseIPAddress == TRUE) {
strcpy_s(hostname, HOSTNAME_SZ, ProgramHostName);
} else {
if (ExpandEnvironmentStrings("%COMPUTERNAME%", hostname, COUNT_OF(hostname)) == 0) {
strcpy_s(hostname, COUNT_OF(hostname), "HOSTNAME_ERR");
Log(LOG_ERROR|LOG_SYS, "Cannot expand %COMPUTERNAME%");
}
}
/* Create Timestamp and add to error_message along with hostname */
/* This maintains consistency with regular non-error packets */
if(SyslogIncludeTag)
{
_snprintf_s(tstamped_message, sizeof(tstamped_message), _TRUNCATE,
"%s %s %s: %s",
GetTimeStamp(),
hostname,
SyslogTag,
error_message
);
}
else
{
_snprintf_s(tstamped_message, sizeof(tstamped_message), _TRUNCATE,
"%s %s %s",
GetTimeStamp(),
hostname,
error_message
);
}
/* Send to syslog if network running */
if (SyslogSend(tstamped_message, syslog_level))
{
/* Otherwise, send to eventlog */
LogSend(eventlog_priority, tstamped_message);
}
/* Output to console */
if (LogInteractive) {
fputs(tstamped_message, stderr);
fputc('\n', stderr);
}
/* Okay to log again */
logging = FALSE;
}