caller

package module
v0.0.0-...-f23c17c Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2017 License: Apache-2.0 Imports: 7 Imported by: 1

README

caller

Package caller provides a helping hand around runtime.Caller() to look up file, line and name of the calling function. caller caches the results of its lookups and strips the uninteresting prefix from both the caller's location and name.

go get github.com/birkelund/caller

Usage

By default, the packge assumes that your project files are located on a path resembling $GOPATH/src/domain/org/project/(pkg)/module/submodule/file.go. In that case, use caller.Lookup(n) directly to strip paths down to module/submodule/file.go.

If that does not fit your project, use caller.NewCallResolver(re *regexp.Regexp) in combination with caller.BuildPathStripper(depth int) to get a suitable call resolver. depth is the number of subfolders from $GOPATH/src until the root of your project (e.g. $GOROOT/src/domain/x/tools/myproject would be a depth of 3).

Credits

This package is extracted from the CockroachDB source tree and modified to remove cockroach specific packages.

Documentation

Overview

Package caller provides a helping hand around runtime.Caller().

Package caller provides a helping hand around runtime.Caller() to look up file, line and name of the calling function. caller caches the results of its lookups and strips the uninteresting prefix from both the caller's location and name.

By default, the packge assumes that your project files are located on a path resembling `$GOPATH/src/domain/org/project/(pkg)/module/submodule/file.go`. In that case, use `caller.Lookup(n)` directly to strip paths down to `module/submodule/file.go`.

If that does not fit your project, use `caller.NewCallResolver(re *regexp.Regexp)` in combination with `caller.BuildPathStripper(depth int)` to get a suitable call resolver. `depth` is the number of subfolders from `$GOPATH/src` until the root of your project (e.g. `$GOROOT/src/domain/x/tools/myproject` would be a depth of 3).

This package is extracted from the CockroachDB source tree (see https://github.com/cockroachdb/cockroach/tree/master/pkg/util/caller) and modified to remove cockroach specific packages.

Index

Constants

This section is empty.

Variables

View Source
var DefaultCallResolver = NewCallResolver(defaultRE)

Functions

func BuildPathStripper

func BuildPathStripper(depth int) *regexp.Regexp

func Lookup

func Lookup(depth int) (file string, line int, fun string)

Lookup returns the file, line and function of the caller at the requested depth, using a default call resolver which drops the path of the project repository.

Types

type CallResolver

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

A CallResolver is a helping hand around runtime.Caller() to look up file, line and name of the calling function. CallResolver caches the results of its lookups and strips the uninteresting prefix from both the caller's location and name; see NewCallResolver().

func NewCallResolver

func NewCallResolver(re *regexp.Regexp) *CallResolver

NewCallResolver returns a CallResolver. The supplied pattern must specify a valid regular expression and is used to format the paths returned by Lookup(): If submatches are specified, their concatenation forms the path, otherwise the match of the whole expression is used. Paths which do not match at all are left unchanged.

func (*CallResolver) Lookup

func (cr *CallResolver) Lookup(depth int) (file string, line int, fun string)

Lookup returns the (reduced) file, line and function of the caller at the requested depth.

Jump to

Keyboard shortcuts

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