Skip to content

Commit

Permalink
Fixed the testing and improved the CheckError function
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterclaerhout committed Feb 6, 2020
1 parent 411f652 commit c2fbbd6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
24 changes: 16 additions & 8 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"fmt"
"io"
"os"
"strings"
"time"
Expand Down Expand Up @@ -31,10 +32,10 @@ var DebugSQLMode = false
var TimeZone *time.Location

// Stdout is the writer to where the stdout messages should be written (defaults to os.Stdout)
var Stdout *os.File = os.Stdout
var Stdout io.Writer = os.Stdout

// Stderr is the writer to where the stderr messages should be written (defaults to os.Stderr)
var Stderr *os.File = os.Stderr
var Stderr io.Writer = os.Stderr

// DefaultTimeFormat is the default format to use for the timestamps
var DefaultTimeFormat = "2006-01-02 15:04:05.000"
Expand Down Expand Up @@ -222,11 +223,18 @@ func Fatalf(format string, args ...interface{}) {
//
// If DebugMode is enabled a stack trace will also be printed to stderr
func CheckError(err error) {
if err != nil {
printMessage("FATAL", err.Error())
if DebugMode {
StackTrace(err)
}
OsExit(1)

if err == nil {
return
}

msg := err.Error()
if DebugMode {
msg = formatMessage(FormattedStackTrace(err))
}

printMessage("FATAL", msg)

OsExit(1)

}
5 changes: 2 additions & 3 deletions logger_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/fatih/color"
"github.com/mattn/go-colorable"
)

var logMutex = &sync.Mutex{}
Expand Down Expand Up @@ -66,9 +65,9 @@ func printMessage(level string, message string) {
}

if PrintColors {
cw := colorable.NewColorable(w)
// cw := colorable.NewColorable(w)
if c, ok := colors[level]; ok {
c.Fprintln(cw, message)
c.Fprintln(w, message)
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ func TestCheckError(t *testing.T) {
{"nil-debug-color", nil, true, "", "", -1},

{"err-release-nocolor", errors.New("test"), false, "", "test | FATAL | test\n", 1},
{"err-debug-nocolor", errors.New("test"), true, "", "test | FATAL | test\ntest | ERROR | *errors.fundamental test\n", 1},
{"err-debug-nocolor", errors.New("test"), true, "", "test | FATAL | *errors.fundamental test\n", 1},
}

for _, tc := range tests {
Expand Down Expand Up @@ -593,7 +593,7 @@ func TestCheckError(t *testing.T) {

assert.Equal(t, tc.expectedStdout, actualStdOut)
if tc.debug {
assert.True(t, strings.HasPrefix(actualStdErr, tc.expectedStderr))
assert.True(t, strings.HasPrefix(actualStdErr, tc.expectedStderr), actualStdErr)
} else {
assert.Equal(t, tc.expectedStderr, actualStdErr)
}
Expand Down

0 comments on commit c2fbbd6

Please sign in to comment.