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:
Cory Redmond 2022-12-12 20:13:05 +00:00
parent cc367f9125
commit a21da9a793
No known key found for this signature in database
GPG Key ID: 803E8E76C6000C60
2 changed files with 24 additions and 9 deletions

View File

@ -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)
}
}

View File

@ -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)