Skip to content

Commit 00e17aa

Browse files
committed
Add a new error Winevt::EventLog::ChannelNotFoundError
Our customer who uses fluent-plugin-windows-evnetlog wants to skip unknown channels without stoping Fluentd. It's reasonable because channels are dynamically added or removed. But winevt_c doesn't provide a way to distinguish `ERROR_EVT_CHANNEL_NOT_FOUND` and other errors. This commit adds `Winevt::EventLog::ChannelNotFoundError` and raise it when `ERROR_EVT_CHANNEL_NOT_FOUND` is occurred. Signed-off-by: Takuro Ashie <[email protected]>
1 parent 9bcea01 commit 00e17aa

File tree

6 files changed

+34
-2
lines changed

6 files changed

+34
-2
lines changed

ext/winevt/winevt.c

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ VALUE rb_cQuery;
55
VALUE rb_cEventLog;
66
VALUE rb_cSubscribe;
77
VALUE rb_eWinevtQueryError;
8+
VALUE rb_eChannelNotFoundError;
89
VALUE rb_eRemoteHandlerError;
910

1011
static ID id_call;
@@ -17,6 +18,7 @@ Init_winevt(void)
1718
rb_cQuery = rb_define_class_under(rb_cEventLog, "Query", rb_cObject);
1819
rb_cSubscribe = rb_define_class_under(rb_cEventLog, "Subscribe", rb_cObject);
1920
rb_eWinevtQueryError = rb_define_class_under(rb_cQuery, "Error", rb_eStandardError);
21+
rb_eChannelNotFoundError = rb_define_class_under(rb_cEventLog, "ChannelNotFoundError", rb_eStandardError);
2022
rb_eRemoteHandlerError = rb_define_class_under(rb_cSubscribe, "RemoteHandlerError", rb_eRuntimeError);
2123

2224
Init_winevt_channel(rb_cEventLog);

ext/winevt/winevt_c.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ VALUE wstr_to_rb_str(UINT cp, const WCHAR* wstr, int clen);
3737
#if defined(__cplusplus)
3838
[[ noreturn ]]
3939
#endif /* __cplusplus */
40-
void raise_system_error(VALUE error, DWORD errorCode);
40+
void raise_system_error(VALUE error, DWORD errorCode);
41+
void raise_channel_not_found_error(VALUE channelPath);
4142
VALUE render_to_rb_str(EVT_HANDLE handle, DWORD flags);
4243
EVT_HANDLE connect_to_remote(LPWSTR computerName, LPWSTR domain,
4344
LPWSTR username, LPWSTR password,
@@ -58,6 +59,7 @@ extern VALUE rb_cChannel;
5859
extern VALUE rb_cBookmark;
5960
extern VALUE rb_cSubscribe;
6061
extern VALUE rb_eWinevtQueryError;
62+
extern VALUE rb_eChannelNotFoundError;
6163
extern VALUE rb_eRemoteHandlerError;
6264
extern VALUE rb_cLocale;
6365
extern VALUE rb_cSession;

ext/winevt/winevt_query.c

+3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ rb_winevt_query_initialize(VALUE argc, VALUE *argv, VALUE self)
131131
hRemoteHandle, evtChannel, evtXPath, EvtQueryChannelPath | EvtQueryTolerateQueryErrors);
132132
err = GetLastError();
133133
if (err != ERROR_SUCCESS) {
134+
if (err == ERROR_EVT_CHANNEL_NOT_FOUND) {
135+
raise_channel_not_found_error(channel);
136+
}
134137
raise_system_error(rb_eRuntimeError, err);
135138
}
136139
winevtQuery->offset = 0L;

ext/winevt/winevt_subscribe.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,17 @@ rb_winevt_subscribe_subscribe(int argc, VALUE* argv, VALUE self)
248248
if (hSignalEvent != NULL) {
249249
CloseHandle(hSignalEvent);
250250
}
251+
251252
if (rb_obj_is_kind_of(rb_session, rb_cSession)) {
252253
rb_raise(rb_eRemoteHandlerError, "Remoting subscription is not working. errCode: %ld\n", status);
253-
} else {
254+
}
255+
256+
switch (status) {
257+
case ERROR_EVT_CHANNEL_NOT_FOUND:
258+
raise_channel_not_found_error(rb_path);
259+
default:
254260
raise_system_error(rb_eWinevtQueryError, status);
261+
break;
255262
}
256263
}
257264

ext/winevt/winevt_utils.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ raise_system_error(VALUE error, DWORD errorCode)
5656
#pragma GCC diagnostic pop
5757
}
5858

59+
void
60+
raise_channel_not_found_error(VALUE channelPath)
61+
{
62+
#pragma GCC diagnostic push
63+
#pragma GCC diagnostic ignored "-Wformat="
64+
#pragma GCC diagnostic ignored "-Wformat-extra-args"
65+
rb_raise(rb_eChannelNotFoundError, "Channel Not Found: %" PRIsVALUE, channelPath);
66+
#pragma GCC diagnostic pop
67+
}
68+
5969
VALUE
6070
render_to_rb_str(EVT_HANDLE handle, DWORD flags)
6171
{

test/test_winevt.rb

+8
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ def test_invalid_locale
300300
@subscribe.locale = "ex_EX" # Invalid Locale
301301
end
302302
end
303+
304+
def test_channel_not_found
305+
bookmark = Winevt::EventLog::Bookmark.new
306+
subscribe = Winevt::EventLog::Subscribe.new
307+
assert_raise(Winevt::EventLog::ChannelNotFoundError) do
308+
subscribe.subscribe("NonExistentChannel", "*")
309+
end
310+
end
303311
end
304312

305313
class ChannelTest < self

0 commit comments

Comments
 (0)