debug.go 2.2 KB

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