另客网go项目公用的代码库

debug.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.ReleaseMode) to disable 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 debugPrintWARNINGDefault() {
  42. debugPrint(`[WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.
  43. `)
  44. debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
  45. `)
  46. }
  47. func debugPrintWARNINGNew() {
  48. debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.
  49. - using env: export GIN_MODE=release
  50. - using code: gin.SetMode(gin.ReleaseMode)
  51. `)
  52. }
  53. func debugPrintWARNINGSetHTMLTemplate() {
  54. debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called
  55. at initialization. ie. before any route is registered or the router is listening in a socket:
  56. router := gin.Default()
  57. router.SetHTMLTemplate(template) // << good place
  58. `)
  59. }
  60. func debugPrintError(err error) {
  61. if err != nil {
  62. debugPrint("[ERROR] %v\n", err)
  63. }
  64. }