-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroller.go
More file actions
82 lines (71 loc) · 1.54 KB
/
roller.go
File metadata and controls
82 lines (71 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"flag"
"log"
"net"
"net/http"
_ "net/http/pprof"
"os"
"gopkg.in/natefinch/lumberjack.v2"
)
var (
channels = NewBroadcastChannels()
)
func ConfigLog(fileName string, maxSizeinMB int, maxBackup int, maxAge int) {
log.SetOutput(&lumberjack.Logger{
Filename: fileName,
MaxSize: maxSizeinMB, // megabytes
MaxBackups: maxBackup,
MaxAge: maxAge, //days
})
}
func IsDirectory(path string) bool {
if fileInfo, err := os.Stat(path); err != nil {
return false
} else {
return fileInfo.IsDir()
}
}
var (
debugAddr string
)
func main() {
logDir := flag.String("d", "stdout", "log dir")
flag.StringVar(&debugAddr, "x", "", "enable port at address")
bindAddress := flag.String("l", "127.0.0.1:6380", "Listen address")
flag.Parse()
if *logDir != "stdout" {
if IsDirectory(*logDir) {
ConfigLog(*logDir+"/applog.log", 20, 20, 30)
} else {
if err := os.Mkdir(*logDir, 0755); err == nil {
ConfigLog(*logDir+"/applog.log", 20, 20, 30)
}
}
}
listener, err := net.Listen("tcp", *bindAddress)
if err != nil {
panic(err)
}
if debugAddr != "" {
go func() {
if l, err := net.Listen("tcp", debugAddr); err != nil {
log.Println(err)
return
} else {
//_,p,_ := net.SplitHostPort(l.Addr().String())
//glog.Infoln("http://localhost:"+p+"/debug/pprof/goroutine?debug=2")
http.Serve(l, nil)
}
}()
}
for {
conn, err := listener.Accept()
if err != nil {
log.Println("Error on accept: ", err)
continue
}
client := NewClient(conn, channels)
go client.Run()
}
}