Use glog for logging

This commit is contained in:
Shizun Ge
2021-10-12 21:31:45 -07:00
parent fb42a7f5de
commit b4c680e6c3
3 changed files with 11 additions and 5 deletions

2
go.mod
View File

@@ -1,3 +1,5 @@
module endlessh-go
go 1.17
require github.com/golang/glog v1.0.0 // indirect

2
go.sum Normal file
View File

@@ -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=

12
main.go
View File

@@ -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.