Skip to content

Commit d735e57

Browse files
authored
Merge pull request #3 from campoy/justforfunc
applying justforfunc changes
2 parents 87a28d6 + 32b0e02 commit d735e57

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

logpipe.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
"bufio"
1919
"fmt"
2020
"io"
21-
"log"
2221
"os"
2322
"os/signal"
23+
"time"
2424

2525
flags "github.com/jessevdk/go-flags"
2626

@@ -33,70 +33,76 @@ func main() {
3333
ProjectID string `short:"p" long:"project" description:"Google Cloud Platform Project ID" required:"true"`
3434
LogName string `short:"l" long:"logname" description:"The name of the log to write to" default:"default"`
3535
}
36-
if _, err := flags.Parse(&opts); err != nil {
36+
_, err := flags.Parse(&opts)
37+
if err != nil {
3738
os.Exit(2)
3839
}
3940

4041
// Check if Standard In is coming from a pipe
4142
fi, err := os.Stdin.Stat()
4243
if err != nil {
43-
log.Fatalf("Could not stat standard input: %v", err)
44+
errorf("Could not stat standard input: %v", err)
4445
}
4546
if fi.Mode()&os.ModeNamedPipe == 0 {
46-
fmt.Fprintln(os.Stderr, "Nothing is piped in so there is nothing to log!")
47-
os.Exit(2)
47+
errorf("Nothing is piped in so there is nothing to log!")
4848
}
4949

5050
// Creates a client.
5151
ctx := context.Background()
5252
client, err := logging.NewClient(ctx, opts.ProjectID)
5353
if err != nil {
54-
log.Fatalf("Failed to create client: %v", err)
54+
errorf("Failed to create client: %v", err)
5555
}
5656
errc := make(chan error)
5757
client.OnError = func(err error) { errc <- err }
5858

59+
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
60+
defer cancel()
61+
if err := client.Ping(ctx); err != nil {
62+
errorf("Failed to ping logging service: %v", err)
63+
}
64+
5965
// Selects the log to write to.
6066
logger := client.Logger(opts.LogName)
6167

62-
// Read from Stdin and log it to Stdout and Stackdriver
6368
lines := make(chan string)
6469
go func() {
70+
defer close(lines)
71+
// Read from Stdin and log it to Stdout and Stackdriver
6572
s := bufio.NewScanner(io.TeeReader(os.Stdin, os.Stdout))
6673
for s.Scan() {
6774
lines <- s.Text()
6875
}
6976
if err := s.Err(); err != nil {
70-
errc <- fmt.Errorf("could not read from std in: %v", err)
77+
fmt.Fprintf(os.Stderr, "Failed to scan input: %v\n", err)
7178
}
72-
close(lines)
7379
}()
7480

7581
signals := make(chan os.Signal)
7682
signal.Notify(signals, os.Interrupt)
7783

78-
mainLoop:
84+
loop:
7985
for {
8086
select {
8187
case line, ok := <-lines:
8288
if !ok {
83-
break mainLoop
89+
break loop
8490
}
8591
logger.Log(logging.Entry{Payload: line})
86-
case err := <-errc:
87-
log.Printf("error received: %v", err)
88-
break mainLoop
89-
case <-signals:
90-
fmt.Fprintln(os.Stderr, "received interrupt: exiting program")
91-
break mainLoop
92+
case s := <-signals:
93+
fmt.Fprintf(os.Stderr, "Terminating program after receiving signal: %v\n", s)
94+
break loop
9295
}
9396
}
9497

9598
// Closes the client and flushes the buffer to the Stackdriver Logging
9699
// service.
97100
if err := client.Close(); err != nil {
98-
log.Fatalf("Failed to close client: %v", err)
101+
errorf("Failed to close client: %v", err)
99102
}
103+
}
100104

101-
fmt.Fprintln(os.Stderr, "Finished logging")
105+
func errorf(format string, args ...interface{}) {
106+
fmt.Fprintf(os.Stderr, format+"\n", args...)
107+
os.Exit(2)
102108
}

0 commit comments

Comments
 (0)