service properties to constants

This commit is contained in:
Andrey Meshkov 2019-02-05 14:21:07 +03:00
parent aab8da4c7c
commit 93ea27077f
3 changed files with 14 additions and 11 deletions

View File

@ -11,6 +11,9 @@ import (
const ( const (
launchdStdoutPath = "/var/log/AdGuardHome.stdout.log" launchdStdoutPath = "/var/log/AdGuardHome.stdout.log"
launchdStderrPath = "/var/log/AdGuardHome.stderr.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 // 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") log.Fatal("Unable to find the path to the current directory")
} }
svcConfig := &service.Config{ svcConfig := &service.Config{
Name: "AdGuardHome", Name: serviceName,
DisplayName: "AdGuard Home service", DisplayName: serviceDisplayName,
Description: "AdGuard Home: Network-level blocker", Description: serviceDescription,
WorkingDirectory: pwd, WorkingDirectory: pwd,
Arguments: []string{"-s", "run"}, Arguments: []string{"-s", "run"},
} }

View File

@ -9,7 +9,7 @@ import (
// configureSyslog reroutes standard logger output to syslog // configureSyslog reroutes standard logger output to syslog
func configureSyslog() error { 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 { if err != nil {
return err return err
} }

View File

@ -8,9 +8,6 @@ import (
"golang.org/x/sys/windows/svc/eventlog" "golang.org/x/sys/windows/svc/eventlog"
) )
// should be the same as the service name!
const eventLogSrc = "AdGuardHome"
type eventLogWriter struct { type eventLogWriter struct {
el *eventlog.Log el *eventlog.Log
} }
@ -21,15 +18,18 @@ func (w *eventLogWriter) Write(b []byte) (int, error) {
} }
func configureSyslog() 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 // 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(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 { if !strings.Contains(err.Error(), "registry key already exists") && err != windows.ERROR_ACCESS_DENIED {
return err return err
} }
} }
el, err := eventlog.Open(eventLogSrc) el, err := eventlog.Open(serviceName)
if err != nil { if err != nil {
return err return err
} }