debug.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 = 10
  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. func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
  21. if IsDebugging() {
  22. nuHandlers := len(handlers)
  23. handlerName := nameOfFunction(handlers.Last())
  24. if DebugPrintRouteFunc == nil {
  25. debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
  26. } else {
  27. DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)
  28. }
  29. }
  30. }
  31. func debugPrintLoadTemplate(tmpl *template.Template) {
  32. if IsDebugging() {
  33. var buf strings.Builder
  34. for _, tmpl := range tmpl.Templates() {
  35. buf.WriteString("\t- ")
  36. buf.WriteString(tmpl.Name())
  37. buf.WriteString("\n")
  38. }
  39. debugPrint("Loaded HTML Templates (%d): \n%s\n", len(tmpl.Templates()), buf.String())
  40. }
  41. }
  42. func debugPrint(format string, values ...interface{}) {
  43. if IsDebugging() {
  44. if !strings.HasSuffix(format, "\n") {
  45. format += "\n"
  46. }
  47. fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)
  48. }
  49. }
  50. func getMinVer(v string) (uint64, error) {
  51. first := strings.IndexByte(v, '.')
  52. last := strings.LastIndexByte(v, '.')
  53. if first == last {
  54. return strconv.ParseUint(v[first+1:], 10, 64)
  55. }
  56. return strconv.ParseUint(v[first+1:last], 10, 64)
  57. }
  58. func debugPrintWARNINGDefault() {
  59. if v, e := getMinVer(runtime.Version()); e == nil && v <= ginSupportMinGoVer {
  60. debugPrint(`[WARNING] Now Gin requires Go 1.11 or later and Go 1.12 will be required soon.
  61. `)
  62. }
  63. debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
  64. `)
  65. }
  66. func debugPrintWARNINGNew() {
  67. debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.
  68. - using env: export GIN_MODE=release
  69. - using code: gin.SetMode(gin.ReleaseMode)
  70. `)
  71. }
  72. func debugPrintWARNINGSetHTMLTemplate() {
  73. debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called
  74. at initialization. ie. before any route is registered or the router is listening in a socket:
  75. router := gin.Default()
  76. router.SetHTMLTemplate(template) // << good place
  77. `)
  78. }
  79. func debugPrintError(err error) {
  80. if err != nil {
  81. if IsDebugging() {
  82. fmt.Fprintf(DefaultErrorWriter, "[GIN-debug] [ERROR] %v\n", err)
  83. }
  84. }
  85. }