github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/znet/debug.go (about) 1 package znet 2 3 import ( 4 "fmt" 5 "html/template" 6 "reflect" 7 "runtime" 8 9 "github.com/sohaha/zlsgo/zlog" 10 "github.com/sohaha/zlsgo/zreflect" 11 "github.com/sohaha/zlsgo/zstring" 12 ) 13 14 func routeLog(log *zlog.Logger, tf, method, path string) string { 15 mLen := zstring.Len(method) 16 var mtd string 17 min := 6 18 if mLen < min { 19 mtd = zstring.Pad(method, min, " ", zstring.PadLeft) 20 } else { 21 mtd = zstring.Substr(method, 0, min) 22 } 23 24 switch method { 25 case "GET": 26 method = log.ColorTextWrap(zlog.ColorLightCyan, mtd) 27 case "POST": 28 method = log.ColorTextWrap(zlog.ColorLightBlue, mtd) 29 case "PUT": 30 method = log.ColorTextWrap(zlog.ColorLightGreen, mtd) 31 case "DELETE": 32 method = log.ColorTextWrap(zlog.ColorRed, mtd) 33 case "ANY": 34 method = log.ColorTextWrap(zlog.ColorLightMagenta, mtd) 35 case "OPTIONS": 36 method = log.ColorTextWrap(zlog.ColorLightMagenta, mtd) 37 case "FILE": 38 method = log.ColorTextWrap(zlog.ColorLightMagenta, mtd) 39 default: 40 method = log.ColorTextWrap(zlog.ColorDefault, mtd) 41 } 42 path = zstring.Pad(path, 20, " ", zstring.PadRight) 43 return fmt.Sprintf(tf, method, path) 44 } 45 46 func templatesDebug(e *Engine, t *template.Template) { 47 l := 0 48 buf := zstring.Buffer() 49 for _, t := range t.Templates() { 50 n := t.Name() 51 if n == "" { 52 continue 53 } 54 buf.WriteString("\t - " + n + "\n") 55 l++ 56 } 57 e.Log.Debugf("Loaded HTML Templates (%d): \n%s", l, buf.String()) 58 } 59 60 func routeAddLog(e *Engine, method string, path string, action Handler, middlewareCount int) { 61 if e.IsDebug() { 62 v := zreflect.ValueOf(action) 63 if e.webMode == testCode { 64 e.Log.Debug(routeLog(e.Log, "%s %-40s", method, path)) 65 return 66 } 67 68 if v.Kind() == reflect.Func { 69 e.Log.Debug(routeLog(e.Log, fmt.Sprintf("%%s %%-40s -> %s (%d handlers)", runtime.FuncForPC(v.Pointer()).Name(), middlewareCount), method, path)) 70 } else { 71 e.Log.Warn(routeLog(e.Log, fmt.Sprintf("%%s %%-40s -> %s (%d handlers)", v.Type().String(), middlewareCount), method, path)) 72 } 73 } 74 }