forked from digisan/logkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setting.go
130 lines (119 loc) · 2.58 KB
/
setting.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
package logkit
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync/atomic"
"time"
. "github.com/digisan/go-generics"
fd "github.com/digisan/gotk/file-dir"
)
// WarnDetail :
func WarnDetail(enable bool) {
warnDetail = enable
}
// Log2C :
func Log2C(enable bool) {
log2C = enable
}
// Log2F :
func Log2F(enable, new bool, logFile string) (string, error) {
if enable {
if new {
if _, err := remove(logFile); err != nil {
return "", err
}
}
return setLog(logFile), nil
}
return "", resetLog()
}
func remove(logFile string) (string, error) {
dir := filepath.Dir(logFile)
logFile = strings.TrimSuffix(filepath.Base(logFile), ".log")
des, err := os.ReadDir(dir)
if err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return "", err
}
} else {
return "", err
}
}
for _, de := range des {
if strings.Contains(de.Name(), logFile+"@") {
fPath := filepath.Join(dir, de.Name())
if err := os.RemoveAll(fPath); err != nil {
return "", err
}
return fPath, nil
}
}
return "", nil
}
// FilePerm :
const FilePerm = 0666
// setLog :
func setLog(logFile string) string {
zone, offset := time.Now().Zone()
if sHasSuffix(logFile, ".log") {
logFile = logFile[:len(logFile)-4]
}
logFile += fSf("@%s%s%4.1f.log", zone, IF(offset < 0, "-", "+"), float32(offset/3600.0))
if absPath, err := filepath.Abs(logFile); err == nil {
fd.MustAppendFile(absPath, nil, false)
if f, err := os.OpenFile(absPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, FilePerm); err == nil {
mPathFile[absPath] = f
log.SetFlags(log.LstdFlags) // log.SetFlags(log.LstdFlags | log.LUTC)
log.SetOutput(f)
log2F = true
}
}
return logFile
}
// resetLog : call once at exit
func resetLog() error {
for logPath, f := range mPathFile {
// delete empty error log
fi, err := f.Stat()
if err != nil {
return err
}
if fi.Size() == 0 {
if err := os.Remove(logPath); err != nil {
return err
}
}
// close
f.Close()
}
mPathFile = make(map[string]*os.File)
log.SetOutput(os.Stdout)
log2F = false
return nil
}
func Fn4GrpIdxLogF(group string, start int, ll logCategory, warnOnErr bool) func(v ...any) {
index := int64(start - 1)
return func(v ...any) {
prefix := group
if start >= 0 {
prefix = fmt.Sprintf("%s %06d", group, atomic.AddInt64(&index, 1))
}
fn := Log
switch ll {
case DEBUG:
fn = DebugP1
case WARN:
fn = WarnP1 // must warn even if no error
if warnOnErr {
fn = WarnP1OnErr
}
case FAIL:
fn = FailP1OnErr
}
fn("%s - "+v[0].(string), append([]any{prefix}, v[1:]...)...)
}
}