pruntime

package
v0.4.104 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2023 License: ISC Imports: 15 Imported by: 1

Documentation

Overview

Package pruntime provides an interface to the Go standard library’s runtime package using only serializable simple types

Stack traces and code locations have several formats:

codeLocation := pruntime.NewCodeLocation(0)
codeLocation.Base() // package and type
  → mypackage.(*MyType).MyFunc
codeLocation.PackFunc() // very brief
  → mypackage.MyFunc
codeLocation.Name(): // function name only
  → MyFunc
codeLocation.Short() // line, no package path
  → mypackage.(*MyType).MyFunc-myfile.go:19
codeLocation.Long() // uniquely identifiable
  → codeberg.org/haraldrudell/mypackage.(*MyType).MyFunc-myfile.go:19
codeLocation.Full() // everything
  → codeberg.org/haraldrudell/mypackage.(*MyType).MyFunc-/fs/mypackage/myfile.go:19
codeLocation.String() // two lines
  → "codeberg.org/haraldrudell/mypackage.(*MyType).MyFunc\n  /fs/mypackage/myfile.go:19"

Stack can determine where a goroutine was created and whether this is the main thread

pruntime.GoRoutineID()  → 1
pruntime.NewStack(0).Creator.Short()  → main.main-pruntime.go:30
fmt.Println(pruntime.NewStack(0).IsMainThread)  → true
pruntime.NewStack(0).Frames[0].Args  → (0x104c12c60?)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func D added in v0.4.29

func D(format string, a ...interface{})

D prints to stderr with code location. Thread-safe.

  • pruntime.D is like parl.D but can be used by packages imported by parl
  • pruntime imports from only one parl package: plogger
  • D is meant for temporary output intended to be removed before check-in

func DebugStack added in v0.4.12

func DebugStack(skipFrames int) (stack string)

DebugStack produces a string stack frame modified debug.Stack: Stack frames other than the callers of puntime.DebugStack are removed. skipFrames allows for removing additional frames. tabs are replaces with two spaces.

func FirstStackLine added in v0.4.99

func FirstStackLine() (firstStackLine string)

FirstStackLine efficiently obtains the first line of a runtime.Stack

  • "goroutine 34 [running]:\n…"

func Invocation

func Invocation(stackFramesToSkip int) (stackTrace string)

Invocation returns an invocation stack trace for debug printing, empty string on troubles. The result is similar to the output from debug.Stack, but has some stack frames removed. tabs are replaced by two spaces. stackFramesToSkip 0 means first frame will be the caller of Invocation "goroutine 1 [running]:\ngithub.com/haraldrudell/parl/mains.(*Executable).AddErr(0x1809300, 0x158b620, 0xc000183800, 0x1) mains.(*Executable).AddErr-executable.go:302…"

func IsCloseOfClosedChannel added in v0.4.103

func IsCloseOfClosedChannel(err error) (is bool)

IsCloseOfClosedChannel returns true if err’s error chain contains the runtime error “close of closed channel”

func IsRuntimeError

func IsRuntimeError(err error) (err1 runtime.Error)

IsRuntimeError determines if err’s error chain contains a runtime.Error

  • err1 is then a non-nil error value
  • runtime.Error is an interface describing the unexported runtime.plainError type

func IsSendOnClosedChannel

func IsSendOnClosedChannel(err error) (is bool)

IsSendOnClosedChannel returns true if err’s error chain contains the runtime error “send on closed channel”

  • runtime.plainError is an unexported named type of underlying type string
  • each error occurrence has a unique runtime.plainError value
  • runtime.plainError type have empty method RuntimeError()
  • the runtime.Error interface has the RuntimeError method
  • the Error method of runtime.Error returns the string value

func SplitAbsoluteFunctionName added in v0.4.29

func SplitAbsoluteFunctionName(absPath string) (
	packagePath, packageName, typePath, funcName string)

SplitAbsoluteFunctionName splits an absolute function name into its parts

  • input: github.com/haraldrudell/parl/error116.(*TypeName).FuncName[...]
  • packagePath: "github.com/haraldrudell/parl/"
  • packageName: "error116" single identifier, not empty
  • typePath: "(*TypeName)" may be empty
  • funcName: "FuncName[...]"

func StackString added in v0.4.99

func StackString() (stack string)

Types

type CachedLocation added in v0.4.87

type CachedLocation struct {
	// contains filtered or unexported fields
}

CachedLocation caches the code location and performantly provides it in string formats. Thread-safe

  • one top-level CachedLocation variable is required for each code location
  • code line provided is the location of first getter method

usage:

var mycl pruntime.CachedLocation
func f() {
  println(mycl.PackFunc())

func (*CachedLocation) FuncIdentifier added in v0.4.88

func (c *CachedLocation) FuncIdentifier() (funcIdentifier string)

"myFunc" Thread-safe

func (*CachedLocation) FuncName added in v0.4.87

func (c *CachedLocation) FuncName() (location string)

"github.com/haraldrudell/parl/mains.(*Executable).AddErr" Thread-safe

  • FuncName is the value compared to by [parl.SetRegexp]

func (*CachedLocation) PackFunc added in v0.4.87

func (c *CachedLocation) PackFunc() (packFunc string)

"mains.AddErr" Thread-safe

  • similar to [perrors.NewPF] or [perrors.ErrorfPF]

func (*CachedLocation) Short added in v0.4.87

func (c *CachedLocation) Short() (location string)

"mains.(*Executable).AddErr-executable.go:25" Thread-safe

  • similar to [perrors.Short] location

type CodeLocation

type CodeLocation struct {
	// File is the absolute path to the go source file
	//  /opt/foxyboy/sw/privates/parl/mains/executable.go
	File string
	// Line is the line number in the source file
	//  35
	Line int
	// FuncName is the fully qualified Go package path,
	// a possible value or pointer receiver struct name,
	// and the function name
	//  github.com/haraldrudell/parl/mains.(*Executable).AddErr
	FuncName string
}

CodeLocation is similar to runtime.Frame, but contains basic types string and int only

func GetCodeLocation

func GetCodeLocation(rFrame *runtime.Frame) (cl *CodeLocation)

GetCodeLocation converts a runtime stack frame to a code location stack frame. runtime contains opaque types while code location consists of basic types int and string only

func NewCodeLocation

func NewCodeLocation(stackFramesToSkip int) (cl *CodeLocation)

NewCodeLocation gets data for a single stack frame. if stackFramesToSkip is 0, NewCodeLocation returns data for its immediate caller.

func (*CodeLocation) Base

func (cl *CodeLocation) Base() (baseName string)

Base returns base package name, an optional type name and the function name:

mains.(*Executable).AddErr

func (*CodeLocation) Dump added in v0.4.92

func (cl *CodeLocation) Dump() (s string)

File: "/opt/homebrew/Cellar/go/1.20.4/libexec/src/testing/testing.go" Line: 1576 FuncName: "testing.tRunner"

func (*CodeLocation) Full

func (cl *CodeLocation) Full() (funcName string)

Full returns all available information on one line the function name, base filename and line number:

mains.(*Executable).AddErr-executable.go:25

func (*CodeLocation) FuncIdentifier added in v0.4.88

func (cl *CodeLocation) FuncIdentifier() (funcIdentifier string)

func (*CodeLocation) FuncLine added in v0.4.29

func (cl *CodeLocation) FuncLine() (funcLine string)

"github.com/haraldrudell/parl/mains.(*Executable).AddErr:43"

func (*CodeLocation) IsSet added in v0.4.25

func (cl *CodeLocation) IsSet() (isSet bool)

IsSet returns true if this CodeLocation has a value, ie. is not zero-value

func (*CodeLocation) Long

func (cl *CodeLocation) Long() (funcName string)

Long returns full package path, an optional type name and the function name, base filename and line number:

mains.(*Executable).AddErr-executable.go:25

func (*CodeLocation) Name

func (cl *CodeLocation) Name() (funcName string)

FuncName returns function name, characters no space:

AddErr

func (*CodeLocation) PackFunc

func (cl *CodeLocation) PackFunc() (packageDotFunction string)

PackFunc return base package name and function:

mains.AddErr

func (*CodeLocation) Package

func (cl *CodeLocation) Package() (packageName string)

Package return base package name, a single word of characters with no space:

mains

func (*CodeLocation) Short

func (cl *CodeLocation) Short() (funcName string)

Short returns base package name, an optional type name and the function name, base filename and line number:

mains.(*Executable).AddErr-executable.go:25

func (CodeLocation) String

func (cl CodeLocation) String() string

String returns a two-line string representation suitable for a multi-line stack trace. Typical output:

github.com/haraldrudell/parl/error116.(*TypeName).FuncName\n
  /opt/sw/privates/parl/error116/codelocation_test.go:20

type StackSlice added in v0.4.6

type StackSlice []CodeLocation

StackSlice represents a StackSlice of program counters.

func NewStackSlice added in v0.4.6

func NewStackSlice(skip int) (slice StackSlice)

NewStackSlice returns a slice of stack frames for the current execution.

  • if skip is 0, the caller of NewStackSlice is the first stack frame
  • for skip argument less than 0, 0 is used

func (StackSlice) Clone added in v0.4.6

func (st StackSlice) Clone() (s StackSlice)

func (StackSlice) Format added in v0.4.6

func (st StackSlice) Format(s fmt.State, verb rune)

Format implements fmt.Formatter

func (StackSlice) Short added in v0.4.6

func (st StackSlice) Short() (s string)

func (StackSlice) String added in v0.4.6

func (st StackSlice) String() (s string)

Jump to

Keyboard shortcuts

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