diff --git a/go.mod b/go.mod index de5847c..642c86b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module endlessh-go go 1.17 + +require github.com/golang/glog v1.0.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6b45e65 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ= +github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4= diff --git a/main.go b/main.go index 623f58f..8f584c4 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,8 @@ import ( "os" "sync/atomic" "time" + + "github.com/golang/glog" ) var ( @@ -39,7 +41,7 @@ type client struct { func NewClient(conn net.Conn, interval time.Duration, maxClient int64) *client { atomic.AddInt64(&numCurrentClients, 1) atomic.AddUint64(&numTotalClients, 1) - fmt.Printf("%v ACCEPT host=%v n=%v/%v\n", time.Now(), conn.RemoteAddr(), numCurrentClients, maxClient) + glog.V(1).Infof("ACCEPT host=%v n=%v/%v\n", conn.RemoteAddr(), numCurrentClients, maxClient) return &client{ conn: conn, next: time.Now().Add(interval), @@ -61,7 +63,7 @@ func (c *client) Send(bannerMaxLength int64) error { func (c *client) Close() { atomic.AddInt64(&numCurrentClients, -1) - fmt.Printf("%v CLOSE host=%v time=%v bytes=%v\n", time.Now(), c.conn.RemoteAddr(), time.Now().Sub(c.start), c.bytes_sent) + glog.V(1).Infof("CLOSE host=%v time=%v bytes=%v\n", c.conn.RemoteAddr(), time.Now().Sub(c.start), c.bytes_sent) c.conn.Close() } @@ -83,12 +85,12 @@ func main() { // Listen for incoming connections. l, err := net.Listen(*connType, *connHost+":"+*connPort) if err != nil { - fmt.Println("Error listening:", err.Error()) + glog.Errorf("Error listening: %v", err) os.Exit(1) } // Close the listener when the application closes. defer l.Close() - fmt.Println("Listening on " + *connHost + ":" + *connPort) + glog.Infof("Listening on %v:%v", *connHost, *connPort) clients := make(chan *client, *maxClients) go func(clients chan *client, interval time.Duration, bannerMaxLength int64) { @@ -113,7 +115,7 @@ func main() { // Listen for an incoming connection. conn, err := l.Accept() if err != nil { - fmt.Println("Error accepting: ", err.Error()) + glog.Errorf("Error accepting: %v", err) os.Exit(1) } // Handle connections in a new goroutine.