github.com/tencent/goom@v1.0.1/debug.go (about) 1 package mocker 2 3 import ( 4 "reflect" 5 6 "github.com/tencent/goom/arg" 7 "github.com/tencent/goom/internal/hack" 8 "github.com/tencent/goom/internal/iface" 9 "github.com/tencent/goom/internal/logger" 10 ) 11 12 // excludeFunc 对 excludeFunc 不进行拦截 13 const ( 14 excludeFunc = "time.Now" 15 ) 16 17 // interceptDebugInfo 添加对 apply 的拦截代理,截取函数调用信息用于 debug 18 func interceptDebugInfo(imp interface{}, pFunc iface.PFunc, mocker Mocker) (interface{}, iface.PFunc) { 19 if !logger.IsDebugOpen() { 20 return imp, pFunc 21 } 22 23 // 因为当使用了 when 时候,imp 代理会被覆盖,pFunc 会生效; 所以优先拦截有 pFunc 代理的 mock 回调 24 if pFunc != nil { 25 originPFunc := pFunc 26 pFunc = func(params []reflect.Value) []reflect.Value { 27 results := originPFunc(params) 28 // 日志打印用到了 time.Now,避免递归死循环 29 if mocker.String() == excludeFunc { 30 return results 31 } 32 logger.Consolefc(logger.DebugLevel, "mocker [%s] called, args [%s], results [%s]", 33 logger.Caller(hack.InterceptCallerSkip), mocker.String(), arg.SprintV(params), arg.SprintV(results)) 34 return results 35 } 36 return imp, pFunc 37 } 38 39 if imp != nil { 40 originImp := imp 41 imp = reflect.MakeFunc(reflect.TypeOf(imp), func(params []reflect.Value) []reflect.Value { 42 results := reflect.ValueOf(originImp).Call(params) 43 // 日志打印用到了 time.Now,避免递归死循环 44 if mocker.String() == excludeFunc { 45 return results 46 } 47 logger.Consolefc(logger.DebugLevel, "mocker [%s] called, args [%s], results [%s]", 48 logger.Caller(hack.InterceptCallerSkip), mocker.String(), arg.SprintV(params), arg.SprintV(results)) 49 return results 50 }).Interface() 51 return imp, pFunc 52 } 53 54 return imp, pFunc 55 }