Skip to content

Commit abd3526

Browse files
committed
cmd/vulnreport: add color to logs (and refactor)
Refactor logging and add colors. Colors are on by default but can be turned off with `vulnreport -color=false [subcommand]...`, or by setting the environment variable `NO_COLOR`. Change-Id: Iaea30dfeb045b2b690683f257cf49676c785951a Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/562555 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 0c44de6 commit abd3526

File tree

3 files changed

+146
-21
lines changed

3 files changed

+146
-21
lines changed

cmd/vulnreport/color/color.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package color
6+
7+
const (
8+
escape = "\033["
9+
end = "m"
10+
11+
// Reset all attributes and colors to their defaults.
12+
Reset = escape + "0" + end
13+
14+
// Attribute codes
15+
Bold = escape + "1" + end
16+
Faint = escape + "2" + end
17+
Underline = escape + "4" + end
18+
Blink = escape + "5" + end
19+
20+
// Foreground colors
21+
Black = escape + "30" + end
22+
Red = escape + "31" + end
23+
Green = escape + "32" + end
24+
Yellow = escape + "33" + end
25+
Blue = escape + "34" + end
26+
Magenta = escape + "35" + end
27+
Cyan = escape + "36" + end
28+
White = escape + "37" + end
29+
30+
// Background colors
31+
BlackBG = escape + "40" + end
32+
RedBG = escape + "41" + end
33+
GreenBG = escape + "42" + end
34+
YellowBG = escape + "43" + end
35+
BlueBG = escape + "44" + end
36+
MagentaBG = escape + "45" + end
37+
CyanBG = escape + "46" + end
38+
WhiteBG = escape + "47" + end
39+
40+
// High intensity foreground colors
41+
BlackHi = escape + "90" + end
42+
RedHi = escape + "91" + end
43+
GreenHi = escape + "92" + end
44+
YellowHi = escape + "93" + end
45+
BlueHi = escape + "94" + end
46+
MagentaHi = escape + "95" + end
47+
CyanHi = escape + "96" + end
48+
WhiteHi = escape + "97" + end
49+
50+
// High intensity background colors
51+
BlackHiBG = escape + "100" + end
52+
RedHiBG = escape + "101" + end
53+
GreenHiBG = escape + "102" + end
54+
YellowHiBG = escape + "103" + end
55+
BlueHiBG = escape + "104" + end
56+
MagentaHiBG = escape + "105" + end
57+
CyanHiBG = escape + "106" + end
58+
WhiteHiBG = escape + "107" + end
59+
)

cmd/vulnreport/log/log.go

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,117 @@
55
package log
66

77
import (
8+
"fmt"
89
"io"
910
"os"
1011

1112
"log"
13+
14+
"golang.org/x/vulndb/cmd/vulnreport/color"
1215
)
1316

17+
func SetQuiet() {
18+
loggers[infoLvl] = log.New(io.Discard, "", 0)
19+
}
20+
21+
func RemoveColor() {
22+
for lvl := range loggers {
23+
loggers[lvl].SetPrefix(metas[lvl].prefix)
24+
}
25+
colorize = false
26+
}
27+
28+
const (
29+
infoLvl int = iota
30+
outLvl
31+
warnLvl
32+
errLvl
33+
)
34+
35+
func defaultLoggers() []*log.Logger {
36+
ls := make([]*log.Logger, len(metas))
37+
for lvl, lm := range metas {
38+
ls[lvl] = log.New(lm.w, lm.color+lm.prefix, 0)
39+
}
40+
return ls
41+
}
42+
1443
var (
15-
infolog *log.Logger
16-
outlog *log.Logger
17-
warnlog *log.Logger
18-
errlog *log.Logger
44+
loggers = defaultLoggers()
45+
46+
// Whether to display colors in logs.
47+
colorize bool = true
48+
49+
metas = []*metadata{
50+
infoLvl: {
51+
prefix: "info: ",
52+
color: color.Faint,
53+
w: os.Stderr,
54+
},
55+
outLvl: {
56+
prefix: "",
57+
color: color.Reset,
58+
w: os.Stdout,
59+
},
60+
warnLvl: {
61+
prefix: "WARNING: ",
62+
color: color.YellowHi,
63+
w: os.Stderr,
64+
},
65+
errLvl: {
66+
prefix: "ERROR: ",
67+
color: color.RedHi,
68+
w: os.Stderr,
69+
},
70+
}
1971
)
2072

21-
func Init(quiet bool) {
22-
if quiet {
23-
infolog = log.New(io.Discard, "", 0)
24-
} else {
25-
infolog = log.New(os.Stderr, "info: ", 0)
73+
type metadata struct {
74+
prefix string
75+
color string
76+
w io.Writer
77+
}
78+
79+
func printf(lvl int, format string, v ...any) {
80+
println(lvl, fmt.Sprintf(format, v...))
81+
}
82+
83+
func println(lvl int, v ...any) {
84+
l := loggers[lvl]
85+
l.Println(v...)
86+
if colorize {
87+
fmt.Fprint(l.Writer(), color.Reset)
2688
}
27-
outlog = log.New(os.Stdout, "", 0)
28-
warnlog = log.New(os.Stderr, "WARNING: ", 0)
29-
errlog = log.New(os.Stderr, "ERROR: ", 0)
3089
}
3190

3291
func Infof(format string, v ...any) {
33-
infolog.Printf(format, v...)
92+
printf(infoLvl, format, v...)
3493
}
3594

3695
func Outf(format string, v ...any) {
37-
outlog.Printf(format, v...)
96+
printf(outLvl, format, v...)
3897
}
3998

4099
func Warnf(format string, v ...any) {
41-
warnlog.Printf(format, v...)
100+
printf(warnLvl, format, v...)
42101
}
43102

44103
func Errf(format string, v ...any) {
45-
errlog.Printf(format, v...)
104+
printf(errLvl, format, v...)
46105
}
47106

48107
func Info(v ...any) {
49-
infolog.Println(v...)
108+
println(infoLvl, v...)
50109
}
51110

52111
func Out(v ...any) {
53-
outlog.Println(v...)
112+
println(outLvl, v...)
54113
}
55114

56115
func Warn(v ...any) {
57-
warnlog.Println(v...)
116+
println(warnLvl, v...)
58117
}
59118

60119
func Err(v ...any) {
61-
errlog.Println(v...)
120+
println(errLvl, v...)
62121
}

cmd/vulnreport/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ var (
2222
githubToken = flag.String("ghtoken", "", "GitHub access token (default: value of VULN_GITHUB_ACCESS_TOKEN)")
2323
cpuprofile = flag.String("cpuprofile", "", "write cpuprofile to this file")
2424
quiet = flag.Bool("q", false, "quiet mode (suppress info logs)")
25+
colorize = flag.Bool("color", os.Getenv("NO_COLOR") == "", "show colors in logs")
2526
)
2627

2728
func init() {
28-
vlog.Init(*quiet)
2929
out := flag.CommandLine.Output()
3030
flag.Usage = func() {
3131
fmt.Fprintf(out, "usage: vulnreport [flags] [cmd] [args]\n\n")
@@ -67,6 +67,13 @@ func main() {
6767
log.Fatal("subcommand required")
6868
}
6969

70+
if *quiet {
71+
vlog.SetQuiet()
72+
}
73+
if !*colorize {
74+
vlog.RemoveColor()
75+
}
76+
7077
if *githubToken == "" {
7178
*githubToken = os.Getenv("VULN_GITHUB_ACCESS_TOKEN")
7279
}

0 commit comments

Comments
 (0)