2022-09-13 20:06:23 +03:00
|
|
|
//go:build windows
|
2021-05-21 14:55:42 +03:00
|
|
|
|
2021-03-16 19:42:15 +03:00
|
|
|
package aghos
|
2019-02-05 14:09:05 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2020-12-08 16:01:13 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2019-02-05 14:09:05 +03:00
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
"golang.org/x/sys/windows/svc/eventlog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type eventLogWriter struct {
|
|
|
|
el *eventlog.Log
|
|
|
|
}
|
|
|
|
|
2020-11-30 19:23:14 +03:00
|
|
|
// Write implements io.Writer interface for eventLogWriter.
|
2019-02-05 14:09:05 +03:00
|
|
|
func (w *eventLogWriter) Write(b []byte) (int, error) {
|
|
|
|
return len(b), w.el.Info(1, string(b))
|
|
|
|
}
|
|
|
|
|
2024-07-10 15:18:46 +03:00
|
|
|
// configureSyslog sets standard log output to event log.
|
|
|
|
func configureSyslog(serviceName string) (err error) {
|
|
|
|
// 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 14:21:07 +03:00
|
|
|
|
2019-02-05 14:09:05 +03: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.
|
2024-07-10 15:18:46 +03:00
|
|
|
err = eventlog.InstallAsEventCreate(serviceName, eventlog.Info|eventlog.Warning|eventlog.Error)
|
|
|
|
if err != nil &&
|
|
|
|
!strings.Contains(err.Error(), "registry key already exists") &&
|
|
|
|
err != windows.ERROR_ACCESS_DENIED {
|
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return err
|
2019-02-05 14:09:05 +03:00
|
|
|
}
|
2024-07-10 15:18:46 +03:00
|
|
|
|
2019-02-05 14:21:07 +03:00
|
|
|
el, err := eventlog.Open(serviceName)
|
2019-02-05 14:09:05 +03:00
|
|
|
if err != nil {
|
2024-07-10 15:18:46 +03:00
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
2019-02-05 14:09:05 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetOutput(&eventLogWriter{el: el})
|
2024-07-10 15:18:46 +03:00
|
|
|
|
2019-02-05 14:09:05 +03:00
|
|
|
return nil
|
|
|
|
}
|