-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcommon.go
63 lines (52 loc) · 1.18 KB
/
common.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
62
package main
import (
"github.com/wxnacy/wgo/logger"
"log"
"fmt"
"os"
// "time"
"strings"
)
var wlog *logger.Logger // 日志
var tempdir string // 临时目录
var tempCmpltFile string
func tempDir() string {
if tempdir == "" {
tmp := os.TempDir()
if !strings.HasSuffix(tmp, "/") {
tmp += "/"
}
// tempdir = fmt.Sprintf("%s%s-%d/", tmp, "wgo", time.Now().Unix())
tempdir = fmt.Sprintf("%s%s-%d/", tmp, "wgo", 0)
}
return tempdir
}
func destroyTempDir() {
err := os.RemoveAll(tempDir())
handlerErr(err)
}
func tempCompleteFile() string {
if tempCmpltFile == "" {
tempCmpltFile = fmt.Sprintf("%swgo_complete.go", tempDir())
}
return tempCmpltFile
}
func Logger() *logger.Logger {
if wlog == nil {
wlog = logger.NewLogger()
}
return wlog
}
func initLogger() {
initTempDir()
h, err := logger.NewRotatingFileHandler(tempDir() + "wgo.log")
handlerErr(err)
Logger().AddHandler(h)
Logger().SetLevel(logger.LevelError)
}
func handlerErr(err error) {
if err != nil {
Logger().Error(err)
log.Fatal(err)
}
}