Denmaseno há 9 anos atrás
pai
commit
63518bfc88
2 ficheiros alterados com 41 adições e 10 exclusões
  1. 23 10
      log.go
  2. 18 0
      log_test.go

+ 23 - 10
log.go

@@ -57,12 +57,25 @@ func Dump(v ...interface{}) interface{} {
 	}
 }
 
+func dump(a ...interface{}) []interface{} {
+	for ai, av := range a {
+		if avf, ok := av.(func() string); ok {
+			a[ai] = avf()
+		} else if avf, ok := av.(fmt.Stringer); ok {
+			a[ai] = avf.String()
+		} else if avf, ok := av.(fmt.GoStringer); ok {
+			a[ai] = avf.GoString()
+		}
+	}
+	return a
+}
+
 // Trace log
 func Trace(a ...interface{}) {
 	if level <= TRACE {
 		_, path, line, _ := runtime.Caller(1)
 		_, file := filepath.Split(path)
-		fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
+		fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
 	}
 }
 
@@ -71,7 +84,7 @@ func Tracef(format string, a ...interface{}) {
 	if level <= TRACE {
 		_, path, line, _ := runtime.Caller(1)
 		_, file := filepath.Split(path)
-		fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
+		fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
 	}
 }
 
@@ -80,7 +93,7 @@ func Debug(a ...interface{}) {
 	if level <= DEBUG {
 		_, path, line, _ := runtime.Caller(1)
 		_, file := filepath.Split(path)
-		fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
+		fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
 	}
 }
 
@@ -89,7 +102,7 @@ func Debugf(format string, a ...interface{}) {
 	if level <= DEBUG {
 		_, path, line, _ := runtime.Caller(1)
 		_, file := filepath.Split(path)
-		fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
+		fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
 	}
 }
 
@@ -98,7 +111,7 @@ func Info(a ...interface{}) {
 	if level <= INFO {
 		_, path, line, _ := runtime.Caller(1)
 		_, file := filepath.Split(path)
-		fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
+		fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
 	}
 }
 
@@ -107,7 +120,7 @@ func Infof(format string, a ...interface{}) {
 	if level <= INFO {
 		_, path, line, _ := runtime.Caller(1)
 		_, file := filepath.Split(path)
-		fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
+		fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
 	}
 }
 
@@ -116,7 +129,7 @@ func Warning(a ...interface{}) {
 	if level <= WARNING {
 		_, path, line, _ := runtime.Caller(1)
 		_, file := filepath.Split(path)
-		fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
+		fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
 	}
 }
 
@@ -125,7 +138,7 @@ func Warningf(format string, a ...interface{}) {
 	if level <= WARNING {
 		_, path, line, _ := runtime.Caller(1)
 		_, file := filepath.Split(path)
-		fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
+		fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
 	}
 }
 
@@ -134,7 +147,7 @@ func Error(a ...interface{}) {
 	if level <= ERROR {
 		_, path, line, _ := runtime.Caller(1)
 		_, file := filepath.Split(path)
-		fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
+		fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
 	}
 }
 
@@ -143,6 +156,6 @@ func Errorf(format string, a ...interface{}) {
 	if level <= ERROR {
 		_, path, line, _ := runtime.Caller(1)
 		_, file := filepath.Split(path)
-		fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
+		fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
 	}
 }

+ 18 - 0
log_test.go

@@ -0,0 +1,18 @@
+package log
+
+import (
+	"testing"
+)
+
+// TestSimple1 test
+func TestSimple1(t *testing.T) {
+	t.Log("test simple1")
+
+	var fx func() string
+
+	fx = func() string {
+		return "XXXX"
+	}
+
+	Trace("HELO ", fx)
+}