Skip to content

Commit 9f8a2a2

Browse files
authored
Small refactor for Windows Event Log gap handling (#1777)
1 parent 7c660ef commit 9f8a2a2

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

plugins/inputs/windows_event_log/wineventlog/utils.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"strings"
1616
"syscall"
1717
"time"
18-
"unsafe"
1918

2019
"golang.org/x/text/encoding/unicode"
2120
"golang.org/x/text/transform"
@@ -238,17 +237,3 @@ func insertPlaceholderValues(rawMessage string, evtDataValues []Datum) string {
238237
}
239238
return sb.String()
240239
}
241-
242-
func utf16PtrToString(ptr *uint16) string {
243-
utf16Slice := make([]uint16, 0, 1024)
244-
for i := 0; ; i++ {
245-
// Get the value at memory address ptr + (i * sizeof(uint16))
246-
element := *(*uint16)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) + uintptr(i)*unsafe.Sizeof(uint16(0))))
247-
248-
if element == 0 {
249-
break // Null terminator found
250-
}
251-
utf16Slice = append(utf16Slice, element)
252-
}
253-
return syscall.UTF16ToString(utf16Slice)
254-
}

plugins/inputs/windows_event_log/wineventlog/utils_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ package wineventlog
88

99
import (
1010
"encoding/hex"
11+
"syscall"
1112
"testing"
13+
"unsafe"
1214

1315
"github.com/stretchr/testify/assert"
1416

@@ -252,3 +254,17 @@ func TestCreateRangeQuery(t *testing.T) {
252254
func resetState() {
253255
NumberOfBytesPerCharacter = 0
254256
}
257+
258+
func utf16PtrToString(ptr *uint16) string {
259+
utf16Slice := make([]uint16, 0, 1024)
260+
for i := 0; ; i++ {
261+
// Get the value at memory address ptr + (i * sizeof(uint16))
262+
element := *(*uint16)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) + uintptr(i)*unsafe.Sizeof(uint16(0))))
263+
264+
if element == 0 {
265+
break // Null terminator found
266+
}
267+
utf16Slice = append(utf16Slice, element)
268+
}
269+
return syscall.UTF16ToString(utf16Slice)
270+
}

plugins/inputs/windows_event_log/wineventlog/wineventlog.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,10 @@ func (w *windowsEventLog) readGaps() []*windowsEventLogRecord {
302302
continue
303303
}
304304

305-
handle, err := w.openAtRange(r)
306-
defer func() {
307-
winEventAPI.EvtClose(handle)
308-
}()
305+
readRecords, err := w.readGap(r)
309306
if err != nil {
310307
continue
311308
}
312-
readRecords := w.readFromHandle(handle)
313309
records = append(records, readRecords...)
314310
}
315311

@@ -319,6 +315,18 @@ func (w *windowsEventLog) readGaps() []*windowsEventLogRecord {
319315
return records
320316
}
321317

318+
func (w *windowsEventLog) readGap(r state.Range) ([]*windowsEventLogRecord, error) {
319+
handle, err := w.openAtRange(r)
320+
defer func() {
321+
winEventAPI.EvtClose(handle)
322+
}()
323+
if err != nil {
324+
return nil, err
325+
}
326+
readRecords := w.readFromHandle(handle)
327+
return readRecords, nil
328+
}
329+
322330
func (w *windowsEventLog) read() []*windowsEventLogRecord {
323331
return w.readFromHandle(w.eventHandle)
324332
}

plugins/inputs/windows_event_log/wineventlog/wineventlog_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,15 @@ func TestReadGaps(t *testing.T) {
182182
winEventAPI = mockAPI
183183

184184
// This is per EvtHandle hence the necessity to break up these calls
185-
// 0, 1, 4 were "sent" previously (should be skipped)
186-
mockAPI.AddMockEventsForQuery(createMockEventRecords(0, 2, 5))
185+
// 1, 2, 5 were "sent" previously (should be skipped)
186+
mockAPI.AddMockEventsForQuery(createMockEventRecords(1, 2, 5))
187187
// Gap records (should be read by gap reading)
188188
mockAPI.AddMockEventsForQuery(createMockEventRecords(3, 4))
189189

190190
elog.Init()
191191

192192
// Simulate new subscription events arriving
193-
mockAPI.SimulateSubscriptionEvents(createMockEventRecords(5, 6, 7, 8))
193+
mockAPI.SimulateSubscriptionEvents(createMockEventRecords(6, 7, 8))
194194

195195
var records []logs.LogEvent
196196
// SetOutput calls run as well hence the omission of elog.run()
@@ -202,7 +202,7 @@ func TestReadGaps(t *testing.T) {
202202
elog.Stop()
203203

204204
expectedRecords := []int{
205-
3, 4, 5, 6, 7, 8,
205+
3, 4, 6, 7, 8,
206206
}
207207

208208
assert.Empty(t, elog.gapsToRead, "Gaps should be cleared after reading")

0 commit comments

Comments
 (0)