mirror of
https://github.com/golang/sys.git
synced 2026-02-08 19:56:04 +03:00
`StringToUTF16` is deprecated and will panic if given an "invalid" string (in particular, one that has a null byte in it). The replacement function is `UTF16FromString`, and it returns an error if there was a problem. This change replaces all uses of `StringToUTF16` with `UTF16FromString`. The `service` struct now no longer stores a `string` name but rather a `*uint16` pointer to the name. It should not be possible to panic due to UTF16 string conversion at this point. Fixes golang/go#73006 Change-Id: Idce9cdbb4651fef8481f0cad19b5df0314fd4277 Reviewed-on: https://go-review.googlesource.com/c/sys/+/659936 Reviewed-by: Carlos Amedee <carlos@golang.org> Auto-Submit: Carlos Amedee <carlos@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alex Brainman <alex.brainman@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
// Copyright 2012 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//go:build windows
|
|
|
|
// Package eventlog implements access to Windows event log.
|
|
package eventlog
|
|
|
|
import (
|
|
"errors"
|
|
"syscall"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// Log provides access to the system log.
|
|
type Log struct {
|
|
Handle windows.Handle
|
|
}
|
|
|
|
// Open retrieves a handle to the specified event log.
|
|
func Open(source string) (*Log, error) {
|
|
return OpenRemote("", source)
|
|
}
|
|
|
|
// OpenRemote does the same as Open, but on different computer host.
|
|
func OpenRemote(host, source string) (*Log, error) {
|
|
if source == "" {
|
|
return nil, errors.New("Specify event log source")
|
|
}
|
|
var hostPointer *uint16
|
|
if host != "" {
|
|
var err error
|
|
hostPointer, err = syscall.UTF16PtrFromString(host)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
sourcePointer, err := syscall.UTF16PtrFromString(source)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
h, err := windows.RegisterEventSource(hostPointer, sourcePointer)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Log{Handle: h}, nil
|
|
}
|
|
|
|
// Close closes event log l.
|
|
func (l *Log) Close() error {
|
|
return windows.DeregisterEventSource(l.Handle)
|
|
}
|
|
|
|
func (l *Log) report(etype uint16, eid uint32, msg string) error {
|
|
msgPointer, err := syscall.UTF16PtrFromString(msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ss := []*uint16{msgPointer}
|
|
return windows.ReportEvent(l.Handle, etype, 0, eid, 0, 1, 0, &ss[0], nil)
|
|
}
|
|
|
|
// Info writes an information event msg with event id eid to the end of event log l.
|
|
// When EventCreate.exe is used, eid must be between 1 and 1000.
|
|
func (l *Log) Info(eid uint32, msg string) error {
|
|
return l.report(windows.EVENTLOG_INFORMATION_TYPE, eid, msg)
|
|
}
|
|
|
|
// Warning writes an warning event msg with event id eid to the end of event log l.
|
|
// When EventCreate.exe is used, eid must be between 1 and 1000.
|
|
func (l *Log) Warning(eid uint32, msg string) error {
|
|
return l.report(windows.EVENTLOG_WARNING_TYPE, eid, msg)
|
|
}
|
|
|
|
// Error writes an error event msg with event id eid to the end of event log l.
|
|
// When EventCreate.exe is used, eid must be between 1 and 1000.
|
|
func (l *Log) Error(eid uint32, msg string) error {
|
|
return l.report(windows.EVENTLOG_ERROR_TYPE, eid, msg)
|
|
}
|