-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
57 lines (50 loc) · 1.28 KB
/
main.go
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
package main
import (
"flag"
"log"
"os"
"runtime/pprof"
)
var trace *log.Logger
func main() {
var cf, sf bool
var haddr, raddr string
var fname, pname string
cmdline := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
cmdline.Usage = func() {
log.Printf("usage: %s (-c|-s) [-r [host:]port] [-h [host:]port] [-l logfile]\n", os.Args[0])
}
cmdline.BoolVar(&cf, "c", false, "client mode")
cmdline.BoolVar(&sf, "s", false, "server mode")
cmdline.StringVar(&raddr, "r", ":8001", "RPC address")
cmdline.StringVar(&haddr, "h", ":8080", "Admin WebUI")
cmdline.StringVar(&fname, "l", "/tmp/tcpmeter.log", "Log file name")
cmdline.StringVar(&pname, "p", "", "CPU profile file")
cmdline.Parse(os.Args[1:])
if pname != "" {
f, err := os.Create(pname)
if err != nil {
log.Fatal(err)
}
if err = pprof.StartCPUProfile(f); err != nil {
log.Fatal(err)
}
defer pprof.StopCPUProfile()
}
if cf == sf {
cmdline.Usage()
log.Fatalln("either -s or -c must be specified")
}
logfile, err := os.OpenFile(fname, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0660)
if err != nil {
log.Fatal("OpenFile failed", err)
}
defer logfile.Close()
trace = log.New(logfile, "", log.LstdFlags)
log.SetFlags(log.Flags() | log.Llongfile)
if cf {
ClientMain(haddr)
} else {
TCPServer(raddr)
}
}