Pull request: home: imp logs

Closes #3869.

Squashed commit of the following:

commit 8ee0625ea2ac1f6615ad56c819fbb0c4974767e5
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Nov 23 20:33:14 2021 +0300

    home: imp logs
This commit is contained in:
Ainar Garipov 2021-11-23 20:45:14 +03:00
parent 51f11d2f8e
commit 322944073e

View File

@ -97,7 +97,7 @@ func svcAction(s service.Service, action string) (err error) {
// exist, find our PID using 'ps' command.
func sendSigReload() {
if runtime.GOOS == "windows" {
log.Error("not implemented on windows")
log.Error("service: not implemented on windows")
return
}
@ -107,25 +107,24 @@ func sendSigReload() {
data, err := os.ReadFile(pidfile)
if errors.Is(err, os.ErrNotExist) {
if pid, err = aghos.PIDByCommand(serviceName, os.Getpid()); err != nil {
log.Error("finding AdGuardHome process: %s", err)
log.Error("service: finding AdGuardHome process: %s", err)
return
}
} else if err != nil {
log.Error("reading pid file %s: %s", pidfile, err)
log.Error("service: reading pid file %s: %s", pidfile, err)
return
} else {
parts := strings.SplitN(string(data), "\n", 2)
if len(parts) == 0 {
log.Error("can't read pid file %s: bad value", pidfile)
log.Error("service: parsing pid file %s: bad value", pidfile)
return
}
if pid, err = strconv.Atoi(strings.TrimSpace(parts[0])); err != nil {
log.Error("can't read pid file %s: %s", pidfile, err)
log.Error("service: parsing pid from file %s: %s", pidfile, err)
return
}
@ -133,18 +132,18 @@ func sendSigReload() {
var proc *os.Process
if proc, err = os.FindProcess(pid); err != nil {
log.Error("can't send signal to pid %d: %s", pid, err)
log.Error("service: finding process for pid %d: %s", pid, err)
return
}
if err = proc.Signal(syscall.SIGHUP); err != nil {
log.Error("Can't send signal to pid %d: %s", pid, err)
log.Error("service: sending signal HUP to pid %d: %s", pid, err)
return
}
log.Debug("sent signal to PID %d", pid)
log.Debug("service: sent signal to pid %d", pid)
}
// handleServiceControlAction one of the possible control actions:
@ -163,7 +162,7 @@ func handleServiceControlAction(opts options, clientBuildFS fs.FS) {
chooseSystem()
action := opts.serviceControlAction
log.Printf("Service control action: %s", action)
log.Printf("service: control action: %s", action)
if action == "reload" {
sendSigReload()
@ -173,7 +172,7 @@ func handleServiceControlAction(opts options, clientBuildFS fs.FS) {
pwd, err := os.Getwd()
if err != nil {
log.Fatal("Unable to find the path to the current directory")
log.Fatalf("service: getting current directory: %s", err)
}
runOpts := opts
@ -193,7 +192,7 @@ func handleServiceControlAction(opts options, clientBuildFS fs.FS) {
}
var s service.Service
if s, err = service.New(prg, svcConfig); err != nil {
log.Fatal(err)
log.Fatalf("service: initializing service: %s", err)
}
switch action {
@ -201,7 +200,7 @@ func handleServiceControlAction(opts options, clientBuildFS fs.FS) {
handleServiceStatusCommand(s)
case "run":
if err = s.Run(); err != nil {
log.Fatalf("Failed to run service: %s", err)
log.Fatalf("service: failed to run service: %s", err)
}
case "install":
initConfigFilename(opts)
@ -211,27 +210,27 @@ func handleServiceControlAction(opts options, clientBuildFS fs.FS) {
handleServiceUninstallCommand(s)
default:
if err = svcAction(s, action); err != nil {
log.Fatal(err)
log.Fatalf("service: executing action %q: %s", action, err)
}
}
log.Printf("action %s has been done successfully on %s", action, service.ChosenSystem())
log.Printf("service: action %s has been done successfully on %s", action, service.ChosenSystem())
}
// handleServiceStatusCommand handles service "status" command.
func handleServiceStatusCommand(s service.Service) {
status, errSt := svcStatus(s)
if errSt != nil {
log.Fatalf("failed to get service status: %s", errSt)
log.Fatalf("service: failed to get service status: %s", errSt)
}
switch status {
case service.StatusUnknown:
log.Printf("Service status is unknown")
log.Printf("service: status is unknown")
case service.StatusStopped:
log.Printf("Service is stopped")
log.Printf("service: stopped")
case service.StatusRunning:
log.Printf("Service is running")
log.Printf("service: running")
}
}
@ -239,7 +238,7 @@ func handleServiceStatusCommand(s service.Service) {
func handleServiceInstallCommand(s service.Service) {
err := svcAction(s, "install")
if err != nil {
log.Fatal(err)
log.Fatalf("service: executing action %q: %s", "install", err)
}
if aghos.IsOpenWrt() {
@ -248,16 +247,16 @@ func handleServiceInstallCommand(s service.Service) {
// startup.
_, err = runInitdCommand("enable")
if err != nil {
log.Fatal(err)
log.Fatalf("service: running init enable: %s", err)
}
}
// Start automatically after install.
err = svcAction(s, "start")
if err != nil {
log.Fatalf("Failed to start the service: %s", err)
log.Fatalf("service: starting: %s", err)
}
log.Printf("Service has been started")
log.Printf("service: started")
if detectFirstRun() {
log.Printf(`Almost ready!
@ -276,24 +275,24 @@ func handleServiceUninstallCommand(s service.Service) {
// as it will remove the symlink
_, err := runInitdCommand("disable")
if err != nil {
log.Fatal(err)
log.Fatalf("service: running init disable: %s", err)
}
}
if err := svcAction(s, "uninstall"); err != nil {
log.Fatal(err)
log.Fatalf("service: executing action %q: %s", "uninstall", err)
}
if runtime.GOOS == "darwin" {
// Remove log files on cleanup and log errors.
err := os.Remove(launchdStdoutPath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Printf("removing stdout file: %s", err)
log.Info("service: warning: removing stdout file: %s", err)
}
err = os.Remove(launchdStderrPath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Printf("removing stderr file: %s", err)
log.Info("service: warning: removing stderr file: %s", err)
}
}
}