-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
114 lines (94 loc) · 1.91 KB
/
main.go
File metadata and controls
114 lines (94 loc) · 1.91 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// stream_check project main.go
package main
/*
const char* build_time(void)
{
static const char* psz_build_time = "["__DATE__ " " __TIME__ "]";
return psz_build_time;
}
*/
import "C"
import (
"common/utils"
"flag"
"fmt"
"os"
"path/filepath"
"product_code/check_stream/public"
log4plus "common/log4go"
)
//版本号
var (
ver string = "1.2.11"
exeName string = ""
pidFile string = ""
buildTime = C.GoString(C.build_time())
)
type Flags struct {
Help bool
Version bool
}
func (f *Flags) Init() {
flag.BoolVar(&f.Help, "h", false, "help")
flag.BoolVar(&f.Version, "V", false, "show version")
}
func (f *Flags) Check() (needReturn bool) {
flag.Parse()
if f.Help {
flag.Usage()
needReturn = true
} else if f.Version {
verString := "Check Stream Version: " + ver + "\r\n"
verString += "compile time:" + buildTime + "\r\n"
fmt.Println(verString)
needReturn = true
}
return needReturn
}
var flags *Flags = &Flags{}
func init() {
flags.Init()
exeName = getExeName()
pidFile = public.GetCurrentDirectory() + "/" + exeName + ".pid"
}
func getExeName() string {
ret := ""
ex, err := os.Executable()
if err == nil {
ret = filepath.Base(ex)
}
return ret
}
func setLog() {
logJson := "log.json"
set := false
if bExist, _ := utils.PathExist(logJson); bExist {
if err := log4plus.SetupLogWithConf(logJson); err == nil {
set = true
}
}
if !set {
fileWriter := log4plus.NewFileWriter()
fileWriter.SetPathPattern("log/" + exeName + "-%Y%M%D.log")
log4plus.Register(fileWriter)
log4plus.SetLevel(log4plus.DEBUG)
}
}
func writePid() {
public.SaveFile(fmt.Sprintf("%d", os.Getpid()), pidFile)
}
func main() {
needReturn := flags.Check()
if needReturn {
return
}
//set log
setLog()
defer log4plus.Close()
writePid()
defer os.Remove(pidFile)
log4plus.Info("main begin version=%s", ver)
defer log4plus.Close()
server := NewCheckServer()
server.Run()
}