AdGuardHome/internal/sysutil/syslog_windows.go
Ainar Garipov 73c30590e0 Pull request: all: fix lint and naming issues vol. 4
Merge in DNS/adguard-home from 2276-fix-lint-4 to master

Updates #2276.

Squashed commit of the following:

commit 15d49184cd8ce1f8701bf3221e69418ca1778b36
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Dec 8 15:51:34 2020 +0300

    util: fix naming

commit 3b9a86a0feb8c6e0b167e6e23105e8137b0dda76
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Dec 8 15:41:10 2020 +0300

    all: fix lint and naming issues vol. 4
2020-12-08 16:01:13 +03:00

42 lines
1.1 KiB
Go

// +build windows nacl plan9
package sysutil
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
}