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

Fix data race #8204

Merged
merged 7 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
17 changes: 14 additions & 3 deletions integrations/testlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"runtime"
"strings"
"sync"
"testing"

"code.gitea.io/gitea/modules/log"
Expand All @@ -25,11 +26,21 @@ type TestLogger struct {
var writerCloser = &testLoggerWriterCloser{}

type testLoggerWriterCloser struct {
sync.RWMutex
t testing.TB
}

func (w *testLoggerWriterCloser) setT(t *testing.TB) {
w.Lock()
w.t = *t
w.Unlock()
}

func (w *testLoggerWriterCloser) Write(p []byte) (int, error) {
if w.t != nil {
w.RLock()
t := w.t
w.RUnlock()
if t != nil {
if len(p) > 0 && p[len(p)-1] == '\n' {
p = p[:len(p)-1]
}
Expand All @@ -54,7 +65,7 @@ func (w *testLoggerWriterCloser) Write(p []byte) (int, error) {
}
}()

w.t.Log(string(p))
t.Log(string(p))
return len(p), nil
}
return len(p), nil
Expand All @@ -77,7 +88,7 @@ func PrintCurrentTest(t testing.TB, skip ...int) {
} else {
fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", t.Name(), strings.TrimPrefix(filename, prefix), line)
}
writerCloser.t = t
writerCloser.setT(&t)
}

// Printf takes a format and args and prints the string to os.Stdout
Expand Down
29 changes: 28 additions & 1 deletion modules/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"runtime"
"strings"
"sync"
)

var (
Expand All @@ -16,6 +17,7 @@ var (
// NamedLoggers map of named loggers
NamedLoggers = make(map[string]*Logger)
prefix string
mu sync.RWMutex
)

// NewLogger create a logger for the default logger
Expand All @@ -25,11 +27,17 @@ func NewLogger(bufLen int64, name, provider, config string) *Logger {
CriticalWithSkip(1, "Unable to create default logger: %v", err)
panic(err)
}
return NamedLoggers[DEFAULT]
mu.RLock()
l := NamedLoggers[DEFAULT]
mu.RUnlock()
return l
}

// NewNamedLogger creates a new named logger for a given configuration
func NewNamedLogger(name string, bufLen int64, subname, provider, config string) error {
mu.Lock()
defer mu.Unlock()

logger, ok := NamedLoggers[name]
if !ok {
logger = newLogger(name, bufLen)
Expand All @@ -42,6 +50,9 @@ func NewNamedLogger(name string, bufLen int64, subname, provider, config string)

// DelNamedLogger closes and deletes the named logger
func DelNamedLogger(name string) {
mu.Lock()
defer mu.Unlock()

l, ok := NamedLoggers[name]
if ok {
delete(NamedLoggers, name)
Expand All @@ -51,7 +62,9 @@ func DelNamedLogger(name string) {

// DelLogger removes the named sublogger from the default logger
func DelLogger(name string) error {
mu.RLock()
logger := NamedLoggers[DEFAULT]
mu.RUnlock()
found, err := logger.DelLogger(name)
if !found {
Trace("Log %s not found, no need to delete", name)
Expand All @@ -61,6 +74,9 @@ func DelLogger(name string) error {

// GetLogger returns either a named logger or the default logger
func GetLogger(name string) *Logger {
mu.RLock()
defer mu.RUnlock()

logger, ok := NamedLoggers[name]
if ok {
return logger
Expand All @@ -70,11 +86,15 @@ func GetLogger(name string) *Logger {

// GetLevel returns the minimum logger level
func GetLevel() Level {
mu.RLock()
defer mu.RUnlock()
return NamedLoggers[DEFAULT].GetLevel()
}

// GetStacktraceLevel returns the minimum logger level
func GetStacktraceLevel() Level {
mu.RLock()
defer mu.RUnlock()
return NamedLoggers[DEFAULT].GetStacktraceLevel()
}

Expand Down Expand Up @@ -169,6 +189,9 @@ func IsFatal() bool {

// Close closes all the loggers
func Close() {
mu.Lock()
defer mu.Unlock()

l, ok := NamedLoggers[DEFAULT]
if !ok {
return
Expand All @@ -180,7 +203,9 @@ func Close() {
// Log a message with defined skip and at logging level
// A skip of 0 refers to the caller of this command
func Log(skip int, level Level, format string, v ...interface{}) {
mu.RLock()
l, ok := NamedLoggers[DEFAULT]
lafriks marked this conversation as resolved.
Show resolved Hide resolved
mu.RUnlock()
if ok {
l.Log(skip+1, level, format, v...)
}
Expand All @@ -195,7 +220,9 @@ type LoggerAsWriter struct {
// NewLoggerAsWriter creates a Writer representation of the logger with setable log level
func NewLoggerAsWriter(level string, ourLoggers ...*Logger) *LoggerAsWriter {
if len(ourLoggers) == 0 {
mu.RLock()
ourLoggers = []*Logger{NamedLoggers[DEFAULT]}
mu.RUnlock()
}
l := &LoggerAsWriter{
ourLoggers: ourLoggers,
Expand Down