Skip to content

Commit

Permalink
Merge branch 'master' into release/2.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
200sc committed Feb 25, 2021
2 parents 0dae2bc + 828120f commit a9f3e7d
Show file tree
Hide file tree
Showing 19 changed files with 680 additions and 64 deletions.
39 changes: 22 additions & 17 deletions cover.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,82 +9,82 @@ echo "" > coverage.txt
#
# Don't run coverage on examples, just test that they compile
go test ./examples/...
go test -coverprofile=profile.out -covermode=atomic ./shape
go test -coverprofile=profile.out -covermode=atomic ./alg
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./oakerr
go test -coverprofile=profile.out -covermode=atomic ./alg/intgeom
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./timing
go test -coverprofile=profile.out -covermode=atomic ./alg/floatgeom
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./physics
go test -coverprofile=profile.out -covermode=atomic ./collision
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./event
go test -coverprofile=profile.out -covermode=atomic ./collision/ray
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./render
go test -coverprofile=profile.out -covermode=atomic ./dlog
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./render/mod
go test -coverprofile=profile.out -covermode=atomic ./event
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./render/particle
go test -coverprofile=profile.out -covermode=atomic ./fileutil
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./alg
go test -coverprofile=profile.out -covermode=atomic ./mouse
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./alg/intgeom
go test -coverprofile=profile.out -covermode=atomic ./oakerr
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./alg/floatgeom
go test -coverprofile=profile.out -covermode=atomic ./physics
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./collision
go test -coverprofile=profile.out -covermode=atomic ./render
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./collision/ray
go test -coverprofile=profile.out -covermode=atomic ./render/mod
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./fileutil
go test -coverprofile=profile.out -covermode=atomic ./render/particle
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./mouse
go test -coverprofile=profile.out -covermode=atomic ./scene
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
go test -coverprofile=profile.out -covermode=atomic ./scene
go test -coverprofile=profile.out -covermode=atomic ./shape
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
Expand All @@ -93,4 +93,9 @@ go test -coverprofile=profile.out -covermode=atomic ./dlog
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
fi
go test -coverprofile=profile.out -covermode=atomic ./timing
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
11 changes: 8 additions & 3 deletions dlog/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"time"
)

var (
_ FullLogger = &logger{}
)

type logger struct {
bytPool sync.Pool
debugLevel Level
Expand Down Expand Up @@ -98,11 +102,12 @@ func truncateFileName(f string) string {
}

func (l *logger) checkFilter(f string, in ...interface{}) bool {
ret := false
for _, elem := range in {
ret = ret || strings.Contains(fmt.Sprintf("%s", elem), l.debugFilter)
if strings.Contains(fmt.Sprintf("%s", elem), l.debugFilter) {
return true
}
}
return ret || strings.Contains(f, l.debugFilter)
return strings.Contains(f, l.debugFilter)
}

// SetDebugFilter sets the string which determines
Expand Down
4 changes: 3 additions & 1 deletion dlog/dlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ var oakLogger Logger

// ErrorCheck checks that the input is not nil, then calls Error on it if it is
// not. Otherwise it does nothing.
func ErrorCheck(in error) {
// Emits the input error as is for additional processing if desired.
func ErrorCheck(in error) error {
if in != nil {
Error(in)
}
return in
}

// Error will write a log if the debug level is not NONE
Expand Down
182 changes: 182 additions & 0 deletions dlog/regexLogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package dlog

import (
"bytes"
"fmt"
"io"
"os"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)

var (
_ FullLogger = &RegexLogger{}
)

// RegexLogger is a logger implementation that offers some
// additional features on top of the default logger.
// Todo v3: combine logger implementations.
type RegexLogger struct {
debugLevel Level

debugFilter string
filterRegex *regexp.Regexp
// FilterOverrideLevel is the log level at which
// logs will be shown regardless of the filter.
FilterOverrideLevel Level

writer io.Writer
file io.Writer
}

// NewRegexLogger returns a custom logger that writes to os.Stdout and
// overrides filters on WARN or higher messages.
func NewRegexLogger(level Level) *RegexLogger {
return &RegexLogger{
debugLevel: level,
writer: os.Stdout,
FilterOverrideLevel: WARN,
}
}

// GetLogLevel returns the current log level, i.e WARN or INFO...
func (l *RegexLogger) GetLogLevel() Level {
return l.debugLevel
}

// dLog, the primary function of the package,
// prints out and writes to file a string
// containing the logged data separated by spaces,
// prepended with file and line information.
// It only includes logs which pass the current filters.
func (l *RegexLogger) dLog(w io.Writer, override bool, in ...interface{}) {
//(pc uintptr, file string, line int, ok bool)
_, f, line, ok := runtime.Caller(2)
if strings.Contains(f, "dlog") {
_, f, line, ok = runtime.Caller(3)
}
if ok {
var bldr strings.Builder
f = truncateFileName(f)
// Note on errors: these functions all return
// errors, but they are always nil.
bldr.WriteRune('[')
bldr.WriteString(f)
bldr.WriteRune(':')
bldr.WriteString(strconv.Itoa(line))
bldr.WriteString("] ")
bldr.WriteString(logLevels[l.GetLogLevel()])
bldr.WriteRune(':')
for _, elem := range in {
bldr.WriteString(fmt.Sprintf("%v ", elem))
}
bldr.WriteRune('\n')
fullLog := []byte(bldr.String())

if !override && !l.checkFilter(fullLog) {
return
}

_, err := w.Write(fullLog)
if err != nil {
fmt.Println("Logging error", err)
}
}
}

func (l *RegexLogger) checkFilter(fullLog []byte) bool {
if l.debugFilter == "" {
return true
}
if l.filterRegex != nil {
return l.filterRegex.Match(fullLog)
}
return bytes.Contains(fullLog, []byte(l.debugFilter))
}

// SetDebugFilter sets the string which determines
// what debug messages get printed. Only messages
// which contain the filer as a pseudo-regex
func (l *RegexLogger) SetDebugFilter(filter string) {
l.debugFilter = filter
var err error
l.filterRegex, err = regexp.Compile(filter)
if err != nil {
l.Error("could not compile filter regex", err)
}
}

// SetDebugLevel sets what message levels of debug
// will be printed.
func (l *RegexLogger) SetDebugLevel(dL Level) {
if dL < NONE || dL > VERBOSE {
l.Warn("Unknown debug level: ", dL)
l.debugLevel = NONE
} else {
l.debugLevel = dL
}
}

// CreateLogFile creates a file in the 'logs' directory
// of the starting point of this program to write logs to
func (l *RegexLogger) CreateLogFile() {
file := "logs/dlog"
file += time.Now().Format("_Jan_2_15-04-05_2006")
file += ".txt"
var err error
l.file, err = os.Create(file)
if err != nil {
fmt.Println("[oak]-------- No logs directory found. No logs will be written to file.")
return
}
l.writer = io.MultiWriter(l.file, l.writer)
}

// FileWrite acts just like a regular write on a RegexLogger. It does
// not respect overrides.
func (l *RegexLogger) FileWrite(in ...interface{}) {
if l.file == nil {
return
}
l.dLog(l.file, true, in...)
}

// Error will write a dlog if the debug level is not NONE
func (l *RegexLogger) Error(in ...interface{}) {
if l.debugLevel > NONE {
l.dLog(l.writer, l.FilterOverrideLevel > NONE, in)
}
}

// Warn will write a dLog if the debug level is higher than ERROR
func (l *RegexLogger) Warn(in ...interface{}) {
if l.debugLevel > ERROR {
l.dLog(l.writer, l.FilterOverrideLevel > ERROR, in)
}
}

// Info will write a dLog if the debug level is higher than WARN
func (l *RegexLogger) Info(in ...interface{}) {
if l.debugLevel > WARN {
l.dLog(l.writer, l.FilterOverrideLevel > WARN, in)
}
}

// Verb will write a dLog if the debug level is higher than INFO
func (l *RegexLogger) Verb(in ...interface{}) {
if l.debugLevel > INFO {
l.dLog(l.writer, l.FilterOverrideLevel > INFO, in)
}
}

// SetWriter sets the writer that RegexLogger logs to
func (l *RegexLogger) SetWriter(w io.Writer) error {
if w == nil {
return fmt.Errorf("cannot write to nil writer")
}
l.writer = w
return nil
}
Loading

0 comments on commit a9f3e7d

Please sign in to comment.