aghos: imp code

This commit is contained in:
Dimitry Kolyshev 2024-07-08 10:49:46 +03:00
parent cdf9ccd371
commit ba4a7e1c70
3 changed files with 20 additions and 9 deletions

View File

@ -1,6 +1,6 @@
package aghos package aghos
// ConfigureSyslog reroutes standard logger output to syslog. // ConfigureSyslog reroutes standard logger output to syslog.
func ConfigureSyslog(serviceName string) error { func ConfigureSyslog(serviceName string) (err error) {
return configureSyslog(serviceName) return configureSyslog(serviceName)
} }

View File

@ -8,11 +8,15 @@ import (
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
) )
func configureSyslog(serviceName string) error { // configureSyslog sets standard log output to syslog.
func configureSyslog(serviceName string) (err error) {
w, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_USER, serviceName) w, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_USER, serviceName)
if err != nil { if err != nil {
// Don't wrap the error, because it's informative enough as is.
return err return err
} }
log.SetOutput(w) log.SetOutput(w)
return nil return nil
} }

View File

@ -19,23 +19,30 @@ func (w *eventLogWriter) Write(b []byte) (int, error) {
return len(b), w.el.Info(1, string(b)) return len(b), w.el.Info(1, string(b))
} }
func configureSyslog(serviceName string) error { // configureSyslog sets standard log output to event log.
// Note that the eventlog src is the same as the service name func configureSyslog(serviceName string) (err error) {
// Otherwise, we will get "the description for event id cannot be found" warning in every log record // 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 // Continue if we receive "registry key already exists" or if we get
// ERROR_ACCESS_DENIED so that we can log without administrative permissions // ERROR_ACCESS_DENIED so that we can log without administrative permissions
// for pre-existing eventlog sources. // for pre-existing eventlog sources.
if err := eventlog.InstallAsEventCreate(serviceName, eventlog.Info|eventlog.Warning|eventlog.Error); err != nil { err = eventlog.InstallAsEventCreate(serviceName, eventlog.Info|eventlog.Warning|eventlog.Error)
if !strings.Contains(err.Error(), "registry key already exists") && err != windows.ERROR_ACCESS_DENIED { if err != nil &&
return err !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
} }
el, err := eventlog.Open(serviceName) el, err := eventlog.Open(serviceName)
if err != nil { if err != nil {
// Don't wrap the error, because it's informative enough as is.
return err return err
} }
log.SetOutput(&eventLogWriter{el: el}) log.SetOutput(&eventLogWriter{el: el})
return nil return nil
} }