Skip to content

Commit

Permalink
Merge pull request #43 from fluent-plugins-nursery/channel-not-found-…
Browse files Browse the repository at this point in the history
…error

Add a new error Winevt::EventLog::ChannelNotFoundError
  • Loading branch information
ashie authored Sep 21, 2022
2 parents 726eb17 + 00e17aa commit 929ddb9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ext/winevt/winevt.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ VALUE rb_cQuery;
VALUE rb_cEventLog;
VALUE rb_cSubscribe;
VALUE rb_eWinevtQueryError;
VALUE rb_eChannelNotFoundError;
VALUE rb_eRemoteHandlerError;

static ID id_call;
Expand All @@ -17,6 +18,7 @@ Init_winevt(void)
rb_cQuery = rb_define_class_under(rb_cEventLog, "Query", rb_cObject);
rb_cSubscribe = rb_define_class_under(rb_cEventLog, "Subscribe", rb_cObject);
rb_eWinevtQueryError = rb_define_class_under(rb_cQuery, "Error", rb_eStandardError);
rb_eChannelNotFoundError = rb_define_class_under(rb_cEventLog, "ChannelNotFoundError", rb_eStandardError);
rb_eRemoteHandlerError = rb_define_class_under(rb_cSubscribe, "RemoteHandlerError", rb_eRuntimeError);

Init_winevt_channel(rb_cEventLog);
Expand Down
4 changes: 3 additions & 1 deletion ext/winevt/winevt_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ VALUE wstr_to_rb_str(UINT cp, const WCHAR* wstr, int clen);
#if defined(__cplusplus)
[[ noreturn ]]
#endif /* __cplusplus */
void raise_system_error(VALUE error, DWORD errorCode);
void raise_system_error(VALUE error, DWORD errorCode);
void raise_channel_not_found_error(VALUE channelPath);
VALUE render_to_rb_str(EVT_HANDLE handle, DWORD flags);
EVT_HANDLE connect_to_remote(LPWSTR computerName, LPWSTR domain,
LPWSTR username, LPWSTR password,
Expand All @@ -58,6 +59,7 @@ extern VALUE rb_cChannel;
extern VALUE rb_cBookmark;
extern VALUE rb_cSubscribe;
extern VALUE rb_eWinevtQueryError;
extern VALUE rb_eChannelNotFoundError;
extern VALUE rb_eRemoteHandlerError;
extern VALUE rb_cLocale;
extern VALUE rb_cSession;
Expand Down
3 changes: 3 additions & 0 deletions ext/winevt/winevt_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ rb_winevt_query_initialize(VALUE argc, VALUE *argv, VALUE self)
hRemoteHandle, evtChannel, evtXPath, EvtQueryChannelPath | EvtQueryTolerateQueryErrors);
err = GetLastError();
if (err != ERROR_SUCCESS) {
if (err == ERROR_EVT_CHANNEL_NOT_FOUND) {
raise_channel_not_found_error(channel);
}
raise_system_error(rb_eRuntimeError, err);
}
winevtQuery->offset = 0L;
Expand Down
9 changes: 8 additions & 1 deletion ext/winevt/winevt_subscribe.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,17 @@ rb_winevt_subscribe_subscribe(int argc, VALUE* argv, VALUE self)
if (hSignalEvent != NULL) {
CloseHandle(hSignalEvent);
}

if (rb_obj_is_kind_of(rb_session, rb_cSession)) {
rb_raise(rb_eRemoteHandlerError, "Remoting subscription is not working. errCode: %ld\n", status);
} else {
}

switch (status) {
case ERROR_EVT_CHANNEL_NOT_FOUND:
raise_channel_not_found_error(rb_path);
default:
raise_system_error(rb_eWinevtQueryError, status);
break;
}
}

Expand Down
10 changes: 10 additions & 0 deletions ext/winevt/winevt_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ raise_system_error(VALUE error, DWORD errorCode)
#pragma GCC diagnostic pop
}

void
raise_channel_not_found_error(VALUE channelPath)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat="
#pragma GCC diagnostic ignored "-Wformat-extra-args"
rb_raise(rb_eChannelNotFoundError, "Channel Not Found: %" PRIsVALUE, channelPath);
#pragma GCC diagnostic pop
}

VALUE
render_to_rb_str(EVT_HANDLE handle, DWORD flags)
{
Expand Down
8 changes: 8 additions & 0 deletions test/test_winevt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,14 @@ def test_invalid_locale
@subscribe.locale = "ex_EX" # Invalid Locale
end
end

def test_channel_not_found
bookmark = Winevt::EventLog::Bookmark.new
subscribe = Winevt::EventLog::Subscribe.new
assert_raise(Winevt::EventLog::ChannelNotFoundError) do
subscribe.subscribe("NonExistentChannel", "*")
end
end
end

class ChannelTest < self
Expand Down

0 comments on commit 929ddb9

Please sign in to comment.