From a21da9a793caa68f668f21de5ce8b7cf33495860 Mon Sep 17 00:00:00 2001 From: Cory Redmond Date: Mon, 12 Dec 2022 20:13:05 +0000 Subject: [PATCH] Allow the ability to make DebugPrintRouteFunc more verbose Allow passing the handlers (whilst maintaining backwards compatibility) by passing the raw handlers array. --- debug.go | 21 ++++++++++++--------- debug_test.go | 12 ++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/debug.go b/debug.go index cbcedbc9..35ef3bc8 100644 --- a/debug.go +++ b/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) } } diff --git a/debug_test.go b/debug_test.go index abe8b41c..3a64d628 100644 --- a/debug_test.go +++ b/debug_test.go @@ -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)