debug.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 = 6
  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. fmt.Fprintf(os.Stderr, "[GIN-debug] "+format, values...)
  47. }
  48. }
  49. func getMinVer(v string) (uint64, error) {
  50. first := strings.IndexByte(v, '.')
  51. last := strings.LastIndexByte(v, '.')
  52. if first == last {
  53. return strconv.ParseUint(v[first+1:], 10, 64)
  54. }
  55. return strconv.ParseUint(v[first+1:last], 10, 64)
  56. }
  57. func debugPrintWARNINGDefault() {
  58. if v, e := getMinVer(runtime.Version()); e == nil && v <= ginSupportMinGoVer {
  59. debugPrint(`[WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.
  60. `)
  61. }
  62. debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
  63. `)
  64. }
  65. func debugPrintWARNINGNew() {
  66. debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.
  67. - using env: export GIN_MODE=release
  68. - using code: gin.SetMode(gin.ReleaseMode)
  69. `)
  70. }
  71. func debugPrintWARNINGSetHTMLTemplate() {
  72. debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called
  73. at initialization. ie. before any route is registered or the router is listening in a socket:
  74. router := gin.Default()
  75. router.SetHTMLTemplate(template) // << good place
  76. `)
  77. }
  78. func debugPrintError(err error) {
  79. if err != nil {
  80. debugPrint("[ERROR] %v\n", err)
  81. }
  82. }