debug.go 1.3 KB

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