@@ -20,6 +20,7 @@ import (
2020 "io"
2121 "log"
2222 "os"
23+ "os/signal"
2324
2425 flags "github.com/jessevdk/go-flags"
2526
@@ -52,14 +53,40 @@ func main() {
5253 if err != nil {
5354 log .Fatalf ("Failed to create client: %v" , err )
5455 }
56+ errc := make (chan error )
57+ client .OnError = func (err error ) { errc <- err }
5558
5659 // Selects the log to write to.
5760 logger := client .Logger (opts .LogName )
5861
5962 // Read from Stdin and log it to Stdout and Stackdriver
63+ lines := make (chan string )
6064 s := bufio .NewScanner (io .TeeReader (os .Stdin , os .Stdout ))
61- for s .Scan () {
62- logger .Log (logging.Entry {Payload : s .Text ()})
65+ go func () {
66+ defer close (lines )
67+ for s .Scan () {
68+ lines <- s .Text ()
69+ }
70+ }()
71+
72+ signals := make (chan os.Signal )
73+ signal .Notify (signals , os .Interrupt )
74+
75+ mainLoop:
76+ for {
77+ select {
78+ case line , ok := <- lines :
79+ if ! ok {
80+ break mainLoop
81+ }
82+ logger .Log (logging.Entry {Payload : line })
83+ case err := <- errc :
84+ log .Printf ("error received: %v" , err )
85+ break mainLoop
86+ case <- signals :
87+ fmt .Fprintln (os .Stderr , "received interrupt: exiting program" )
88+ break mainLoop
89+ }
6390 }
6491
6592 // Closes the client and flushes the buffer to the Stackdriver Logging
@@ -72,5 +99,5 @@ func main() {
7299 log .Fatalf ("Failed to scan input: %v" , err )
73100 }
74101
75- log . Println ( "Finished logging" )
102+ fmt . Fprintln ( os . Stderr , "Finished logging" )
76103}
0 commit comments