Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring back doFmtVerbLevelColor on Windows #76

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
7ed74c1
Bring back doFmtVerbLevelColor on Windows
Dec 5, 2015
02b8aca
Fix concurrent data race problem
hilyjiang Dec 7, 2015
2da2aba
Merge pull request #3 from keybase/zanderz/colors_2015.12.04
Dec 7, 2015
d7b8c00
go fmt
Dec 9, 2015
6aa56dd
Merge https://github.com/op/go-logging
taruti Mar 8, 2016
44edaaa
Merge https://github.com/hilyjiang/go-logging
taruti Mar 8, 2016
fd7c815
Merge pull request #5 from keybase/taruti/update-upstream+hilyjiang-repo
taruti Mar 9, 2016
75a0c5e
Fixes missing Criticalf method
gabriel Mar 23, 2016
3ef87c1
Merge pull request #6 from keybase/fixinterface
gabriel Mar 23, 2016
33b7001
Revert "Fixes missing Criticalf method"
gabriel Mar 23, 2016
f3c7c3c
Merge pull request #7 from keybase/revert-6-fixinterface
gabriel Mar 23, 2016
05fd699
fix lint
joshblum Apr 22, 2020
ce9d896
cleanup, rw mutex
joshblum Apr 22, 2020
2c2af16
fix existing broken test
joshblum Apr 22, 2020
ae4bcca
trigger ci
joshblum Apr 22, 2020
0955434
drop go 1.11/1.12
joshblum Apr 22, 2020
7a5ab2e
Merge pull request #9 from keybase/joshblum/locks-HOTPOT-2409
joshblum Apr 23, 2020
58d00cf
move to go modules
marceloneil Apr 29, 2020
153efd7
Merge pull request #10 from marceloneil/marcel/HOTPOT-2576-go-modules
joshblum Nov 9, 2021
854d991
test against supported go versions
joshblum Nov 9, 2021
91d0bb1
kick ci
joshblum Nov 18, 2021
35a15a9
Merge pull request #11 from keybase/joshblum/go1.17
joshblum Nov 18, 2021
aa4058b
test against the latest versions of go
joshblum Oct 7, 2022
c617c0c
go fmt ./...
joshblum Oct 7, 2022
3c57f4b
no ioutil
joshblum Oct 7, 2022
56ff13d
Merge pull request #12 from keybase/joshblum/upgrade-go
joshblum Oct 7, 2022
684745a
migrate to github actions
joshblum Dec 20, 2022
396ca57
Merge pull request #13 from keybase/joshblum/github-actions
joshblum Dec 20, 2022
6019b28
Test against the latest versions of Go
joshblum Dec 13, 2023
4b3ff33
Merge pull request #14 from keybase/joshblum/upgrade-go
joshblum Dec 13, 2023
c0ab005
Upgrade to go 1.23
joshblum Jan 6, 2025
8771804
Merge pull request #15 from keybase/joshblum/go-1.23-upgrade
joshblum Jan 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@

package logging

import (
"sync"
)

// defaultBackend is the backend used for all logging calls.
var defaultBackend LeveledBackend
var defaultBackendMutex sync.Mutex

func init() {
defaultBackendMutex = sync.Mutex{}
}

// Backend is the interface which a log backend need to implement to be able to
// be used as a logging backend.
Expand All @@ -23,7 +32,9 @@ func SetBackend(backends ...Backend) LeveledBackend {
backend = MultiLogger(backends...)
}

defaultBackendMutex.Lock()
defaultBackend = AddModuleLevel(backend)
defaultBackendMutex.Unlock()
return defaultBackend
}

Expand Down
3 changes: 2 additions & 1 deletion examples/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func main() {
// For demo purposes, create two backend for os.Stderr.
backend1 := logging.NewLogBackend(os.Stderr, "", 0)
backend2 := logging.NewLogBackend(os.Stderr, "", 0)

backend1.Color = true
backend2.Color = true
// For messages written to backend2 we want to add some additional
// information to the output, including the used log level and the name of
// the function.
Expand Down
6 changes: 6 additions & 0 deletions level.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type moduleLeveled struct {
backend Backend
formatter Formatter
once sync.Once
mutex sync.Mutex
}

// AddModuleLevel wraps a log backend with knobs to have different log levels
Expand All @@ -81,13 +82,15 @@ func AddModuleLevel(backend Backend) LeveledBackend {
leveled = &moduleLeveled{
levels: make(map[string]Level),
backend: backend,
mutex: sync.Mutex{},
}
}
return leveled
}

// GetLevel returns the log level for the given module.
func (l *moduleLeveled) GetLevel(module string) Level {
l.mutex.Lock()
level, exists := l.levels[module]
if exists == false {
level, exists = l.levels[""]
Expand All @@ -96,12 +99,15 @@ func (l *moduleLeveled) GetLevel(module string) Level {
level = DEBUG
}
}
l.mutex.Unlock()
return level
}

// SetLevel sets the log level for the given module.
func (l *moduleLeveled) SetLevel(level Level, module string) {
l.mutex.Lock()
l.levels[module] = level
l.mutex.Unlock()
}

// IsEnabledFor will return true if logging is enabled for the given module.
Expand Down
58 changes: 58 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2013, Örjan Persson. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package logging

import (
"fmt"
"io"
)

type color int

const (
colorBlack = iota + 30
colorRed
colorGreen
colorYellow
colorBlue
colorMagenta
colorCyan
colorWhite
)

var (
colors = []string{
CRITICAL: colorSeq(colorMagenta),
ERROR: colorSeq(colorRed),
WARNING: colorSeq(colorYellow),
NOTICE: colorSeq(colorGreen),
DEBUG: colorSeq(colorCyan),
}
boldcolors = []string{
CRITICAL: colorSeqBold(colorMagenta),
ERROR: colorSeqBold(colorRed),
WARNING: colorSeqBold(colorYellow),
NOTICE: colorSeqBold(colorGreen),
DEBUG: colorSeqBold(colorCyan),
}
)

func colorSeq(color color) string {
return fmt.Sprintf("\033[%dm", int(color))
}

func colorSeqBold(color color) string {
return fmt.Sprintf("\033[%d;1m", int(color))
}

func doFmtVerbLevelColor(layout string, level Level, output io.Writer) {
if layout == "bold" {
output.Write([]byte(boldcolors[level]))
} else if layout == "reset" {
output.Write([]byte("\033[0m"))
} else {
output.Write([]byte(colors[level]))
}
}
63 changes: 0 additions & 63 deletions log_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,10 @@ package logging

import (
"bytes"
"fmt"
"io"
"log"
)

type color int

const (
ColorBlack = iota + 30
ColorRed
ColorGreen
ColorYellow
ColorBlue
ColorMagenta
ColorCyan
ColorWhite
)

var (
colors = []string{
CRITICAL: ColorSeq(ColorMagenta),
ERROR: ColorSeq(ColorRed),
WARNING: ColorSeq(ColorYellow),
NOTICE: ColorSeq(ColorGreen),
DEBUG: ColorSeq(ColorCyan),
}
boldcolors = []string{
CRITICAL: ColorSeqBold(ColorMagenta),
ERROR: ColorSeqBold(ColorRed),
WARNING: ColorSeqBold(ColorYellow),
NOTICE: ColorSeqBold(ColorGreen),
DEBUG: ColorSeqBold(ColorCyan),
}
)

// LogBackend utilizes the standard log module.
type LogBackend struct {
Logger *log.Logger
Expand Down Expand Up @@ -75,35 +44,3 @@ func (b *LogBackend) Log(level Level, calldepth int, rec *Record) error {
return b.Logger.Output(calldepth+2, rec.Formatted(calldepth+1))
}

// ConvertColors takes a list of ints representing colors for log levels and
// converts them into strings for ANSI color formatting
func ConvertColors(colors []int, bold bool) []string {
converted := []string{}
for _, i := range colors {
if bold {
converted = append(converted, ColorSeqBold(color(i)))
} else {
converted = append(converted, ColorSeq(color(i)))
}
}

return converted
}

func ColorSeq(color color) string {
return fmt.Sprintf("\033[%dm", int(color))
}

func ColorSeqBold(color color) string {
return fmt.Sprintf("\033[%d;1m", int(color))
}

func doFmtVerbLevelColor(layout string, level Level, output io.Writer) {
if layout == "bold" {
output.Write([]byte(boldcolors[level]))
} else if layout == "reset" {
output.Write([]byte("\033[0m"))
} else {
output.Write([]byte(colors[level]))
}
}
13 changes: 5 additions & 8 deletions log_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var (
setConsoleTextAttributeProc = kernel32DLL.NewProc("SetConsoleTextAttribute")
)

type WORD uint16

// Character attributes
// Note:
// -- The attributes are combined to produce various colors (e.g., Blue + Green will create Cyan).
Expand All @@ -36,15 +38,15 @@ const (
)

var (
colors = []uint16{
win_colors = []uint16{
INFO: fgWhite,
CRITICAL: fgMagenta,
ERROR: fgRed,
WARNING: fgYellow,
NOTICE: fgGreen,
DEBUG: fgCyan,
}
boldcolors = []uint16{
win_boldcolors = []uint16{
INFO: fgWhite | fgIntensity,
CRITICAL: fgMagenta | fgIntensity,
ERROR: fgRed | fgIntensity,
Expand Down Expand Up @@ -84,7 +86,7 @@ func NewLogBackend(out io.Writer, prefix string, flag int) *LogBackend {
func (b *LogBackend) Log(level Level, calldepth int, rec *Record) error {
if b.Color && b.f != nil {
buf := &bytes.Buffer{}
setConsoleTextAttribute(b.f, colors[level])
setConsoleTextAttribute(b.f, win_colors[level])
buf.Write([]byte(rec.Formatted(calldepth + 1)))
err := b.Logger.Output(calldepth+2, buf.String())
setConsoleTextAttribute(b.f, fgWhite)
Expand All @@ -100,8 +102,3 @@ func setConsoleTextAttribute(f file, attribute uint16) bool {
ok, _, _ := setConsoleTextAttributeProc.Call(f.Fd(), uintptr(attribute), 0)
return ok != 0
}

func doFmtVerbLevelColor(layout string, level Level, output io.Writer) {
// TODO not supported on Windows since the io.Writer here is actually a
// bytes.Buffer.
}
2 changes: 2 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ func (l *Logger) log(lvl Level, format *string, args ...interface{}) {
return
}

defaultBackendMutex.Lock()
defaultBackend.Log(lvl, 2+l.ExtraCalldepth, record)
defaultBackendMutex.Unlock()
}

// Fatal is equivalent to l.Critical(fmt.Sprint()) followed by a call to os.Exit(1).
Expand Down