-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathasync_console.go
92 lines (85 loc) · 2.2 KB
/
async_console.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
// Copyright (c) 2020 HigKer
// Open Source: MIT License
// Author: SDing <deen.job@qq.com>
// Date: 2020/4/30 - 9:16 下午
package logker
import (
"github.com/fatih/color"
"runtime"
"strings"
"time"
)
// Channel Buffer Size
const (
_ = iota
// 10000 BufferSize
Qs1w = 10000 * iota
// 20000 BufferSize
Qs2w
// 30000 BufferSize
Qs3w
// 40000 BufferSize
Qs4w
// 50000 BufferSize
Qs5w
// 60000 BufferSize
Qs6w
)
// Send Pack Message To Log Queue
func (c *console) sendMsg(msg *message) {
c.asyncTask.logQueue <- msg
}
// Execute this function to start an asynchronous task
func (c *console) begin() {
//time.Sleep(5*time.Second)
switch {
case runtime.NumCPU() > 0 && runtime.NumCPU() <= 2:
go c.asyncOutPutTask()
default:
go c.asyncOutPutTask()
go c.asyncOutPutTask()
}
}
// Asynchronous Output Logging Tasks
func (c *console) asyncOutPutTask() {
for {
<-c.asyncTask.sync
select {
case msg := <-c.asyncTask.logQueue:
switch msg.level {
case DEBUG:
// blue color of log message.
// format log message output console.
color.Blue(c.parsePacket(msg))
case INFO:
color.Green(c.parsePacket(msg))
case WARNING:
color.Yellow(c.parsePacket(msg))
case ERROR:
color.Red(c.parsePacket(msg))
default:
// Log Level Type Error
// Program automatically set to debug
color.Red("-----------------------------------------------------------------")
color.Red("!!!Log Level Type Error,Program automatically set to debug!!!")
color.Red("-----------------------------------------------------------------")
c.logLevel = DEBUG
// recursion
c.sendMsg(c.pack(ERROR, "Log Level Type Error,Program automatically set to debug!!!"))
}
default:
time.Sleep(time.Second)
}
c.asyncTask.sync <- true
}
}
// ReplaceOurCustomMessageFormatIdentifier
// This function was added at 23:50:28 on April 19, 2020 in v1.1.5
func (c *console) parsePacket(msg *message) string {
str := msg.format
str = strings.Replace(str, "{level}", msg.level.toStr(), -1)
str = strings.Replace(str, "{time}", msg.logTime, -1)
str = strings.Replace(str, "{position}", msg.caller, -1)
str = strings.Replace(str, "{message}", msg.msg, -1)
return str + "\n"
}