Skip to content

Bring back doFmtVerbLevelColor on Windows #76

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

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
test:
strategy:
matrix:
go-version: [1.21.x, 1.22.x, 1.23.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.63
- run: go vet ./...
- run: go test ./...
6 changes: 0 additions & 6 deletions .travis.yml

This file was deleted.

9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Golang logging library

[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/op/go-logging) [![build](https://img.shields.io/travis/op/go-logging.svg?style=flat)](https://travis-ci.org/op/go-logging)
[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/op/go-logging)
[![Build Status](https://github.com/keybase/go-logging/actions/workflows/ci.yml/badge.svg)](https://github.com/keybase/go-logging/actions)

Package logging implements a logging infrastructure for Go. Its output format
is customizable and supports different logging backends like syslog, file and
@@ -72,11 +73,11 @@ func main() {

## Installing

### Using *go get*
### Using _go get_

$ go get github.com/op/go-logging

After this command *go-logging* is ready to use. Its source will be in:
After this command _go-logging_ is ready to use. Its source will be in:

$GOPATH/src/pkg/github.com/op/go-logging

@@ -90,4 +91,4 @@ For docs, see http://godoc.org/github.com/op/go-logging or run:

## Additional resources

* [wslog](https://godoc.org/github.com/cryptix/go/logging/wslog) -- exposes log messages through a WebSocket.
- [wslog](https://godoc.org/github.com/cryptix/go/logging/wslog) -- exposes log messages through a WebSocket.
7 changes: 7 additions & 0 deletions backend.go
Original file line number Diff line number Diff line change
@@ -4,8 +4,13 @@

package logging

import (
"sync"
)

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

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

defaultBackendMutex.Lock()
defer defaultBackendMutex.Unlock()
defaultBackend = AddModuleLevel(backend)
return defaultBackend
}
5 changes: 3 additions & 2 deletions examples/example.go
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ package main
import (
"os"

"github.com/op/go-logging"
"github.com/keybase/go-logging"
)

var log = logging.MustGetLogger("example")
@@ -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.
46 changes: 21 additions & 25 deletions format.go
Original file line number Diff line number Diff line change
@@ -154,17 +154,18 @@ type stringFormatter struct {
// The verbs:
//
// General:
// %{id} Sequence number for log message (uint64).
// %{pid} Process id (int)
// %{time} Time when log occurred (time.Time)
// %{level} Log level (Level)
// %{module} Module (string)
// %{program} Basename of os.Args[0] (string)
// %{message} Message (string)
// %{longfile} Full file name and line number: /a/b/c/d.go:23
// %{shortfile} Final file name element and line number: d.go:23
// %{callpath} Callpath like main.a.b.c...c "..." meaning recursive call ~. meaning truncated path
// %{color} ANSI color based on log level
//
// %{id} Sequence number for log message (uint64).
// %{pid} Process id (int)
// %{time} Time when log occurred (time.Time)
// %{level} Log level (Level)
// %{module} Module (string)
// %{program} Basename of os.Args[0] (string)
// %{message} Message (string)
// %{longfile} Full file name and line number: /a/b/c/d.go:23
// %{shortfile} Final file name element and line number: d.go:23
// %{callpath} Callpath like main.a.b.c...c "..." meaning recursive call ~. meaning truncated path
// %{color} ANSI color based on log level
//
// For normal types, the output can be customized by using the 'verbs' defined
// in the fmt package, eg. '%{id:04d}' to make the id output be '%04d' as the
@@ -191,11 +192,12 @@ type stringFormatter struct {
// future.
//
// Experimental:
// %{longpkg} Full package path, eg. github.com/go-logging
// %{shortpkg} Base package path, eg. go-logging
// %{longfunc} Full function name, eg. littleEndian.PutUint32
// %{shortfunc} Base function name, eg. PutUint32
// %{callpath} Call function path, eg. main.a.b.c
//
// %{longpkg} Full package path, eg. github.com/go-logging
// %{shortpkg} Base package path, eg. go-logging
// %{longfunc} Full function name, eg. littleEndian.PutUint32
// %{shortfunc} Base function name, eg. PutUint32
// %{callpath} Call function path, eg. main.a.b.c
func NewStringFormatter(format string) (Formatter, error) {
var fmter = &stringFormatter{}

@@ -274,38 +276,32 @@ func (f *stringFormatter) add(verb fmtVerb, layout string) {
func (f *stringFormatter) Format(calldepth int, r *Record, output io.Writer) error {
for _, part := range f.parts {
if part.verb == fmtVerbStatic {
output.Write([]byte(part.layout))
_, _ = output.Write([]byte(part.layout))
} else if part.verb == fmtVerbTime {
output.Write([]byte(r.Time.Format(part.layout)))
_, _ = output.Write([]byte(r.Time.Format(part.layout)))
} else if part.verb == fmtVerbLevelColor {
doFmtVerbLevelColor(part.layout, r.Level, output)
} else if part.verb == fmtVerbCallpath {
depth, err := strconv.Atoi(part.layout)
if err != nil {
depth = 0
}
output.Write([]byte(formatCallpath(calldepth+1, depth)))
_, _ = output.Write([]byte(formatCallpath(calldepth+1, depth)))
} else {
var v interface{}
switch part.verb {
case fmtVerbLevel:
v = r.Level
break
case fmtVerbID:
v = r.ID
break
case fmtVerbPid:
v = pid
break
case fmtVerbProgram:
v = program
break
case fmtVerbModule:
v = r.Module
break
case fmtVerbMessage:
v = r.Message()
break
case fmtVerbLongfile, fmtVerbShortfile:
_, file, line, ok := runtime.Caller(calldepth + 1)
if !ok {
8 changes: 4 additions & 4 deletions format_test.go
Original file line number Diff line number Diff line change
@@ -109,13 +109,13 @@ func TestFormatFuncName(t *testing.T) {
"main",
"main",
"main"},
{"github.com/op/go-logging.func·001",
"github.com/op/go-logging",
{"github.com/keybase/go-logging.func·001",
"github.com/keybase/go-logging",
"go-logging",
"func·001",
"func·001"},
{"github.com/op/go-logging.stringFormatter.Format",
"github.com/op/go-logging",
{"github.com/keybase/go-logging.stringFormatter.Format",
"github.com/keybase/go-logging",
"go-logging",
"stringFormatter.Format",
"Format"},
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/keybase/go-logging

go 1.21
Empty file added go.sum
Empty file.
9 changes: 7 additions & 2 deletions level.go
Original file line number Diff line number Diff line change
@@ -66,6 +66,7 @@ type LeveledBackend interface {
}

type moduleLeveled struct {
sync.RWMutex
levels map[string]Level
backend Backend
formatter Formatter
@@ -88,11 +89,13 @@ func AddModuleLevel(backend Backend) LeveledBackend {

// GetLevel returns the log level for the given module.
func (l *moduleLeveled) GetLevel(module string) Level {
l.RLock()
defer l.RUnlock()
level, exists := l.levels[module]
if exists == false {
if !exists {
level, exists = l.levels[""]
// no configuration exists, default to debug
if exists == false {
if !exists {
level = DEBUG
}
}
@@ -101,6 +104,8 @@ func (l *moduleLeveled) GetLevel(module string) Level {

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

57 changes: 57 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 (
_ = iota + 30
colorRed
colorGreen
colorYellow
_
colorMagenta
colorCyan
)

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]))
}
}
65 changes: 1 addition & 64 deletions log_nix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

// Copyright 2013, Örjan Persson. All rights reserved.
@@ -8,41 +9,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
@@ -74,36 +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]))
}
}
28 changes: 14 additions & 14 deletions log_test.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ package logging

import (
"bytes"
"io/ioutil"
"io"
"log"
"strings"
"testing"
@@ -60,7 +60,7 @@ func testCallpath(t *testing.T, format string, expect string) {
}
// Verify that the correct callpath is registered by go-logging
if !strings.HasPrefix(parts[1], expect) {
t.Errorf("incorrect callpath: %s", parts[1])
t.Errorf("incorrect callpath: %s missing prefix %s", parts[1], expect)
}
// Verify that the correct message is registered by go-logging
if !strings.HasPrefix(parts[2], "test callpath") {
@@ -69,12 +69,12 @@ func testCallpath(t *testing.T, format string, expect string) {
}

func TestLogCallpath(t *testing.T) {
testCallpath(t, "%{callpath} %{message}", "TestLogCallpath.testCallpath.rec...rec.a.b.c")
testCallpath(t, "%{callpath:-1} %{message}", "TestLogCallpath.testCallpath.rec...rec.a.b.c")
testCallpath(t, "%{callpath:0} %{message}", "TestLogCallpath.testCallpath.rec...rec.a.b.c")
testCallpath(t, "%{callpath} %{message}", "TestLogCallpath.String.rec...a.b.c")
testCallpath(t, "%{callpath:-1} %{message}", "TestLogCallpath.String.rec...a.b.c")
testCallpath(t, "%{callpath:0} %{message}", "TestLogCallpath.String.rec...a.b.c")
testCallpath(t, "%{callpath:1} %{message}", "~.c")
testCallpath(t, "%{callpath:2} %{message}", "~.b.c")
testCallpath(t, "%{callpath:3} %{message}", "~.a.b.c")
testCallpath(t, "%{callpath:2} %{message}", "~.c.c")
testCallpath(t, "%{callpath:3} %{message}", "~.b.c.c")
}

func BenchmarkLogMemoryBackendIgnored(b *testing.B) {
@@ -98,34 +98,34 @@ func BenchmarkLogChannelMemoryBackend(b *testing.B) {
}

func BenchmarkLogLeveled(b *testing.B) {
backend := SetBackend(NewLogBackend(ioutil.Discard, "", 0))
backend := SetBackend(NewLogBackend(io.Discard, "", 0))
backend.SetLevel(INFO, "")

RunLogBenchmark(b)
}

func BenchmarkLogLogBackend(b *testing.B) {
backend := SetBackend(NewLogBackend(ioutil.Discard, "", 0))
backend := SetBackend(NewLogBackend(io.Discard, "", 0))
backend.SetLevel(DEBUG, "")
RunLogBenchmark(b)
}

func BenchmarkLogLogBackendColor(b *testing.B) {
colorizer := NewLogBackend(ioutil.Discard, "", 0)
colorizer := NewLogBackend(io.Discard, "", 0)
colorizer.Color = true
backend := SetBackend(colorizer)
backend.SetLevel(DEBUG, "")
RunLogBenchmark(b)
}

func BenchmarkLogLogBackendStdFlags(b *testing.B) {
backend := SetBackend(NewLogBackend(ioutil.Discard, "", log.LstdFlags))
backend := SetBackend(NewLogBackend(io.Discard, "", log.LstdFlags))
backend.SetLevel(DEBUG, "")
RunLogBenchmark(b)
}

func BenchmarkLogLogBackendLongFileFlag(b *testing.B) {
backend := SetBackend(NewLogBackend(ioutil.Discard, "", log.Llongfile))
backend := SetBackend(NewLogBackend(io.Discard, "", log.Llongfile))
backend.SetLevel(DEBUG, "")
RunLogBenchmark(b)
}
@@ -141,14 +141,14 @@ func RunLogBenchmark(b *testing.B) {
}

func BenchmarkLogFixed(b *testing.B) {
backend := SetBackend(NewLogBackend(ioutil.Discard, "", 0))
backend := SetBackend(NewLogBackend(io.Discard, "", 0))
backend.SetLevel(DEBUG, "")

RunLogBenchmarkFixedString(b)
}

func BenchmarkLogFixedIgnored(b *testing.B) {
backend := SetBackend(NewLogBackend(ioutil.Discard, "", 0))
backend := SetBackend(NewLogBackend(io.Discard, "", 0))
backend.SetLevel(INFO, "")
RunLogBenchmarkFixedString(b)
}
19 changes: 10 additions & 9 deletions log_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//go:build windows
// +build windows

// 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.
@@ -17,10 +19,14 @@ 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).
// Clearing all foreground or background colors results in black; setting all creates white.
//
// Clearing all foreground or background colors results in black; setting all creates white.
//
// See https://msdn.microsoft.com/en-us/library/windows/desktop/ms682088(v=vs.85).aspx#_win32_character_attributes.
const (
fgBlack = 0x0000
@@ -36,15 +42,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,
@@ -84,7 +90,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)
@@ -100,8 +106,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.
}
10 changes: 6 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ type Record struct {
func (r *Record) Formatted(calldepth int) string {
if r.formatted == "" {
var buf bytes.Buffer
r.formatter.Format(calldepth+1, r, &buf)
_ = r.formatter.Format(calldepth+1, r, &buf)
r.formatted = buf.String()
}
return r.formatted
@@ -70,7 +70,7 @@ func (r *Record) Message() string {
if r.message == nil {
// Redact the arguments that implements the Redactor interface
for i, arg := range r.Args {
if redactor, ok := arg.(Redactor); ok == true {
if redactor, ok := arg.(Redactor); ok {
r.Args[i] = redactor.Redacted()
}
}
@@ -163,11 +163,13 @@ func (l *Logger) log(lvl Level, format *string, args ...interface{}) {
// ExtraCallDepth allows this to be extended further up the stack in case we
// are wrapping these methods, eg. to expose them package level
if l.haveBackend {
l.backend.Log(lvl, 2+l.ExtraCalldepth, record)
_ = l.backend.Log(lvl, 2+l.ExtraCalldepth, record)
return
}

defaultBackend.Log(lvl, 2+l.ExtraCalldepth, record)
defaultBackendMutex.RLock()
defer defaultBackendMutex.RUnlock()
_ = defaultBackend.Log(lvl, 2+l.ExtraCalldepth, record)
}

// Fatal is equivalent to l.Critical(fmt.Sprint()) followed by a call to os.Exit(1).
19 changes: 10 additions & 9 deletions memory.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !appengine
// +build !appengine

package logging
@@ -74,7 +75,7 @@ func (b *MemoryBackend) Log(level Level, calldepth int, rec *Record) error {
tailp,
np,
)
if swapped == true {
if swapped {
if tailp == nil {
b.head = np
} else {
@@ -100,7 +101,7 @@ func (b *MemoryBackend) Log(level Level, calldepth int, rec *Record) error {
headp,
unsafe.Pointer(head.next),
)
if swapped == true {
if swapped {
atomic.AddInt32(&b.size, -1)
break
}
@@ -128,11 +129,11 @@ const (
// ChannelMemoryBackend is very similar to the MemoryBackend, except that it
// internally utilizes a channel.
type ChannelMemoryBackend struct {
sync.Mutex
maxSize int
size int
incoming chan *Record
events chan event
mu sync.Mutex
running bool
flushWg sync.WaitGroup
stopWg sync.WaitGroup
@@ -156,11 +157,11 @@ func NewChannelMemoryBackend(size int) *ChannelMemoryBackend {
// Start launches the internal goroutine which starts processing data from the
// input channel.
func (b *ChannelMemoryBackend) Start() {
b.mu.Lock()
defer b.mu.Unlock()
b.Lock()
defer b.Unlock()

// Launch the goroutine unless it's already running.
if b.running != true {
if !b.running {
b.running = true
b.stopWg.Add(1)
go b.process()
@@ -212,12 +213,12 @@ func (b *ChannelMemoryBackend) Flush() {

// Stop signals the internal goroutine to exit and waits until it have.
func (b *ChannelMemoryBackend) Stop() {
b.mu.Lock()
if b.running == true {
b.Lock()
defer b.Unlock()
if b.running {
b.running = false
b.events <- eventStop
}
b.mu.Unlock()
b.stopWg.Wait()
}

3 changes: 2 additions & 1 deletion syslog.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//+build !windows,!plan9
//go:build !windows && !plan9
// +build !windows,!plan9

package logging

3 changes: 2 additions & 1 deletion syslog_fallback.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//+build windows plan9
//go:build windows || plan9
// +build windows plan9

package logging