2015-03-23 08:03:12 +03:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gin
|
|
|
|
|
2015-08-03 18:28:12 +03:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
)
|
2015-03-23 08:03:12 +03:00
|
|
|
|
2015-05-24 02:00:17 +03:00
|
|
|
func init() {
|
|
|
|
log.SetFlags(0)
|
|
|
|
}
|
2015-07-02 21:24:54 +03:00
|
|
|
|
|
|
|
// IsDebugging returns true if the framework is running in debug mode.
|
2017-11-02 16:48:54 +03:00
|
|
|
// Use SetMode(gin.ReleaseMode) to disable debug mode.
|
2015-03-23 08:03:12 +03:00
|
|
|
func IsDebugging() bool {
|
2015-04-07 13:27:23 +03:00
|
|
|
return ginMode == debugCode
|
2015-03-23 08:03:12 +03:00
|
|
|
}
|
|
|
|
|
2015-05-09 04:34:43 +03:00
|
|
|
func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
|
2015-03-23 08:03:12 +03:00
|
|
|
if IsDebugging() {
|
|
|
|
nuHandlers := len(handlers)
|
2015-06-07 14:49:36 +03:00
|
|
|
handlerName := nameOfFunction(handlers.Last())
|
2015-12-10 17:15:17 +03:00
|
|
|
debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
|
2015-03-23 08:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 18:28:12 +03:00
|
|
|
func debugPrintLoadTemplate(tmpl *template.Template) {
|
|
|
|
if IsDebugging() {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, tmpl := range tmpl.Templates() {
|
|
|
|
buf.WriteString("\t- ")
|
|
|
|
buf.WriteString(tmpl.Name())
|
|
|
|
buf.WriteString("\n")
|
|
|
|
}
|
|
|
|
debugPrint("Loaded HTML Templates (%d): \n%s\n", len(tmpl.Templates()), buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-23 08:03:12 +03:00
|
|
|
func debugPrint(format string, values ...interface{}) {
|
|
|
|
if IsDebugging() {
|
2015-05-24 02:00:17 +03:00
|
|
|
log.Printf("[GIN-debug] "+format, values...)
|
2015-03-23 08:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
2015-05-09 04:34:43 +03:00
|
|
|
|
2017-09-29 06:58:57 +03:00
|
|
|
func debugPrintWARNINGDefault() {
|
2018-06-26 13:56:43 +03:00
|
|
|
debugPrint(`[WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.
|
|
|
|
|
|
|
|
`)
|
2017-09-29 06:58:57 +03:00
|
|
|
debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
|
|
|
|
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2015-07-02 21:24:54 +03:00
|
|
|
func debugPrintWARNINGNew() {
|
2015-05-24 02:21:23 +03:00
|
|
|
debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.
|
|
|
|
- using env: export GIN_MODE=release
|
|
|
|
- using code: gin.SetMode(gin.ReleaseMode)
|
|
|
|
|
|
|
|
`)
|
2015-05-09 04:34:43 +03:00
|
|
|
}
|
|
|
|
|
2015-07-02 21:24:54 +03:00
|
|
|
func debugPrintWARNINGSetHTMLTemplate() {
|
2015-06-26 17:08:55 +03:00
|
|
|
debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called
|
|
|
|
at initialization. ie. before any route is registered or the router is listening in a socket:
|
|
|
|
|
|
|
|
router := gin.Default()
|
|
|
|
router.SetHTMLTemplate(template) // << good place
|
|
|
|
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2015-05-09 04:34:43 +03:00
|
|
|
func debugPrintError(err error) {
|
|
|
|
if err != nil {
|
|
|
|
debugPrint("[ERROR] %v\n", err)
|
|
|
|
}
|
|
|
|
}
|