Skip to content

Commit 1da3682

Browse files
committed
Add GetRenderedEventsWithTimeout
Adds a new function `GetRenderedEventsWithTimeout()` that accepts a timeout to customize the wait duration of the `EvtNext()` call. The existing `GetRenderedEvents()` function is kept for backward compatibility and now calls the new function with a 2-second timeout.
1 parent 0c6d5ae commit 1da3682

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

winlog/winlog.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,32 @@ func DefaultSubscribeConfig() (*SubscribeConfig, error) {
101101
// publisherCache is a cache of Handles for publisher metadata to avoid
102102
// expensive Windows API calls. Pass in an empty map on the first call. Once
103103
// you've finished using GetRenderedEvents, pass all the contained values to Close.
104+
//
105+
// This is a convenience wrapper that calls GetRenderedEventsWithTimeout with a
106+
// reasonable 2-second timeout.
104107
func GetRenderedEvents(config *SubscribeConfig, publisherCache map[string]windows.Handle, resultSet windows.Handle, maxEvents int, locale uint32) ([]string, error) {
108+
return GetRenderedEventsWithTimeout(2*time.Second, config, publisherCache, resultSet, maxEvents, locale)
109+
}
110+
111+
// GetRenderedEventsWithTimeout iterates over a subscription or query result set
112+
// up to a configurable maximum and returns the rendered events as a slice of
113+
// UTF8 formatted XML strings. A timeout is used to get the first event handles.
114+
// publisherCache is a cache of Handles for publisher metadata to avoid
115+
// expensive Windows API calls. Pass in an empty map on the first call. Once
116+
// you've finished using GetRenderedEvents, pass all the contained values to
117+
// Close.
118+
func GetRenderedEventsWithTimeout(timeout time.Duration, config *SubscribeConfig, publisherCache map[string]windows.Handle, resultSet windows.Handle, maxEvents int, locale uint32) ([]string, error) {
105119
var events = make([]windows.Handle, maxEvents)
106120
var returned uint32
107121

108122
// Get handles to events from the result set.
109123
err := wevtapi.EvtNext(
110-
resultSet, // Handle to query or subscription result set.
111-
uint32(len(events)), // The number of events to attempt to retrieve.
112-
&events[0], // Pointer to the array of event handles.
113-
2000, // Timeout in milliseconds to wait.
114-
0, // Reserved. Must be zero.
115-
&returned) // The number of handles in the array that are set by the API.
124+
resultSet, // Handle to query or subscription result set.
125+
uint32(len(events)), // The number of events to attempt to retrieve.
126+
&events[0], // Pointer to the array of event handles.
127+
uint32(timeout.Milliseconds()), // Timeout in milliseconds to wait.
128+
0, // Reserved. Must be zero.
129+
&returned) // The number of handles in the array that are set by the API.
116130
if err == windows.ERROR_NO_MORE_ITEMS {
117131
return nil, err
118132
} else if err != nil {

0 commit comments

Comments
 (0)