Skip to content
Myr

debug.go

This file

Path

debug.go

114 lines · 2.9 KiB · Go

Last changed by

ci: update Go version support to 1.25+ across CI and docs (#4550)Bo-Yi Wubefore adoptionae3f524

History of this file

Take 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	"fmt"9	"html/template"10	"runtime"11	"strconv"12	"strings"13	"sync/atomic"14)1516const ginSupportMinGoVer = 251718var runtimeVersion = runtime.Version()1920// IsDebugging returns true if the framework is running in debug mode.21// Use SetMode(gin.ReleaseMode) to disable debug mode.22func IsDebugging() bool {23	return atomic.LoadInt32(&ginMode) == debugCode24}2526// DebugPrintRouteFunc indicates debug log output format.27var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)2829// DebugPrintFunc indicates debug log output format.30var DebugPrintFunc func(format string, values ...any)3132func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {33	if IsDebugging() {34		nuHandlers := len(handlers)35		handlerName := nameOfFunction(handlers.Last())36		if DebugPrintRouteFunc == nil {37			debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)38		} else {39			DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)40		}41	}42}4344func debugPrintLoadTemplate(tmpl *template.Template) {45	if IsDebugging() {46		var buf strings.Builder47		for _, tmpl := range tmpl.Templates() {48			buf.WriteString("\t- ")49			buf.WriteString(tmpl.Name())50			buf.WriteString("\n")51		}52		debugPrint("Loaded HTML Templates (%d): \n%s\n", len(tmpl.Templates()), buf.String())53	}54}5556func debugPrint(format string, values ...any) {57	if !IsDebugging() {58		return59	}6061	if DebugPrintFunc != nil {62		DebugPrintFunc(format, values...)63		return64	}6566	if !strings.HasSuffix(format, "\n") {67		format += "\n"68	}69	fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)70}7172func getMinVer(v string) (uint64, error) {73	first := strings.IndexByte(v, '.')74	last := strings.LastIndexByte(v, '.')75	if first == last {76		return strconv.ParseUint(v[first+1:], 10, 64)77	}78	return strconv.ParseUint(v[first+1:last], 10, 64)79}8081func debugPrintWARNINGDefault() {82	if v, e := getMinVer(runtimeVersion); e == nil && v < ginSupportMinGoVer {83		debugPrint(`[WARNING] Now Gin requires Go 1.25+.8485`)86	}87	debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.8889`)90}9192func debugPrintWARNINGNew() {93	debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.94 - using env:	export GIN_MODE=release95 - using code:	gin.SetMode(gin.ReleaseMode)9697`)98}99100func debugPrintWARNINGSetHTMLTemplate() {101	debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called102at initialization. ie. before any route is registered or the router is listening in a socket:103104	router := gin.Default()105	router.SetHTMLTemplate(template) // << good place106107`)108}109110func debugPrintError(err error) {111	if err != nil && IsDebugging() {112		fmt.Fprintf(DefaultErrorWriter, "[GIN-debug] [ERROR] %v\n", err)113	}114}