-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathjoint_windows.go
More file actions
60 lines (54 loc) · 1.76 KB
/
joint_windows.go
File metadata and controls
60 lines (54 loc) · 1.76 KB
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
package log
import (
"fmt"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
)
//传入要打印的信息,拼接好后返回
func joint(prefix, message string, color colorType) string {
now := time.Now().Format("2006/01/02 15:04:05")
filename, funcname, line := getpProcInfo()
s := fmt.Sprint(prefix, ": ", now, " ", filename, ":", line, ":", funcname, ": ", message)
//如果是windows系统彩色打印,需要调试kernel32.dll文件
if isColor {
winKernelOpen(color)
}
return s
}
func (l mylog) joint(prefix, message string, color colorType) string {
now := time.Now().Format("2006/01/02 15:04:05")
filename, funcname, line := getpProcInfo()
s := fmt.Sprint(prefix, ": ", now, " ", filename, ":", line, ":", funcname, ": ", message)
if l.isColor {
winKernelOpen(color)
}
return s
}
//传入颜色代码,windows系统开启彩色打印
func winKernelOpen(color colorType) {
kernel32 := syscall.NewLazyDLL("kernel32.dll")
proc := kernel32.NewProc("SetConsoleTextAttribute")
proc.Call(uintptr(syscall.Stdout), uintptr(color))
}
//windows系统关闭彩色打印
func winKernelColse() {
kernel32 := syscall.NewLazyDLL("kernel32.dll")
proc := kernel32.NewProc("SetConsoleTextAttribute")
handle, _, _ := proc.Call(uintptr(syscall.Stdout), uintptr(7))
CloseHandle := kernel32.NewProc("CloseHandle")
CloseHandle.Call(handle)
}
//获取打印日志的进程信息
func getpProcInfo() (filename, funcname string, line int) {
pc, filename, line, ok := runtime.Caller(3)
if ok {
funcname = runtime.FuncForPC(pc).Name() // main.(*MyStruct).foo
funcname = filepath.Ext(funcname) // .foo
funcname = strings.TrimPrefix(funcname, ".") // foo
filename = filepath.Base(filename) // /full/path/basename.go => basename.go
}
return
}