2021-01-26 15:35:51 +00:00
|
|
|
// +build windows plan9
|
2020-11-30 16:23:14 +00:00
|
|
|
|
|
|
|
package sysutil
|
2019-02-05 11:09:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2020-12-08 13:01:13 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2019-02-05 11:09:05 +00:00
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
"golang.org/x/sys/windows/svc/eventlog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type eventLogWriter struct {
|
|
|
|
el *eventlog.Log
|
|
|
|
}
|
|
|
|
|
2020-11-30 16:23:14 +00:00
|
|
|
// Write implements io.Writer interface for eventLogWriter.
|
2019-02-05 11:09:05 +00:00
|
|
|
func (w *eventLogWriter) Write(b []byte) (int, error) {
|
|
|
|
return len(b), w.el.Info(1, string(b))
|
|
|
|
}
|
|
|
|
|
2020-11-30 16:23:14 +00:00
|
|
|
func configureSyslog(serviceName string) error {
|
2019-02-05 11:21:07 +00:00
|
|
|
// Note that the eventlog src is the same as the service name
|
|
|
|
// Otherwise, we will get "the description for event id cannot be found" warning in every log record
|
|
|
|
|
2019-02-05 11:09:05 +00:00
|
|
|
// Continue if we receive "registry key already exists" or if we get
|
|
|
|
// ERROR_ACCESS_DENIED so that we can log without administrative permissions
|
|
|
|
// for pre-existing eventlog sources.
|
2019-02-05 11:21:07 +00:00
|
|
|
if err := eventlog.InstallAsEventCreate(serviceName, eventlog.Info|eventlog.Warning|eventlog.Error); err != nil {
|
2019-02-05 11:09:05 +00:00
|
|
|
if !strings.Contains(err.Error(), "registry key already exists") && err != windows.ERROR_ACCESS_DENIED {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-02-05 11:21:07 +00:00
|
|
|
el, err := eventlog.Open(serviceName)
|
2019-02-05 11:09:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetOutput(&eventLogWriter{el: el})
|
|
|
|
return nil
|
|
|
|
}
|