Skip to content

Commit

Permalink
Merge pull request #40 from iotdog/master
Browse files Browse the repository at this point in the history
fix a bug of terminal log printing
  • Loading branch information
AlexStocks committed Dec 23, 2020
2 parents d5cb050 + 2c81d49 commit 87ff08f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions log/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"path/filepath"
"runtime"
"time"
)

import (
"github.com/mattn/go-isatty"
)

Expand Down Expand Up @@ -85,7 +87,7 @@ func CPrintfln(color []byte, format string, args ...interface{}) {
func CEPrintf(color []byte, format string, args ...interface{}) {
logStr := fmt.Sprintf(format, args...)
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Fprint(os.Stderr, string(color)+funcFileLine()+"%s"+string(reset), logStr)
fmt.Fprintf(os.Stderr, string(color)+funcFileLine()+"%s"+string(reset), logStr)
} else {
fmt.Fprintf(os.Stderr, "%s", logStr)
}
Expand All @@ -94,7 +96,7 @@ func CEPrintf(color []byte, format string, args ...interface{}) {
func CEPrintfln(color []byte, format string, args ...interface{}) {
logStr := fmt.Sprintf(format, args...)
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Fprint(os.Stderr, string(color)+funcFileLine()+"%s"+string(reset)+"\n", logStr)
fmt.Fprintf(os.Stderr, string(color)+funcFileLine()+"%s"+string(reset)+"\n", logStr)
} else {
fmt.Fprintf(os.Stderr, "%s\n", logStr)
}
Expand Down
4 changes: 4 additions & 0 deletions log/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ func TestColorLog(t *testing.T) {
CWarn("%s", "/test/group%3Dbjtelecom%26protocol%3Dpb%26role%3DSRT_Provider%26service%3Dshopping%26version%3D1.0.1")
CError("Error")
}

func TestCPrintfln(t *testing.T) {
CPrintfln(NRed, "%s", "/test/group%3Dbjtelecom%26protocol%3Dpb%26role%3DSRT_Provider%26service%3Dshopping%26version%3D1.0.1")
}

0 comments on commit 87ff08f

Please sign in to comment.