-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug.go
51 lines (42 loc) · 806 Bytes
/
debug.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
package main
import (
"flag"
"fmt"
"github.com/astaxie/beego/logs"
"log"
"net"
"net/http"
"net/http/pprof"
)
var (
debug bool
port int
)
func init() {
flag.BoolVar(&debug, "debug", false, "running pprof for golang debug.")
flag.IntVar(&port, "port", 18000, "debug pprof http server listen.")
}
func DebugFlag() bool {
return debug
}
func DebugInit() error {
flag.Parse()
if !debug {
return nil
}
listen, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Println(err.Error())
return err
}
pprofHandler := http.NewServeMux()
pprofHandler.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
server := &http.Server{Handler: pprofHandler}
go func() {
err := server.Serve(listen)
if err != nil {
logs.Error(err.Error())
}
}()
return nil
}