debug.go 1.4 KB

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