debug.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. "html/template"
  8. "log"
  9. )
  10. func init() {
  11. log.SetFlags(0)
  12. }
  13. // IsDebugging returns true if the framework is running in debug mode.
  14. // Use SetMode(gin.Release) to switch to disable the debug mode.
  15. func IsDebugging() bool {
  16. return ginMode == debugCode
  17. }
  18. func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
  19. if IsDebugging() {
  20. nuHandlers := len(handlers)
  21. handlerName := nameOfFunction(handlers.Last())
  22. debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
  23. }
  24. }
  25. func debugPrintLoadTemplate(tmpl *template.Template) {
  26. if IsDebugging() {
  27. var buf bytes.Buffer
  28. for _, tmpl := range tmpl.Templates() {
  29. buf.WriteString("\t- ")
  30. buf.WriteString(tmpl.Name())
  31. buf.WriteString("\n")
  32. }
  33. debugPrint("Loaded HTML Templates (%d): \n%s\n", len(tmpl.Templates()), buf.String())
  34. }
  35. }
  36. func debugPrint(format string, values ...interface{}) {
  37. if IsDebugging() {
  38. log.Printf("[GIN-debug] "+format, values...)
  39. }
  40. }
  41. func debugPrintWARNINGNew() {
  42. debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.
  43. - using env: export GIN_MODE=release
  44. - using code: gin.SetMode(gin.ReleaseMode)
  45. `)
  46. }
  47. func debugPrintWARNINGSetHTMLTemplate() {
  48. debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called
  49. at initialization. ie. before any route is registered or the router is listening in a socket:
  50. router := gin.Default()
  51. router.SetHTMLTemplate(template) // << good place
  52. `)
  53. }
  54. func debugPrintError(err error) {
  55. if err != nil {
  56. debugPrint("[ERROR] %v\n", err)
  57. }
  58. }