debug.go 2.2 KB

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