Add -v for verbose logging

This commit is contained in:
Tevin Zhang 2019-02-12 16:00:33 +08:00
parent 2ef7e36ff8
commit 62afebf5fa
1 changed files with 9 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"flag" "flag"
"fmt"
"log" "log"
"net" "net"
"os" "os"
@ -61,6 +62,7 @@ type Config struct {
Timeout time.Duration Timeout time.Duration
Requests int Requests int
Concurrency int Concurrency int
Verbose bool
} }
func parseConfig() *Config { func parseConfig() *Config {
@ -71,6 +73,7 @@ func parseConfig() *Config {
flag.StringVar(&conf.Addr, "a", "google.com:80", "TCP address to test") 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.Requests, "n", 1, "Number of requests to perform")
flag.IntVar(&conf.Concurrency, "c", 1, "Number of checks to perform simultaneously") 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 // Parse flags
flag.Parse() flag.Parse()
if _, err := net.ResolveTCPAddr("tcp", conf.Addr); err != nil { if _, err := net.ResolveTCPAddr("tcp", conf.Addr); err != nil {
@ -142,6 +145,9 @@ func (cc *ConcurrentChecker) doCheck() {
case nil: case nil:
cc.counter.Inc(CSucceed) cc.counter.Inc(CSucceed)
default: default:
if cc.conf.Verbose {
fmt.Println(err)
}
if _, ok := err.(*tcp.ErrConnect); ok { if _, ok := err.(*tcp.ErrConnect); ok {
cc.counter.Inc(CErrConnect) cc.counter.Inc(CErrConnect)
} else { } else {
@ -209,6 +215,9 @@ Concurrency: %d`, conf.Addr, conf.Timeout, conf.Requests, conf.Concurrency)
} }
duration := time.Now().Sub(startedAt) duration := time.Now().Sub(startedAt)
if conf.Verbose {
log.Println("Canceling checking loop")
}
cancel() cancel()
log.Println("") log.Println("")