runtime

package
v1.2.8 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2022 License: MIT Imports: 8 Imported by: 6

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	DefaultPanic     = Panic{}
	NeverPanic       = Panic{IgnoreCrash: true}
	LogPanic         = Panic{PanicHandlers: []func(interface{}){logPanic}}
	NeverPanicButLog = Panic{IgnoreCrash: true, PanicHandlers: []func(interface{}){logPanic}}
)

Functions

func BytesPointer

func BytesPointer(data []byte) unsafe.Pointer

BytesPointer returns a pointer which an be used by CGO

func GetCallStack

func GetCallStack(size int) string

GetCallStack Same as stdlib http server code. Manually allocate stack trace buffer size to prevent excessively large logs

func GetCaller

func GetCaller(skip int) string

GetCaller returns the caller of the function that calls it. The argument skip is the number of stack frames to skip before recording in pc, with 0 identifying the frame for Callers itself and 1 identifying the caller of Callers.

Example
package main

import (
	"fmt"

	"github.com/searKing/golang/go/runtime"
)

func main() {
	caller := runtime.GetCaller(1)
	fmt.Print(caller)

}
Output:

github.com/searKing/golang/go/runtime_test.ExampleGetCaller

func GetCallerFrame

func GetCallerFrame(skip int) *runtime.Frame

GetCallerFrame returns the caller frame of the function that calls it. The argument skip is the number of stack frames to skip before recording in pc, with 0 identifying the frame for Callers itself and 1 identifying the caller of Callers.

func GetCallerFuncFileLine

func GetCallerFuncFileLine(skip int) (caller string, file string, line int)

GetCallerFuncFileLine returns the __FUNCTION__, __FILE__ and __LINE__ of the function that calls it. The argument skip is the number of stack frames to skip before recording in pc, with 0 identifying the frame for Callers itself and 1 identifying the caller of Callers.

func GetEIP

func GetEIP(uintptr) uintptr

GetEIP returns the location, that is EIP after CALL

func GetShortCaller

func GetShortCaller(skip int) string

GetShortCaller returns the short form of GetCaller. The argument skip is the number of stack frames to skip before recording in pc, with 0 identifying the frame for Callers itself and 1 identifying the caller of Callers.

func GetShortCallerFuncFileLine

func GetShortCallerFuncFileLine(skip int) (caller string, file string, line int)

GetShortCallerFuncFileLine returns the short form of GetCallerFuncFileLine. The argument skip is the number of stack frames to skip before recording in pc, with 0 identifying the frame for Callers itself and 1 identifying the caller of Callers.

Example
package main

import (
	"fmt"

	"github.com/searKing/golang/go/runtime"
)

func main() {
	caller, file, line := runtime.GetShortCallerFuncFileLine(1)
	fmt.Printf("%s() %s:%d", caller, file, line)

}
Output:

ExampleGetShortCallerFuncFileLine() run_example.go:63

func RecoverFromPanic

func RecoverFromPanic(err error) error

RecoverFromPanic replaces the specified error with an error containing the original error, and the call tree when a panic occurs. This enables error handlers to handle errors and panics the same way.

Types

type Panic

type Panic struct {
	// IgnoreCrash controls the behavior of Recover and now defaults false.
	// if false, crash immediately, rather than eating panics.
	IgnoreCrash bool

	// PanicHandlers for something like logging the panic message, shutting down go routines gracefully.
	PanicHandlers []func(interface{})
}

Panic simply catches a panic and logs an error. Meant to be called via defer. Additional context-specific handlers can be provided, and will be called in case of panic.

func HandlePanicWith

func HandlePanicWith(handlers ...func(interface{})) Panic

func (*Panic) AppendHandler

func (p *Panic) AppendHandler(handlers ...func(interface{})) *Panic

func (Panic) Recover

func (p Panic) Recover()

Recover actually crashes if IgnoreCrash is false, after calling PanicHandlers.

Jump to

Keyboard shortcuts

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