mirror of
https://github.com/gin-gonic/gin.git
synced 2025-04-06 03:57:46 +08:00
Allow the ability to make DebugPrintRouteFunc more verbose
Allow passing the handlers (whilst maintaining backwards compatibility) by passing the raw handlers array.
This commit is contained in:
parent
cc367f9125
commit
a21da9a793
21
debug.go
21
debug.go
@ -21,17 +21,20 @@ func IsDebugging() bool {
|
||||
}
|
||||
|
||||
// DebugPrintRouteFunc indicates debug log output format.
|
||||
var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)
|
||||
var DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
|
||||
debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
|
||||
}
|
||||
|
||||
// DebugPrintRouteRawFunc indicates debug log output format with handlers.
|
||||
var DebugPrintRouteRawFunc = func(httpMethod, absolutePath string, handlers HandlersChain) {
|
||||
if DebugPrintRouteFunc != nil {
|
||||
DebugPrintRouteFunc(httpMethod, absolutePath, nameOfFunction(handlers.Last()), len(handlers))
|
||||
}
|
||||
}
|
||||
|
||||
func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
|
||||
if IsDebugging() {
|
||||
nuHandlers := len(handlers)
|
||||
handlerName := nameOfFunction(handlers.Last())
|
||||
if DebugPrintRouteFunc == nil {
|
||||
debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
|
||||
} else {
|
||||
DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)
|
||||
}
|
||||
if IsDebugging() && DebugPrintRouteRawFunc != nil {
|
||||
DebugPrintRouteRawFunc(httpMethod, absolutePath, handlers)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,6 +77,18 @@ func TestDebugPrintRouteFunc(t *testing.T) {
|
||||
assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param1/:param2 --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, re)
|
||||
}
|
||||
|
||||
func TestDebugPrintRouteRawFunc(t *testing.T) {
|
||||
DebugPrintRouteRawFunc = func(httpMethod, absolutePath string, handlers HandlersChain) {
|
||||
fmt.Fprintf(DefaultWriter, "[GIN-debug] %-6s %-40s --> %s (%d handlers)\n", httpMethod, absolutePath, nameOfFunction(handlers.Last()), len(handlers)+1)
|
||||
}
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
debugPrintRoute("GET", "/path/to/route/:param1/:param2", HandlersChain{func(c *Context) {}, handlerNameTest})
|
||||
SetMode(TestMode)
|
||||
})
|
||||
assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param1/:param2 --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(3 handlers\)\n$`, re)
|
||||
}
|
||||
|
||||
func TestDebugPrintLoadTemplate(t *testing.T) {
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
|
Loading…
x
Reference in New Issue
Block a user