【Gin-v1.9.0源码阅读】debug.go

// 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

import (
    "fmt"
    "html/template"
    "runtime"
    "strconv"
    "strings"
)

// gin支持的最低go版本号go1.16
const ginSupportMinGoVer = 16

// IsDebugging returns true if the framework is running in debug mode.
// 如果框架在调试模式下运行,则IsDebugging返回true。
// Use SetMode(gin.ReleaseMode) to disable debug mode.
// 使用SetMode(gin.ReleaseMode)禁用调试模式。
func IsDebugging() bool {
    return ginMode == debugCode
}

// DebugPrintRouteFunc indicates debug log output format.
// DebugPrintRouteFunc表示调试日志输出格式。
var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)

func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
    if IsDebugging() {
        nuHandlers := len(handlers)
        handlerName := nameOfFunction(handlers.Last())
        if DebugPrintRouteFunc == nil {
            debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
        } else {
            DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)
        }
    }
}

func debugPrintLoadTemplate(tmpl *template.Template) {
    if IsDebugging() {
        var buf strings.Builder
        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())
    }
}

func debugPrint(format string, values ...any) {
    if IsDebugging() {
        if !strings.HasSuffix(format, "\n") {
            format += "\n"
        }
        fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)
    }
}

// 获取go最低版本 o1.20 返回的是20
func getMinVer(v string) (uint64, error) {
    // IndexByte返回s中c的第一个实例的索引,如果s中不存在c,则返回-1。例如go1.20则返回3
    first := strings.IndexByte(v, '.')
    // LastIndexByte返回s中c的最后一个实例的索引,如果s中不存在c,则返回-1。例如go1.20则返回3
    last := strings.LastIndexByte(v, '.')
    if first == last {
        return strconv.ParseUint(v[first+1:], 10, 64)
    }
    return strconv.ParseUint(v[first+1:last], 10, 64)
}

func debugPrintWARNINGDefault() {
    // runtime.Version() 当前go版本号g01.20
    if v, e := getMinVer(runtime.Version()); e == nil && v < ginSupportMinGoVer {
        // 【警告】现在gin需要go达到1.16+
        debugPrint(`[WARNING] Now Gin requires Go 1.16+.

`)
    }
    // [警告]正在创建一个已连接Logger和Recovery中间件的Engine实例。
    debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

`)
}

func debugPrintWARNINGNew() {
    // [警告]以“调试”模式运行。在生产中切换到“发布”模式。
    //-使用env:export GIN_MODE=发布
    //-使用代码:gin.SetMode(gin.ReleaseMode)
    debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:    export GIN_MODE=release
 - using code:    gin.SetMode(gin.ReleaseMode)

`)
}

func debugPrintWARNINGSetHTMLTemplate() {
    // [警告]由于SetHTMLTemplate()不是线程安全的。只应调用
    // 在初始化时。即,在注册任何路由或路由器在套接字中侦听之前:
    //路由器:=gin.Default()
    //router.SetHTMLTemplate(模板)//<<好地方
    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

`)
}

func debugPrintError(err error) {
    if err != nil && IsDebugging() {
        fmt.Fprintf(DefaultErrorWriter, "[GIN-debug] [ERROR] %v\n", err)
    }
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/129775995
今日推荐