forked from name5566/leaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaf.go
61 lines (52 loc) · 1.04 KB
/
leaf.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
58
59
60
61
package leaf
import (
"fmt"
"github.com/name5566/leaf/conf"
"github.com/name5566/leaf/log"
"github.com/name5566/leaf/module"
"os"
"os/signal"
"path"
"runtime/pprof"
"time"
)
func Run(mods ...module.Module) {
// logger
if conf.LogLevel != "" {
logger, err := log.New(conf.LogLevel, conf.LogPath)
if err != nil {
panic(err)
}
log.Export(logger)
defer logger.Close()
}
log.Release("Leaf starting up")
// profile
if conf.EnableProfiling {
now := time.Now()
filename := fmt.Sprintf("%d%02d%02d_%02d_%02d_%02d.prof",
now.Year(),
now.Month(),
now.Day(),
now.Hour(),
now.Minute(),
now.Second())
f, err := os.Create(path.Join(conf.ProfilePath, filename))
if err != nil {
log.Fatal("%v", err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// module
for i := 0; i < len(mods); i++ {
module.Register(mods[i])
}
module.Init()
// close
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
sig := <-c
log.Release("Leaf closing down (signal: %v)", sig)
module.Destroy()
}