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) - } ``` diff --git a/daemon.go b/daemon.go index a815a6e..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. @@ -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) 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. 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)