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. "bytes"
  7. "fmt"
  8. "html/template"
  9. "os"
  10. "runtime"
  11. "strconv"
  12. "strings"
  13. )
  14. const ginSupportMinGoVer = 8
  15. // IsDebugging returns true if the framework is running in debug mode.
  16. // Use SetMode(gin.ReleaseMode) to disable debug mode.
  17. func IsDebugging() bool {
  18. return ginMode == debugCode
  19. }
  20. // DebugPrintRouteFunc indicates debug log output format.
  21. var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)
  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 bytes.Buffer
  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 ...interface{}) {
  45. if IsDebugging() {
  46. if !strings.HasSuffix(format, "\n") {
  47. format += "\n"
  48. }
  49. fmt.Fprintf(os.Stderr, "[GIN-debug] "+format, values...)
  50. }
  51. }
  52. func getMinVer(v string) (uint64, error) {
  53. first := strings.IndexByte(v, '.')
  54. last := strings.LastIndexByte(v, '.')
  55. if first == last {
  56. return strconv.ParseUint(v[first+1:], 10, 64)
  57. }
  58. return strconv.ParseUint(v[first+1:last], 10, 64)
  59. }
  60. func debugPrintWARNINGDefault() {
  61. if v, e := getMinVer(runtime.Version()); e == nil && v <= ginSupportMinGoVer {
  62. debugPrint(`[WARNING] Now Gin requires Go 1.8 or later and Go 1.9 will be required soon.
  63. `)
  64. }
  65. debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
  66. `)
  67. }
  68. func debugPrintWARNINGNew() {
  69. debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.
  70. - using env: export GIN_MODE=release
  71. - using code: gin.SetMode(gin.ReleaseMode)
  72. `)
  73. }
  74. func debugPrintWARNINGSetHTMLTemplate() {
  75. debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called
  76. at initialization. ie. before any route is registered or the router is listening in a socket:
  77. router := gin.Default()
  78. router.SetHTMLTemplate(template) // << good place
  79. `)
  80. }
  81. func debugPrintError(err error) {
  82. if err != nil {
  83. debugPrint("[ERROR] %v\n", err)
  84. }
  85. }