Skip to content

Commit 8512c9f

Browse files
author
Steve Sanders
committed
Broke some windows builds.
Bring back doFmtVerbLevelColor
1 parent f2de3fa commit 8512c9f

File tree

4 files changed

+84
-63
lines changed

4 files changed

+84
-63
lines changed

examples/example.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func main() {
2727
// For demo purposes, create two backend for os.Stderr.
2828
backend1 := logging.NewLogBackend(os.Stderr, "", 0)
2929
backend2 := logging.NewLogBackend(os.Stderr, "", 0)
30-
30+
backend1.Color = true
31+
backend2.Color = true
3132
// For messages written to backend2 we want to add some additional
3233
// information to the output, including the used log level and the name of
3334
// the function.

log.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2013, Örjan Persson. 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 logging
6+
7+
import (
8+
"fmt"
9+
"io"
10+
)
11+
12+
// TODO initialize here
13+
var colors []string
14+
var boldcolors []string
15+
16+
type color int
17+
18+
const (
19+
colorBlack = iota + 30
20+
colorRed
21+
colorGreen
22+
colorYellow
23+
colorBlue
24+
colorMagenta
25+
colorCyan
26+
colorWhite
27+
)
28+
29+
func init_colors() {
30+
colors = []string{
31+
CRITICAL: colorSeq(colorMagenta),
32+
ERROR: colorSeq(colorRed),
33+
WARNING: colorSeq(colorYellow),
34+
NOTICE: colorSeq(colorGreen),
35+
DEBUG: colorSeq(colorCyan),
36+
}
37+
boldcolors = []string{
38+
CRITICAL: colorSeqBold(colorMagenta),
39+
ERROR: colorSeqBold(colorRed),
40+
WARNING: colorSeqBold(colorYellow),
41+
NOTICE: colorSeqBold(colorGreen),
42+
DEBUG: colorSeqBold(colorCyan),
43+
}
44+
}
45+
46+
func colorSeq(color color) string {
47+
return fmt.Sprintf("\033[%dm", int(color))
48+
}
49+
50+
func colorSeqBold(color color) string {
51+
return fmt.Sprintf("\033[%d;1m", int(color))
52+
}
53+
54+
func doFmtVerbLevelColor(layout string, level Level, output io.Writer) {
55+
if layout == "bold" {
56+
output.Write([]byte(boldcolors[level]))
57+
} else if layout == "reset" {
58+
output.Write([]byte("\033[0m"))
59+
} else {
60+
output.Write([]byte(colors[level]))
61+
}
62+
}
63+

log_nix.go

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,10 @@ package logging
88

99
import (
1010
"bytes"
11-
"fmt"
1211
"io"
1312
"log"
1413
)
1514

16-
// TODO initialize here
17-
var colors []string
18-
var boldcolors []string
19-
20-
type color int
21-
22-
const (
23-
colorBlack = iota + 30
24-
colorRed
25-
colorGreen
26-
colorYellow
27-
colorBlue
28-
colorMagenta
29-
colorCyan
30-
colorWhite
31-
)
32-
3315
// LogBackend utilizes the standard log module.
3416
type LogBackend struct {
3517
Logger *log.Logger
@@ -56,37 +38,7 @@ func (b *LogBackend) Log(level Level, calldepth int, rec *Record) error {
5638
return b.Logger.Output(calldepth+2, rec.Formatted(calldepth+1))
5739
}
5840

59-
func colorSeq(color color) string {
60-
return fmt.Sprintf("\033[%dm", int(color))
61-
}
62-
63-
func colorSeqBold(color color) string {
64-
return fmt.Sprintf("\033[%d;1m", int(color))
65-
}
6641

6742
func init() {
68-
colors = []string{
69-
CRITICAL: colorSeq(colorMagenta),
70-
ERROR: colorSeq(colorRed),
71-
WARNING: colorSeq(colorYellow),
72-
NOTICE: colorSeq(colorGreen),
73-
DEBUG: colorSeq(colorCyan),
74-
}
75-
boldcolors = []string{
76-
CRITICAL: colorSeqBold(colorMagenta),
77-
ERROR: colorSeqBold(colorRed),
78-
WARNING: colorSeqBold(colorYellow),
79-
NOTICE: colorSeqBold(colorGreen),
80-
DEBUG: colorSeqBold(colorCyan),
81-
}
82-
}
83-
84-
func doFmtVerbLevelColor(layout string, level Level, output io.Writer) {
85-
if layout == "bold" {
86-
output.Write([]byte(boldcolors[level]))
87-
} else if layout == "reset" {
88-
output.Write([]byte("\033[0m"))
89-
} else {
90-
output.Write([]byte(colors[level]))
91-
}
92-
}
43+
init_colors()
44+
}

log_windows.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@ import (
99
"bytes"
1010
"io"
1111
"log"
12-
"os"
1312
"syscall"
1413
)
1514

15+
type FileDescriptorGetter interface {
16+
Fd() uintptr
17+
}
18+
1619
var (
1720
kernel32DLL = syscall.NewLazyDLL("kernel32.dll")
1821
setConsoleTextAttributeProc = kernel32DLL.NewProc("SetConsoleTextAttribute")
1922
)
2023

2124
// TODO initialize here
22-
var colors []WORD
23-
var boldcolors []WORD
25+
var win_colors []WORD
26+
var win_boldcolors []WORD
27+
2428

25-
type color int
2629
type WORD uint16
2730

2831
const (
@@ -51,14 +54,18 @@ type LogBackend struct {
5154
}
5255

5356
// NewLogBackend creates a new LogBackend.
54-
func NewLogBackend(out *os.File, prefix string, flag int) *LogBackend {
55-
return &LogBackend{Logger: log.New(out, prefix, flag), Handle: out.Fd()}
57+
func NewLogBackend(out io.Writer, prefix string, flag int) *LogBackend {
58+
var handle uintptr
59+
if fdg, ok := interface{}(out).(FileDescriptorGetter); ok {
60+
handle = fdg.Fd()
61+
}
62+
return &LogBackend{Logger: log.New(out, prefix, flag), Handle: handle}
5663
}
5764

5865
func (b *LogBackend) Log(level Level, calldepth int, rec *Record) error {
5966
if b.Color {
6067
buf := &bytes.Buffer{}
61-
setConsoleTextAttribute(b.Handle, colors[level])
68+
setConsoleTextAttribute(b.Handle, win_colors[level])
6269
buf.Write([]byte(rec.Formatted(calldepth + 1)))
6370
// For some reason, the Go logger arbitrarily decided "2" was the correct
6471
// call depth...
@@ -70,15 +77,16 @@ func (b *LogBackend) Log(level Level, calldepth int, rec *Record) error {
7077
}
7178

7279
func init() {
73-
colors = []WORD{
80+
init_colors()
81+
win_colors = []WORD{
7482
INFO: fgWhite,
7583
CRITICAL: fgMagenta,
7684
ERROR: fgRed,
7785
WARNING: fgYellow,
7886
NOTICE: fgGreen,
7987
DEBUG: fgCyan,
8088
}
81-
boldcolors = []WORD{
89+
win_boldcolors = []WORD{
8290
INFO: fgWhite | fgIntensity,
8391
CRITICAL: fgMagenta | fgIntensity,
8492
ERROR: fgRed | fgIntensity,
@@ -115,6 +123,3 @@ func checkError(r1, r2 uintptr, err error) error {
115123
// Calling use(p) ensures that p is kept live until that point.
116124
func use(p interface{}) {}
117125

118-
func doFmtVerbLevelColor(layout string, level Level, output io.Writer) {
119-
// no-op for now. We need the file descriptor to do colors.
120-
}

0 commit comments

Comments
 (0)