caller

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: MIT Imports: 4 Imported by: 1

README

go-caller Go ReferenceGo Report Card

go-caller is a library for collecting stack traces at the runtime.

Usage

package main

import (
	"fmt"

	"github.com/newmo-oss/go-caller"
)

func main() {
	stacktrace := caller.New(1)
	frame := stacktrace[0]
	// main.go:10 main.main
	fmt.Printf("%s:%d %s.%s\n", frame.File(), frame.Line(), frame.PkgName(), frame.FuncName())
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Frame

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

Frame is a stack of frame.

func (Frame) File

func (f Frame) File() string

File returns the file path.

func (Frame) Format

func (f Frame) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

The verb means as follows.

verb	description	example
s	file name	a.go
s+	file path	example.com/sample/a/a.go
d	line number	10
n	function name	F.G.func1
P	package name	a
P+	import path	example.com/sample/a
v	file:line	a.go:10

func (Frame) FuncName

func (f Frame) FuncName() string

FuncName returns function name without package path. e.g., example.com/sample/a.F.G.func1 -> F.G.func1

func (Frame) Line

func (f Frame) Line() int

Line returns the line number.

func (Frame) PkgName

func (f Frame) PkgName() string

PkgName returns the package name of the function. e.g., example.com/sample/a.F.G.func1 -> a

func (Frame) PkgPath

func (f Frame) PkgPath() string

PkgPath returns the package (import path) of the function. e.g., example.com/sample/a.F.G.func1 -> example.com/sample/a

func (Frame) RuntimeFrame

func (f Frame) RuntimeFrame() runtime.Frame

RuntimeFrame returns runtime.Frame.

type StackTrace

type StackTrace []Frame

StackTrace is stack of Frames.

func New

func New(skip int) StackTrace

New creates callers of StackTrace. The argument skip is the number of stack frames to skip before recording, with 0 identifying the frame for New itself and 1 identifying the caller of New. It mean that New calls runtime.Callers with 2+skip as the first argument.

func (StackTrace) Format

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

Format implements fmt.Formatter.

Jump to

Keyboard shortcuts

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