AdGuardHome/internal/aghos/syslog_windows.go
Ainar Garipov 9f5a015f42 Pull request: all: imp build tags
Updates #2419.

Squashed commit of the following:

commit e57e6ce56c59d4ef42b1716247fe48a86404179e
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Jun 15 19:37:15 2021 +0300

    all: imp build tags
2021-06-15 19:42:41 +03:00

43 lines
1.2 KiB
Go

//go:build windows || plan9
// +build windows plan9
package aghos
import (
"strings"
"github.com/AdguardTeam/golibs/log"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/svc/eventlog"
)
type eventLogWriter struct {
el *eventlog.Log
}
// Write implements io.Writer interface for eventLogWriter.
func (w *eventLogWriter) Write(b []byte) (int, error) {
return len(b), w.el.Info(1, string(b))
}
func configureSyslog(serviceName string) 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
// 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.
if err := eventlog.InstallAsEventCreate(serviceName, eventlog.Info|eventlog.Warning|eventlog.Error); err != nil {
if !strings.Contains(err.Error(), "registry key already exists") && err != windows.ERROR_ACCESS_DENIED {
return err
}
}
el, err := eventlog.Open(serviceName)
if err != nil {
return err
}
log.SetOutput(&eventLogWriter{el: el})
return nil
}