golang.org/x/sys@v0.20.1-0.20240517151509-673e0f94c16d/windows/svc/eventlog/log.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build windows 6 7 // Package eventlog implements access to Windows event log. 8 package eventlog 9 10 import ( 11 "errors" 12 "syscall" 13 14 "golang.org/x/sys/windows" 15 ) 16 17 // Log provides access to the system log. 18 type Log struct { 19 Handle windows.Handle 20 } 21 22 // Open retrieves a handle to the specified event log. 23 func Open(source string) (*Log, error) { 24 return OpenRemote("", source) 25 } 26 27 // OpenRemote does the same as Open, but on different computer host. 28 func OpenRemote(host, source string) (*Log, error) { 29 if source == "" { 30 return nil, errors.New("Specify event log source") 31 } 32 var s *uint16 33 if host != "" { 34 s = syscall.StringToUTF16Ptr(host) 35 } 36 h, err := windows.RegisterEventSource(s, syscall.StringToUTF16Ptr(source)) 37 if err != nil { 38 return nil, err 39 } 40 return &Log{Handle: h}, nil 41 } 42 43 // Close closes event log l. 44 func (l *Log) Close() error { 45 return windows.DeregisterEventSource(l.Handle) 46 } 47 48 func (l *Log) report(etype uint16, eid uint32, msg string) error { 49 ss := []*uint16{syscall.StringToUTF16Ptr(msg)} 50 return windows.ReportEvent(l.Handle, etype, 0, eid, 0, 1, 0, &ss[0], nil) 51 } 52 53 // Info writes an information event msg with event id eid to the end of event log l. 54 // When EventCreate.exe is used, eid must be between 1 and 1000. 55 func (l *Log) Info(eid uint32, msg string) error { 56 return l.report(windows.EVENTLOG_INFORMATION_TYPE, eid, msg) 57 } 58 59 // Warning writes an warning event msg with event id eid to the end of event log l. 60 // When EventCreate.exe is used, eid must be between 1 and 1000. 61 func (l *Log) Warning(eid uint32, msg string) error { 62 return l.report(windows.EVENTLOG_WARNING_TYPE, eid, msg) 63 } 64 65 // Error writes an error event msg with event id eid to the end of event log l. 66 // When EventCreate.exe is used, eid must be between 1 and 1000. 67 func (l *Log) Error(eid uint32, msg string) error { 68 return l.report(windows.EVENTLOG_ERROR_TYPE, eid, msg) 69 }