forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging_windows.go
109 lines (90 loc) · 2.31 KB
/
logging_windows.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
// +build windows
package logging
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
"syscall"
"time"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"golang.org/x/sys/windows"
)
var (
kernel32Dll *windows.LazyDLL = windows.NewLazySystemDLL("Kernel32.dll")
setConsoleMode *windows.LazyProc = kernel32Dll.NewProc("SetConsoleMode")
color_map = map[string]string{
"reset": "\033[0m",
"red": "\033[31m",
"green": "\033[32m",
"yellow": "\033[33m",
"blue": "\033[34m",
"purple": "\033[35m",
"cyan": "\033[36m",
"gray": "\033[37m",
"white": "\033[97m",
}
)
func EnableVirtualTerminalProcessing(stream syscall.Handle, enable bool) error {
const ENABLE_VIRTUAL_TERMINAL_PROCESSING uint32 = 0x4
var mode uint32
err := syscall.GetConsoleMode(syscall.Stdout, &mode)
if err != nil {
return err
}
if enable {
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING
} else {
mode &^= ENABLE_VIRTUAL_TERMINAL_PROCESSING
}
ret, _, err := setConsoleMode.Call(uintptr(stream), uintptr(mode))
if ret == 0 {
return err
}
return nil
}
type Formatter struct {
stderr_map lfshook.WriterMap
}
func (self *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
b := &bytes.Buffer{}
levelText := strings.ToUpper(entry.Level.String())
fmt.Fprintf(b, "[%s] %v %s ", levelText, entry.Time.Format(time.RFC3339),
strings.TrimRight(entry.Message, "\r\n"))
if len(entry.Data) > 0 {
serialized, _ := json.Marshal(entry.Data)
fmt.Fprintf(b, "%s", serialized)
}
// Only print the result to the console, if there is an stderr
// map to it.
_, pres := self.stderr_map[entry.Level]
if pres {
if NoColor {
fmt.Fprintln(os.Stdout, clearTag(b.String()))
} else {
EnableVirtualTerminalProcessing(syscall.Stdout, true)
fmt.Fprintln(os.Stdout, replaceTagWithCode(b.String()))
EnableVirtualTerminalProcessing(syscall.Stdout, false)
}
}
return nil, nil
}
func replaceTagWithCode(message string) string {
if NoColor {
return clearTag(message)
}
result := tag_regex.ReplaceAllStringFunc(message, func(hit string) string {
matches := tag_regex.FindStringSubmatch(hit)
if len(matches) > 1 {
code, pres := color_map[matches[1]]
if pres {
return code
}
}
return hit
})
reset := color_map["reset"]
return closing_tag_regex.ReplaceAllString(result, reset) + reset
}