From a589360f7d1e030e50855e21a69cf29108548dcd Mon Sep 17 00:00:00 2001 From: Igor Dolzhikov Date: Sat, 31 Jan 2015 12:35:30 +0700 Subject: [PATCH 1/4] extend example with logger --- example/myservice.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/example/myservice.go b/example/myservice.go index cc371bd..6b58241 100644 --- a/example/myservice.go +++ b/example/myservice.go @@ -22,6 +22,8 @@ const ( port = ":9977" ) +var stdlog, errlog *log.Logger + // Service has embedded daemon type Service struct { daemon.Daemon @@ -76,8 +78,8 @@ func (service *Service) Manage() (string, error) { case conn := <-listen: go handleClient(conn) case killSignal := <-interrupt: - log.Println("Got signal:", killSignal) - log.Println("Stoping listening on ", listener.Addr()) + stdlog.Println("Got signal:", killSignal) + stdlog.Println("Stoping listening on ", listener.Addr()) listener.Close() if killSignal == os.Interrupt { return "Daemon was interruped by system signal", nil @@ -112,16 +114,21 @@ func handleClient(client net.Conn) { } } +func init() { + stdlog = log.New(os.Stdout, "", log.Ldate|log.Ltime) + errlog = log.New(os.Stderr, "", log.Ldate|log.Ltime) +} + func main() { srv, err := daemon.New(name, description) if err != nil { - fmt.Println("Error: ", err) + errlog.Println("Error: ", err) os.Exit(1) } service := &Service{srv} status, err := service.Manage() if err != nil { - fmt.Println(status, "\nError: ", err) + errlog.Println(status, "\nError: ", err) os.Exit(1) } fmt.Println(status) From d8ebc7a2a98ff661eb74e0620a1afd637943cc54 Mon Sep 17 00:00:00 2001 From: Igor Dolzhikov Date: Sat, 31 Jan 2015 12:39:53 +0700 Subject: [PATCH 2/4] change comments with example --- daemon.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/daemon.go b/daemon.go index a815a6e..5dfbe04 100644 --- a/daemon.go +++ b/daemon.go @@ -12,7 +12,7 @@ In the current implementation is only supported Linux and Mac Os X daemon. Example: - //Example of the daemon with echo service + // Example of a daemon with echo service package main import ( @@ -36,6 +36,8 @@ Example: port = ":9977" ) + var stdlog, errlog *log.Logger + // Service has embedded daemon type Service struct { daemon.Daemon @@ -90,8 +92,8 @@ Example: case conn := <-listen: go handleClient(conn) case killSignal := <-interrupt: - log.Println("Got signal:", killSignal) - log.Println("Stoping listening on ", listener.Addr()) + stdlog.Println("Got signal:", killSignal) + stdlog.Println("Stoping listening on ", listener.Addr()) listener.Close() if killSignal == os.Interrupt { return "Daemon was interruped by system signal", nil @@ -126,16 +128,21 @@ Example: } } + func init() { + stdlog = log.New(os.Stdout, "", log.Ldate|log.Ltime) + errlog = log.New(os.Stderr, "", log.Ldate|log.Ltime) + } + func main() { srv, err := daemon.New(name, description) if err != nil { - fmt.Println("Error: ", err) + errlog.Println("Error: ", err) os.Exit(1) } service := &Service{srv} status, err := service.Manage() if err != nil { - fmt.Println(status, "\nError: ", err) + errlog.Println(status, "\nError: ", err) os.Exit(1) } fmt.Println(status) From bf9133d39000aa719340833cf48395566cb4ae66 Mon Sep 17 00:00:00 2001 From: Igor Dolzhikov Date: Sat, 31 Jan 2015 12:42:54 +0700 Subject: [PATCH 3/4] copyright --- daemon.go | 2 +- daemon_darwin.go | 2 +- daemon_linux.go | 2 +- daemon_linux_systemd.go | 2 +- daemon_linux_systemv.go | 2 +- daemon_windows.go | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/daemon.go b/daemon.go index 5dfbe04..58501ca 100644 --- a/daemon.go +++ b/daemon.go @@ -1,4 +1,4 @@ -// Copyright 2014 Igor Dolzhikov. All rights reserved. +// Copyright 2015 Igor Dolzhikov. All rights reserved. // Use of this source code is governed by // license that can be found in the LICENSE file. diff --git a/daemon_darwin.go b/daemon_darwin.go index cccb1c2..162d191 100644 --- a/daemon_darwin.go +++ b/daemon_darwin.go @@ -1,4 +1,4 @@ -// Copyright 2014 Igor Dolzhikov. All rights reserved. +// Copyright 2015 Igor Dolzhikov. All rights reserved. // Use of this source code is governed by // license that can be found in the LICENSE file. diff --git a/daemon_linux.go b/daemon_linux.go index e089876..710358a 100644 --- a/daemon_linux.go +++ b/daemon_linux.go @@ -1,4 +1,4 @@ -// Copyright 2014 Igor Dolzhikov. All rights reserved. +// Copyright 2015 Igor Dolzhikov. All rights reserved. // Use of this source code is governed by // license that can be found in the LICENSE file. diff --git a/daemon_linux_systemd.go b/daemon_linux_systemd.go index c8e3b36..063db81 100644 --- a/daemon_linux_systemd.go +++ b/daemon_linux_systemd.go @@ -1,4 +1,4 @@ -// Copyright 2014 Igor Dolzhikov. All rights reserved. +// Copyright 2015 Igor Dolzhikov. All rights reserved. // Use of this source code is governed by // license that can be found in the LICENSE file. diff --git a/daemon_linux_systemv.go b/daemon_linux_systemv.go index fc46126..ef6a4ed 100644 --- a/daemon_linux_systemv.go +++ b/daemon_linux_systemv.go @@ -1,4 +1,4 @@ -// Copyright 2014 Igor Dolzhikov. All rights reserved. +// Copyright 2015 Igor Dolzhikov. All rights reserved. // Use of this source code is governed by // license that can be found in the LICENSE file. diff --git a/daemon_windows.go b/daemon_windows.go index 37d06f2..0567f19 100644 --- a/daemon_windows.go +++ b/daemon_windows.go @@ -1,4 +1,4 @@ -// Copyright 2014 Igor Dolzhikov. All rights reserved. +// Copyright 2015 Igor Dolzhikov. All rights reserved. // Use of this source code is governed by // license that can be found in the LICENSE file. From 2e320352addf9fac03b1847d2b3e09ba1c81f9e1 Mon Sep 17 00:00:00 2001 From: Igor Dolzhikov Date: Sat, 31 Jan 2015 12:44:55 +0700 Subject: [PATCH 4/4] read me example changes --- README.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9a9cfb2..1bf4159 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ func main() { Real example: ```go -// Example of the daemon with echo service +// Example of a daemon with echo service package main import ( @@ -57,6 +57,8 @@ const ( port = ":9977" ) +var stdlog, errlog *log.Logger + // Service has embedded daemon type Service struct { daemon.Daemon @@ -111,8 +113,8 @@ func (service *Service) Manage() (string, error) { case conn := <-listen: go handleClient(conn) case killSignal := <-interrupt: - log.Println("Got signal:", killSignal) - log.Println("Stoping listening on ", listener.Addr()) + stdlog.Println("Got signal:", killSignal) + stdlog.Println("Stoping listening on ", listener.Addr()) listener.Close() if killSignal == os.Interrupt { return "Daemon was interruped by system signal", nil @@ -147,20 +149,24 @@ func handleClient(client net.Conn) { } } +func init() { + stdlog = log.New(os.Stdout, "", log.Ldate|log.Ltime) + errlog = log.New(os.Stderr, "", log.Ldate|log.Ltime) +} + func main() { srv, err := daemon.New(name, description) if err != nil { - fmt.Println("Error: ", err) + errlog.Println("Error: ", err) os.Exit(1) } service := &Service{srv} status, err := service.Manage() if err != nil { - fmt.Println(status, "\nError: ", err) + errlog.Println(status, "\nError: ", err) os.Exit(1) } fmt.Println(status) - } ```