Commit 87ff08f9 authored by Xin.Zh's avatar Xin.Zh Committed by AlexStocks

Merge pull request #40 from iotdog/master

fix a bug of terminal log printing
parents d5cb0508 2c81d49c
...@@ -25,7 +25,9 @@ import ( ...@@ -25,7 +25,9 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"time" "time"
)
import (
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
) )
...@@ -85,7 +87,7 @@ func CPrintfln(color []byte, format string, args ...interface{}) { ...@@ -85,7 +87,7 @@ func CPrintfln(color []byte, format string, args ...interface{}) {
func CEPrintf(color []byte, format string, args ...interface{}) { func CEPrintf(color []byte, format string, args ...interface{}) {
logStr := fmt.Sprintf(format, args...) logStr := fmt.Sprintf(format, args...)
if isatty.IsTerminal(os.Stdout.Fd()) { 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 { } else {
fmt.Fprintf(os.Stderr, "%s", logStr) fmt.Fprintf(os.Stderr, "%s", logStr)
} }
...@@ -94,7 +96,7 @@ func CEPrintf(color []byte, format string, args ...interface{}) { ...@@ -94,7 +96,7 @@ func CEPrintf(color []byte, format string, args ...interface{}) {
func CEPrintfln(color []byte, format string, args ...interface{}) { func CEPrintfln(color []byte, format string, args ...interface{}) {
logStr := fmt.Sprintf(format, args...) logStr := fmt.Sprintf(format, args...)
if isatty.IsTerminal(os.Stdout.Fd()) { 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 { } else {
fmt.Fprintf(os.Stderr, "%s\n", logStr) fmt.Fprintf(os.Stderr, "%s\n", logStr)
} }
......
...@@ -29,3 +29,7 @@ func TestColorLog(t *testing.T) { ...@@ -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") CWarn("%s", "/test/group%3Dbjtelecom%26protocol%3Dpb%26role%3DSRT_Provider%26service%3Dshopping%26version%3D1.0.1")
CError("Error") CError("Error")
} }
func TestCPrintfln(t *testing.T) {
CPrintfln(NRed, "%s", "/test/group%3Dbjtelecom%26protocol%3Dpb%26role%3DSRT_Provider%26service%3Dshopping%26version%3D1.0.1")
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment