debug.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "fmt"
  7. "html/template"
  8. "runtime"
  9. "strconv"
  10. "strings"
  11. )
  12. const ginSupportMinGoVer = 18
  13. // IsDebugging returns true if the framework is running in debug mode.
  14. // Use SetMode(gin.ReleaseMode) to disable debug mode.
  15. func IsDebugging() bool {
  16. return ginMode == debugCode
  17. }
  18. // DebugPrintRouteFunc indicates debug log output format.
  19. var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)
  20. // DebugPrintFunc indicates debug log output format.
  21. var DebugPrintFunc func(format string, values ...interface{})
  22. func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
  23. if IsDebugging() {
  24. nuHandlers := len(handlers)
  25. handlerName := nameOfFunction(handlers.Last())
  26. if DebugPrintRouteFunc == nil {
  27. debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
  28. } else {
  29. DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)
  30. }
  31. }
  32. }
  33. func debugPrintLoadTemplate(tmpl *template.Template) {
  34. if IsDebugging() {
  35. var buf strings.Builder
  36. for _, tmpl := range tmpl.Templates() {
  37. buf.WriteString("\t- ")
  38. buf.WriteString(tmpl.Name())
  39. buf.WriteString("\n")
  40. }
  41. debugPrint("Loaded HTML Templates (%d): \n%s\n", len(tmpl.Templates()), buf.String())
  42. }
  43. }
  44. func debugPrint(format string, values ...any) {
  45. if !IsDebugging() {
  46. return
  47. }
  48. if DebugPrintFunc != nil {
  49. DebugPrintFunc(format, values...)
  50. return
  51. }
  52. if !strings.HasSuffix(format, "\n") {
  53. format += "\n"
  54. }
  55. fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)
  56. }
  57. func getMinVer(v string) (uint64, error) {
  58. first := strings.IndexByte(v, '.')
  59. last := strings.LastIndexByte(v, '.')
  60. if first == last {
  61. return strconv.ParseUint(v[first+1:], 10, 64)
  62. }
  63. return strconv.ParseUint(v[first+1:last], 10, 64)
  64. }
  65. func debugPrintWARNINGDefault() {
  66. if v, e := getMinVer(runtime.Version()); e == nil && v < ginSupportMinGoVer {
  67. debugPrint(`[WARNING] Now Gin requires Go 1.18+.
  68. `)
  69. }
  70. debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
  71. `)
  72. }
  73. func debugPrintWARNINGNew() {
  74. debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.
  75. - using env: export GIN_MODE=release
  76. - using code: gin.SetMode(gin.ReleaseMode)
  77. `)
  78. }
  79. func debugPrintWARNINGSetHTMLTemplate() {
  80. debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called
  81. at initialization. ie. before any route is registered or the router is listening in a socket:
  82. router := gin.Default()
  83. router.SetHTMLTemplate(template) // << good place
  84. `)
  85. }
  86. func debugPrintError(err error) {
  87. if err != nil && IsDebugging() {
  88. fmt.Fprintf(DefaultErrorWriter, "[GIN-debug] [ERROR] %v\n", err)
  89. }
  90. }