debug.go
This file
Path
debug.go
103 lines · 2.7 KiB · Go
Last changed by
Make sure the debug log contains line breaks (#1650)henrylee2cnbefore adoption3d44ff8
History of this fileTake it
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package gin67import (8 "bytes"9 "fmt"10 "html/template"11 "os"12 "runtime"13 "strconv"14 "strings"15)1617const ginSupportMinGoVer = 61819// IsDebugging returns true if the framework is running in debug mode.20// Use SetMode(gin.ReleaseMode) to disable debug mode.21func IsDebugging() bool {22 return ginMode == debugCode23}2425// DebugPrintRouteFunc indicates debug log output format.26var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)2728func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {29 if IsDebugging() {30 nuHandlers := len(handlers)31 handlerName := nameOfFunction(handlers.Last())32 if DebugPrintRouteFunc == nil {33 debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)34 } else {35 DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)36 }37 }38}3940func debugPrintLoadTemplate(tmpl *template.Template) {41 if IsDebugging() {42 var buf bytes.Buffer43 for _, tmpl := range tmpl.Templates() {44 buf.WriteString("\t- ")45 buf.WriteString(tmpl.Name())46 buf.WriteString("\n")47 }48 debugPrint("Loaded HTML Templates (%d): \n%s\n", len(tmpl.Templates()), buf.String())49 }50}5152func debugPrint(format string, values ...interface{}) {53 if IsDebugging() {54 if !strings.HasSuffix(format, "\n") {55 format += "\n"56 }57 fmt.Fprintf(os.Stderr, "[GIN-debug] "+format, values...)58 }59}6061func getMinVer(v string) (uint64, error) {62 first := strings.IndexByte(v, '.')63 last := strings.LastIndexByte(v, '.')64 if first == last {65 return strconv.ParseUint(v[first+1:], 10, 64)66 }67 return strconv.ParseUint(v[first+1:last], 10, 64)68}6970func debugPrintWARNINGDefault() {71 if v, e := getMinVer(runtime.Version()); e == nil && v <= ginSupportMinGoVer {72 debugPrint(`[WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.7374`)75 }76 debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.7778`)79}8081func debugPrintWARNINGNew() {82 debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.83 - using env: export GIN_MODE=release84 - using code: gin.SetMode(gin.ReleaseMode)8586`)87}8889func debugPrintWARNINGSetHTMLTemplate() {90 debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called91at initialization. ie. before any route is registered or the router is listening in a socket:9293 router := gin.Default()94 router.SetHTMLTemplate(template) // << good place9596`)97}9899func debugPrintError(err error) {100 if err != nil {101 debugPrint("[ERROR] %v\n", err)102 }103}