-
Notifications
You must be signed in to change notification settings - Fork 1
/
func.go
135 lines (119 loc) · 2.33 KB
/
func.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package QLog
import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"runtime/debug"
"strconv"
"time"
)
func GetLogTextPrefix(calldepth int, format string) string {
return GetNowTime(format) + BLANK + GetCaller(calldepth)
}
func GetNowTime(format string) (str string) {
var (
now time.Time
)
now = GetNowUnixTimeOBJ()
if format == "" {
y, m, d := now.Date()
h, i, s := now.Clock()
str = Itoa(y, 4) + DIAGONAL + Itoa(int(m), 2) + DIAGONAL + Itoa(d, 2) + BLANK + Itoa(h, 2) + COLON + Itoa(i, 2) + COLON + Itoa(s, 2)
} else {
str = now.Format(format)
}
return
}
func GetNowUnixTimeOBJ() time.Time {
return time.Now().In(TimeLocation)
}
func GetCaller(calldepth int) (str string) {
var (
file string
line int
ok bool
)
_, file, line, ok = runtime.Caller(calldepth)
if !ok {
file = "???"
line = 0
}
str = path.Base(file) + `:` + strconv.Itoa(line) + `:`
return
}
func Itoa(i int, wid int) string {
var b [20]byte
bp := len(b) - 1
for i >= 10 || wid > 1 {
wid--
q := i / 10
b[bp] = byte('0' + i - q*10)
bp--
i = q
}
b[bp] = byte('0' + i)
return string(b[bp:])
}
func mkdirlog(dir string) (e error) {
_, er := os.Stat(dir)
b := er == nil || os.IsExist(er)
if !b {
if err := os.MkdirAll(dir, 0766); err != nil {
if os.IsPermission(err) {
e = err
}
}
}
return
}
func format(v ...interface{}) (s string) {
for key, val := range v {
if key%2 == 0 {
s += fmt.Sprintf("%+v", val) + `=`
} else {
s += fmt.Sprintf("%+v", val) + " "
}
}
return
}
func strDefault(str *string, defaultStr string) {
if *str == "" {
*str = defaultStr
}
}
func intDefault(str *int, defaultInt int) {
if *str == 0 {
*str = defaultInt
}
}
func int64Default(str *int64, defaultInt64 int64) {
if *str == 0 {
*str = defaultInt64
}
}
func catchError() {
if err := recover(); err != nil {
fmt.Println(err, string(debug.Stack()))
}
}
//判断所给路径文件/文件夹是否存在
func isExists(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
//获取绝对路径
func absolutePath(dir string) (fp string) {
fp, _ = filepath.Abs(dir)
return
}
//判断所给路径是否为文件夹
func isDir(path string) bool {
s, err := os.Stat(path)
return err == nil && s.IsDir()
}
//判断所给路径是否为文件
func isFile(path string) bool {
return !isDir(path)
}