From 93ea27077fc727a072ba01bb029cd0c81800f1dc Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Tue, 5 Feb 2019 14:21:07 +0300 Subject: [PATCH] service properties to constants --- service.go | 13 ++++++++----- syslog_others.go | 2 +- syslog_windows.go | 10 +++++----- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/service.go b/service.go index f7ffb8da..fd233f4e 100644 --- a/service.go +++ b/service.go @@ -9,8 +9,11 @@ import ( ) const ( - launchdStdoutPath = "/var/log/AdGuardHome.stdout.log" - launchdStderrPath = "/var/log/AdGuardHome.stderr.log" + launchdStdoutPath = "/var/log/AdGuardHome.stdout.log" + launchdStderrPath = "/var/log/AdGuardHome.stderr.log" + serviceName = "AdGuardHome" + serviceDisplayName = "AdGuard Home service" + serviceDescription = "AdGuard Home: Network-level blocker" ) // Represents the program that will be launched by a service or daemon @@ -51,9 +54,9 @@ func handleServiceControlAction(action string) { log.Fatal("Unable to find the path to the current directory") } svcConfig := &service.Config{ - Name: "AdGuardHome", - DisplayName: "AdGuard Home service", - Description: "AdGuard Home: Network-level blocker", + Name: serviceName, + DisplayName: serviceDisplayName, + Description: serviceDescription, WorkingDirectory: pwd, Arguments: []string{"-s", "run"}, } diff --git a/syslog_others.go b/syslog_others.go index 629d170d..1813bb99 100644 --- a/syslog_others.go +++ b/syslog_others.go @@ -9,7 +9,7 @@ import ( // configureSyslog reroutes standard logger output to syslog func configureSyslog() error { - w, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_USER, "AdGuard Home") + w, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_USER, serviceName) if err != nil { return err } diff --git a/syslog_windows.go b/syslog_windows.go index b6f1d885..e81b63ee 100644 --- a/syslog_windows.go +++ b/syslog_windows.go @@ -8,9 +8,6 @@ import ( "golang.org/x/sys/windows/svc/eventlog" ) -// should be the same as the service name! -const eventLogSrc = "AdGuardHome" - type eventLogWriter struct { el *eventlog.Log } @@ -21,15 +18,18 @@ func (w *eventLogWriter) Write(b []byte) (int, error) { } func configureSyslog() 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(eventLogSrc, eventlog.Info|eventlog.Warning|eventlog.Error); err != nil { + 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(eventLogSrc) + el, err := eventlog.Open(serviceName) if err != nil { return err }