debug.go 2.7 KB

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