-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug.go
74 lines (61 loc) · 1.79 KB
/
debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package nin
import (
"fmt"
"runtime"
"strconv"
"strings"
)
const ninSupportMinGoVer = 18
// IsDebugging returns true if the framework is running in debug mode.
// Use SetMode(nin.ReleaseMode) to disable debug mode.
func IsDebugging() bool {
return ninMode == debugCode
}
// DebugPrintRouteFunc indicates debug log output format.
var DebugPrintRouteFunc func(path, handlerName string, nuHandlers int)
func debugPrintRoute(path string, handlers HandlersChain) {
if IsDebugging() {
nuHandlers := len(handlers)
handlerName := nameOfFunction(handlers.Last())
if DebugPrintRouteFunc == nil {
debugPrint("%-6s %-25s --> (%d handlers)\n", path, handlerName, nuHandlers)
} else {
DebugPrintRouteFunc(path, handlerName, nuHandlers)
}
}
}
func debugPrint(format string, values ...any) {
if IsDebugging() {
if !strings.HasSuffix(format, "\n") {
format += "\n"
}
fmt.Fprintf(DefaultWriter, "[NIN-debug] "+format, values...)
}
}
func getMinVer(v string) (uint64, error) {
first := strings.IndexByte(v, '.')
last := strings.LastIndexByte(v, '.')
if first == last {
return strconv.ParseUint(v[first+1:], 10, 64)
}
return strconv.ParseUint(v[first+1:last], 10, 64)
}
func debugPrintWARNINGDefault() {
if v, e := getMinVer(runtime.Version()); e == nil && v < ninSupportMinGoVer {
debugPrint(`[WARNING] Now Nin requires Go 1.18+.
`)
}
debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
`)
}
func debugPrintWARNINGNew() {
debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export NIN_MODE=release
- using code: nin.SetMode(nin.ReleaseMode)
`)
}
func debugPrintError(err error) {
if err != nil && IsDebugging() {
fmt.Fprintf(DefaultErrorWriter, "[NIN-debug] [ERROR] %v\n", err)
}
}