Skip to content

Commit e234b4c

Browse files
committed
improve log system
1 parent 33ca783 commit e234b4c

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/file/file.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func SaveFile(filename string, content string) error {
6161
}
6262

6363
func AddContent2File(filename string, content string) error {
64-
fmt.Println("Start Add Content 2 File")
64+
//fmt.Println("Start Add Content 2 File")
6565
fileObj, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
6666
if err != nil {
6767
return err

src/gtime/gtime.go

+12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const (
88
BASIC_NANO = 3
99
BASIC_FULL = 4
1010
)
11+
const (
12+
STYLE1 = 1
13+
)
1114

1215
func GetCurTime(iType int8) string {
1316
switch iType {
@@ -24,3 +27,12 @@ func GetCurTime(iType int8) string {
2427
}
2528
return time.Now().Format("2006-1-2 15:04:05")
2629
}
30+
31+
func GetCurDate(iType int8) string {
32+
switch iType {
33+
case STYLE1:
34+
return time.Now().Format("20060102")
35+
default:
36+
return time.Now().Format("20060102")
37+
}
38+
}

src/log/log.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111
var (
1212
bEnableLog bool = false
1313
logchan chan string
14-
bShowInConsole bool = false
15-
arrLogType = [...]string{"normal", "warning", "error"}
14+
bShowInConsole bool = false
15+
arrLogType = [...]string{"normal", "warning", "error"}
16+
mapLogPath map[int]string = make(map[int]string)
1617
logPath string
1718
)
1819

@@ -23,15 +24,20 @@ func InitLog() {
2324
bShowInConsole = config.GConfig.LogCfg.ShowInConsole
2425
fmt.Println("Log path from config file:" + config.GConfig.LogCfg.LogPath)
2526
logPath = config.GConfig.LogCfg.LogPath
26-
//check if the logPath exists
27-
if err := file.FolderExists(logPath); err != nil {
28-
if err = file.CreateFolder(logPath); err != nil {
29-
panic(err)
30-
}
27+
for key, val := range arrLogType {
28+
checkAndCreateFolder(logPath+"/"+val+"/", key)
3129
}
3230
go Listen4Log()
3331
}
3432

33+
func checkAndCreateFolder(path string, pos int) {
34+
if err := file.FolderExists(path); err != nil {
35+
if err = file.CreateFolder(path); err != nil {
36+
panic(err)
37+
}
38+
}
39+
mapLogPath[pos] = path
40+
}
3541
func wlog(info string) {
3642
logchan <- info
3743
}
@@ -43,16 +49,17 @@ func Listen4Log() {
4349
select {
4450
case recvStr = <-logchan:
4551
logType := recvStr[0:1]
52+
rawContent := recvStr[1:]
4653
if iType, err = strconv.Atoi(logType); err != nil {
4754
iType = 0
4855
fmt.Printf("error logType in Listen4Log [%s]\n", logType)
4956
}
5057
if bShowInConsole {
5158
fmt.Printf("[%s][%s] %s\n",
52-
gtime.GetCurTime(gtime.BASIC_MILL), arrLogType[iType], recvStr[1:])
59+
gtime.GetCurTime(gtime.BASIC_MILL), arrLogType[iType], rawContent)
5360
}
54-
//file.SaveFile(logPath+"firstFile", "Hello World")
55-
file.AddContent2File(logPath+"firstFile", "Hello World")
61+
filePath := mapLogPath[iType] + gtime.GetCurDate(gtime.STYLE1)
62+
file.AddContent2File(filePath, rawContent+"\n")
5663
}
5764
}
5865

0 commit comments

Comments
 (0)