From 62afebf5fac4f0ddd17f7de79c3612e0eb5fa951 Mon Sep 17 00:00:00 2001 From: Tevin Zhang Date: Tue, 12 Feb 2019 16:00:33 +0800 Subject: [PATCH] Add -v for verbose logging --- app/tcp-checker/tcp-checker.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/tcp-checker/tcp-checker.go b/app/tcp-checker/tcp-checker.go index 51c9614..526ff68 100644 --- a/app/tcp-checker/tcp-checker.go +++ b/app/tcp-checker/tcp-checker.go @@ -3,6 +3,7 @@ package main import ( "context" "flag" + "fmt" "log" "net" "os" @@ -61,6 +62,7 @@ type Config struct { Timeout time.Duration Requests int Concurrency int + Verbose bool } func parseConfig() *Config { @@ -71,6 +73,7 @@ func parseConfig() *Config { flag.StringVar(&conf.Addr, "a", "google.com:80", "TCP address to test") flag.IntVar(&conf.Requests, "n", 1, "Number of requests to perform") flag.IntVar(&conf.Concurrency, "c", 1, "Number of checks to perform simultaneously") + flag.BoolVar(&conf.Verbose, "v", false, "Print more logs e.g. error detail") // Parse flags flag.Parse() if _, err := net.ResolveTCPAddr("tcp", conf.Addr); err != nil { @@ -142,6 +145,9 @@ func (cc *ConcurrentChecker) doCheck() { case nil: cc.counter.Inc(CSucceed) default: + if cc.conf.Verbose { + fmt.Println(err) + } if _, ok := err.(*tcp.ErrConnect); ok { cc.counter.Inc(CErrConnect) } else { @@ -209,6 +215,9 @@ Concurrency: %d`, conf.Addr, conf.Timeout, conf.Requests, conf.Concurrency) } duration := time.Now().Sub(startedAt) + if conf.Verbose { + log.Println("Canceling checking loop") + } cancel() log.Println("")