/
log.go
60 lines (46 loc) · 994 Bytes
/
log.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
// Copyright (c) WithSecure Corporation
// https://foundry.withsecure.com
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
package util
import (
"bytes"
"os"
"golang.org/x/term"
)
var secureOutput bytes.Buffer
var nonSecureOutput bytes.Buffer
const outputLimit = 1024
const flushChr = 0x0a // \n
func BufferedStdoutLog(c byte, secure bool) {
var buf *bytes.Buffer
if secure {
buf = &secureOutput
} else {
buf = &nonSecureOutput
}
buf.WriteByte(c)
if c == flushChr || buf.Len() > outputLimit {
os.Stdout.Write(buf.Bytes())
buf.Reset()
}
}
func BufferedTermLog(c byte, secure bool, t *term.Terminal) {
var buf *bytes.Buffer
var color []byte
if secure {
buf = &secureOutput
color = t.Escape.Green
} else {
buf = &nonSecureOutput
color = t.Escape.Red
}
buf.WriteByte(c)
if c == flushChr || buf.Len() > outputLimit {
t.Write(color)
t.Write(buf.Bytes())
t.Write(t.Escape.Reset)
buf.Reset()
}
}