Browse Source

chek for panic

Denmaseno 9 years ago
parent
commit
c8e8abcdb3
1 changed files with 131 additions and 18 deletions
  1. 131 18
      log.go

+ 131 - 18
log.go

@@ -6,6 +6,7 @@ import (
 	"os"
 	"path/filepath"
 	"runtime"
+	"strings"
 	"time"
 )
 
@@ -71,6 +72,19 @@ func dump(a ...interface{}) []interface{} {
 // Trace log
 func Trace(a ...interface{}) {
 	if level <= TRACE {
+		for i := 1; i > 0 && i < 100; i++ {
+			_, path, _, _ := runtime.Caller(i)
+			if path != "" {
+				if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
+					_, path, line, _ := runtime.Caller(i + 1)
+					_, file := filepath.Split(path)
+					fmt.Fprintf(os.Stderr, "TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
+					return
+				}
+			} else {
+				i = -999
+			}
+		}
 		_, 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(dump(a...)...))
@@ -80,6 +94,19 @@ func Trace(a ...interface{}) {
 // Tracef log
 func Tracef(format string, a ...interface{}) {
 	if level <= TRACE {
+		for i := 1; i > 0 && i < 100; i++ {
+			_, path, _, _ := runtime.Caller(i)
+			if path != "" {
+				if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
+					_, path, line, _ := runtime.Caller(i + 1)
+					_, file := filepath.Split(path)
+					fmt.Fprintf(os.Stderr, "TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
+					return
+				}
+			} else {
+				i = -999
+			}
+		}
 		_, 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, dump(a...)...))
@@ -89,6 +116,19 @@ func Tracef(format string, a ...interface{}) {
 // Debug log
 func Debug(a ...interface{}) {
 	if level <= DEBUG {
+		for i := 1; i > 0 && i < 100; i++ {
+			_, path, _, _ := runtime.Caller(i)
+			if path != "" {
+				if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
+					_, path, line, _ := runtime.Caller(i + 1)
+					_, file := filepath.Split(path)
+					fmt.Fprintf(os.Stderr, "DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
+					return
+				}
+			} else {
+				i = -999
+			}
+		}
 		_, 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(dump(a...)...))
@@ -98,6 +138,19 @@ func Debug(a ...interface{}) {
 // Debugf log
 func Debugf(format string, a ...interface{}) {
 	if level <= DEBUG {
+		for i := 1; i > 0 && i < 100; i++ {
+			_, path, _, _ := runtime.Caller(i)
+			if path != "" {
+				if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
+					_, path, line, _ := runtime.Caller(i + 1)
+					_, file := filepath.Split(path)
+					fmt.Fprintf(os.Stderr, "DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
+					return
+				}
+			} else {
+				i = -999
+			}
+		}
 		_, 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, dump(a...)...))
@@ -107,6 +160,19 @@ func Debugf(format string, a ...interface{}) {
 // Info log
 func Info(a ...interface{}) {
 	if level <= INFO {
+		for i := 1; i > 0 && i < 100; i++ {
+			_, path, _, _ := runtime.Caller(i)
+			if path != "" {
+				if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
+					_, path, line, _ := runtime.Caller(i + 1)
+					_, file := filepath.Split(path)
+					fmt.Fprintf(os.Stderr, "INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
+					return
+				}
+			} else {
+				i = -999
+			}
+		}
 		_, 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(dump(a...)...))
@@ -116,6 +182,19 @@ func Info(a ...interface{}) {
 // Infof log
 func Infof(format string, a ...interface{}) {
 	if level <= INFO {
+		for i := 1; i > 0 && i < 100; i++ {
+			_, path, _, _ := runtime.Caller(i)
+			if path != "" {
+				if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
+					_, path, line, _ := runtime.Caller(i + 1)
+					_, file := filepath.Split(path)
+					fmt.Fprintf(os.Stderr, "INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
+					return
+				}
+			} else {
+				i = -999
+			}
+		}
 		_, 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, dump(a...)...))
@@ -125,6 +204,19 @@ func Infof(format string, a ...interface{}) {
 // Warning log
 func Warning(a ...interface{}) {
 	if level <= WARNING {
+		for i := 1; i > 0 && i < 100; i++ {
+			_, path, _, _ := runtime.Caller(i)
+			if path != "" {
+				if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
+					_, path, line, _ := runtime.Caller(i + 1)
+					_, file := filepath.Split(path)
+					fmt.Fprintf(os.Stderr, "WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
+					return
+				}
+			} else {
+				i = -999
+			}
+		}
 		_, 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(dump(a...)...))
@@ -134,6 +226,19 @@ func Warning(a ...interface{}) {
 // Warningf log
 func Warningf(format string, a ...interface{}) {
 	if level <= WARNING {
+		for i := 1; i > 0 && i < 100; i++ {
+			_, path, _, _ := runtime.Caller(i)
+			if path != "" {
+				if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
+					_, path, line, _ := runtime.Caller(i + 1)
+					_, file := filepath.Split(path)
+					fmt.Fprintf(os.Stderr, "WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
+					return
+				}
+			} else {
+				i = -999
+			}
+		}
 		_, 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, dump(a...)...))
@@ -143,6 +248,19 @@ func Warningf(format string, a ...interface{}) {
 // Error log
 func Error(a ...interface{}) {
 	if level <= ERROR {
+		for i := 1; i > 0 && i < 100; i++ {
+			_, path, _, _ := runtime.Caller(i)
+			if path != "" {
+				if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
+					_, path, line, _ := runtime.Caller(i + 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(dump(a...)...))
+					return
+				}
+			} else {
+				i = -999
+			}
+		}
 		_, 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(dump(a...)...))
@@ -152,26 +270,21 @@ func Error(a ...interface{}) {
 // Errorf log
 func Errorf(format string, a ...interface{}) {
 	if level <= ERROR {
+		for i := 1; i > 0 && i < 100; i++ {
+			_, path, _, _ := runtime.Caller(i)
+			if path != "" {
+				if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
+					_, path, line, _ := runtime.Caller(i + 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, dump(a...)...))
+					return
+				}
+			} else {
+				i = -999
+			}
+		}
 		_, 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, dump(a...)...))
 	}
 }
-
-// RError log
-func RError(a ...interface{}) {
-	if level <= ERROR {
-		_, path, line, _ := runtime.Caller(2)
-		_, 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(dump(a...)...))
-	}
-}
-
-// RErrorf log
-func RErrorf(format string, a ...interface{}) {
-	if level <= ERROR {
-		_, path, line, _ := runtime.Caller(2)
-		_, 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, dump(a...)...))
-	}
-}