runtimeUtil

package
v0.0.0-...-e50112c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	StackFileFormatter     func(file string) (newFile string)
	StackFunctionFormatter func(funcName string) (newFuncName string)
)
View Source
var PanicLogger logs.Logger

Functions

func CallFunc

func CallFunc(f func(), msg ...string) (panic interface{})

安全的调用一个方法。 返回值:

panic: 方法执行期间产生的 panic 对象

func CallFuncE

func CallFuncE(f func() (err error), msg ...string) (err error, panic interface{})

安全的调用一个方法,并返回方法执行期间产生的 panic 返回值:

err: 方法返回的 error 对象
panic: 方法执行期间产生的 panic 对象

func CallFuncI

func CallFuncI(f func() (i interface{}), msg ...string) (i interface{}, panic interface{})

安全的调用一个方法,并返回方法执行期间产生的 panic 返回值:

err: 方法返回的 error 对象
panic: 方法执行期间产生的 panic 对象

func CallFuncIE

func CallFuncIE(f func() (i interface{}, err error), msg ...string) (i interface{}, err error, panic interface{})

安全的调用一个方法,并返回方法执行期间产生的 panic 返回值:

err: 方法返回的 error 对象
panic: 方法执行期间产生的 panic 对象

func Caller

func Caller(skip int) (fn, file string, line int, ok bool)

func CallerFile

func CallerFile(skip int) string

func CallerFileLine

func CallerFileLine(skip int) string

func CallerFunc

func CallerFunc(skip int) string

func ChdirWithTesting

func ChdirWithTesting(reg string)

ChdirWithTesting 判断是否在执行单元测试,尝试切换目录正则匹配的目录

func Go

func Go(n int, f func(i int))

func GoWait

func GoWait(wait time.Duration, n int, f func(i int, wait time.Duration) (done bool)) (allDone bool)

GoWait 并发超时执行,如果超时时间未执行完成,提前返回

func HandleRecover

func HandleRecover(msg string, e ...interface{}) interface{}

处理 recover:如果 e 不为空,则使用 logs.Default 打印堆栈信息 参数:

e: recover() 返回的对象。由于 recover 函数特性,无法在 HandleRecover 内部捕获上层函数的 panic 对象,所以必须由调用方传入。
Example
func() {
	defer HandleRecover("出错啦")
	// do something
}()

func() {
	defer func() {
		// 由于此处是在 defer 匿名函数内部,必须在参数中传递 recover 捕获的结果。
		// 否则在 HandleRecover 内部将由于嵌套层级过深而无法捕获 panic。
		if panicErr := HandleRecover("出错啦", recover()); panicErr != nil {
			// ...
		}
	}()
	// do something
}()
Output:

func IsTesting

func IsTesting() bool

IsTesting 判断是否是在执行单元测试

func PanicIfError

func PanicIfError(err error, msg ...string)

func PanicIfNil

func PanicIfNil(a interface{}, msg ...string)

func Recover

func Recover() *recoverHandler

创建一个用于捕获并处理 Panic 的对象。

该对象总是会将错误信息及堆栈打印到日志中,除非通过修改 PanicLogger 屏蔽日志。
Example
func(a *testing.T) {
	// 成功,能捕获到 recover
	defer Recover().Handle()
	a.Log("trigger panic")
}(nil)

func(a *testing.T) {
	defer func() {
		// 成功,在匿名函数中,先 recover 然后传递给 Handle。
		// Handle 结束后会将 recover 捕获到的对象重新返回。
		p := Recover().Handle(recover())
		print(p) // not nil
	}()
	a.Log("trigger panic")
}(nil)

func(a *testing.T) {
	defer func() {
		// 失败:
		// 由于在 defer 后的匿名函数内部,层次太深 Handle 内无法捕获 recover。
		// 如果要在匿名函数中使用,则必须使用 Handle(recover()) 代替。
		p := Recover().Handle()
		print(p) // nil
	}()
	a.Log("trigger panic")
}(nil)
Output:

func Retry

func Retry(loops int, interval time.Duration, f func(loop int) (breaking bool, err error)) (looped int, err error)

重试 参数:

loops: 最大重试次数
interval: 重试间隔

返回值:

looped: 一共重试了多少次,从 0 开始。

func StackStr

func StackStr(skip int, f ...func(frame *Frame) string) string

func StackStrN

func StackStrN(skip, limit int, f ...func(frame *Frame) string) string

func WriteStack

func WriteStack(w io.Writer, stack *StackInfo, msg ...string)

Types

type Frame

type Frame struct {
	Func  string `json:"func,omitempty"`
	Args  string `json:"args,omitempty"`
	File  string `json:"file,omitempty"`
	Line  int    `json:"line,omitempty"`
	Entry string `json:"entry,omitempty"`
}

func (*Frame) String

func (this *Frame) String() string

type StackInfo

type StackInfo struct {
	Goroutine int
	Frames    []*Frame
}

func Stack

func Stack(skip int, limit ...int) *StackInfo

func (*StackInfo) FPrint

func (this *StackInfo) FPrint(w io.Writer, f ...func(f *Frame) (string, bool))

func (*StackInfo) FPrints

func (this *StackInfo) FPrints(w io.Writer, msg string, f ...func(f *Frame) (string, bool))

func (*StackInfo) Log

func (this *StackInfo) Log(logger logs.Logger, level int, msg ...string)

func (*StackInfo) Logs

func (this *StackInfo) Logs(logger logs.Logger, level int, msg string, f ...func(f *Frame) (string, bool))

func (*StackInfo) Print

func (this *StackInfo) Print(f ...func(f *Frame) (string, bool))

func (*StackInfo) Prints

func (this *StackInfo) Prints(msg string, f ...func(f *Frame) (string, bool))

func (*StackInfo) ToString

func (this *StackInfo) ToString(msg string, f ...func(f *Frame) (string, bool)) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL