tracer

package module
v0.0.0-...-5d43492 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2018 License: Apache-2.0 Imports: 4 Imported by: 0

README

tracer

enable tracing of function calls with scoped entry/exit indentation

https://github.com/davidwalter0/go-tracer.git

go get github.com/davidwalter0/go-tracer

Deferable resource acquisition and release

A facet of scoped syntax is often the scoped resource acquisition, release semantic of a file handle or file reader

One method to achieve this is to have the acquisition function return a release function

    func AcquireRelease( resource *Resource ) ( release func() ) {
        resource.acquire()
        return func() { resource.release() }
    }

    // Can be called then like 
    defer AcquireRelease(&resource)()
    // which acquires at the point of the defer call, and is released
    // when the deferred returned method leaves the defer scope

This deferable pattern is used to setup a single scoped call from the defer function call point in source.

Another example to acquire and release a lock

    import (
        "github.com/davidwalter0/go-mutex"
    )
    // package scoped var
    var monitor = mutex.NewMonitor()

    func ProtectedBlock() {
      defer monitor()()
      // ... protected shared resources
    }

Tracing

Execute a scoped print from point of defer call to exit of a block with 0..n args to print for the trace entry and exit text.

For a thread safe version, use defer GuardedTrace()() be aware of locking hierarchical tracing with the same guard.

chained configurable option settings added for enable and detailed e.g.

    var detailed=false
    var enable=false
...
    defer tracer.Detailed(detailed).Enable(enable).ScopedTrace()()

Create an instance of tracer and call the receiver method with a defer call, for example:

Create

    var tracer *tracer.Tracer = tracer.New()

Call

    defer tracer.ScopedTrace()()

Call with trace text args

	defer tracer.ScopedTrace("scope process", "more", "text"))()

The unit tests dump some sample output

> tracer.TestTracerRecurse
  > tracer.recursive_trace >>3<<
    > tracer.recursive_trace >>2<<
      > tracer.recursive_trace >>0<<
      < tracer.recursive_trace >>0<<
    < tracer.recursive_trace >>2<<
    > tracer.deeper depth    2    4
      > tracer.deeper depth    2    3
        > tracer.deeper depth    2    2
          > tracer.deeper depth    2    1
            > tracer.deeper depth    2    0
            < tracer.deeper depth    2    0
          < tracer.deeper depth    2    1
        < tracer.deeper depth    2    2
      < tracer.deeper depth    2    3
    < tracer.deeper depth    2    4
  < tracer.recursive_trace >>3<<
< tracer.TestTracerRecurse


Notice that defer scope is to end of function, not end of loop or block

    === RUN   TestTraceLoopFuncScope

    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope > i 1
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope < i 1
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope > i 2
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope < i 2
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope > i 3
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope < i 3
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope > i 4
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope < i 4
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope > i 5
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope < i 5
    --- PASS: TestTraceLoopFuncScope (0.00s)
    === RUN   TestTraceLoopScope

    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope > i 1
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope < i 1
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope > i 2
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope < i 2
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope > i 3
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope < i 3
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope > i 4
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope < i 4
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope > i 5
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope < i 5
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:149:go-tracer.TestTraceLoopScope > i 1
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:149:go-tracer.TestTraceLoopScope   > i 2
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:149:go-tracer.TestTraceLoopScope     > i 3
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:149:go-tracer.TestTraceLoopScope       > i 4
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:149:go-tracer.TestTraceLoopScope         > i 5
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope           > i 1
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope           < i 1
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope           > i 2
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope           < i 2
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope           > i 3
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope           < i 3
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope           > i 4
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope           < i 4
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:129:go-tracer.TraceFuncScope           > i 5
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:130:go-tracer.TraceFuncScope           < i 5
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:154:go-tracer.TestTraceLoopScope         < i 5
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:154:go-tracer.TestTraceLoopScope       < i 4
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:154:go-tracer.TestTraceLoopScope     < i 3
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:154:go-tracer.TestTraceLoopScope   < i 2
    /go/src/github.com/davidwalter0/go-tracer/tracer_test.go:154:go-tracer.TestTraceLoopScope < i 1
    --- PASS: TestTraceLoopScope (0.00s)
    PASS
    ok  	github.com/davidwalter0/go-tracer	0.002s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallerInfo

func CallerInfo(detailed bool) (where string)

CallerInfo build the process trace call tree context info for a trace.

func CurrentScopeTraceDetail

func CurrentScopeTraceDetail()

CurrentScopeTraceDetail dump to stdout the stack location

Types

type Mutex

type Mutex sync.Mutex

Mutex local synonym for sync.Mutex for receiver methods

func (*Mutex) Guard

func (mutex *Mutex) Guard() func()

Guard alias of Monitor() block scoped mutex returns function for defer call. Ex: defer mutex.Guard()()

func (*Mutex) Lock

func (mutex *Mutex) Lock()

Lock the mutex

func (*Mutex) Monitor

func (mutex *Mutex) Monitor() func()

Monitor block scoped mutex block scoped mutex returns function for defer call. Ex: defer mutex.Monitor()()

func (*Mutex) MonitorTrace

func (mutex *Mutex) MonitorTrace(args ...interface{}) func()

MonitorTrace block scoped mutex with depth print defer mutex.MonitorTrace()() prefer to use example from tests with defer GuardedTrace()()

func (*Mutex) Unlock

func (mutex *Mutex) Unlock()

Unlock the mutex

type Tracer

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

Tracer state info for trace

func New

func New() (tracer *Tracer)

New initialize and return a new Tracer struct

func (*Tracer) Detail

func (tracer *Tracer) Detail() bool

Detail returns tracing detail state

func (*Tracer) Detailed

func (tracer *Tracer) Detailed(set bool) *Tracer

Detailed set detail state to the set value return Tracer

func (*Tracer) Disable

func (tracer *Tracer) Disable() *Tracer

Disable tracing for this Tracer object

func (*Tracer) Enable

func (tracer *Tracer) Enable(enable bool) *Tracer

Enable sets to arg enable state and returns *Tracer enabled state

func (*Tracer) Enabled

func (tracer *Tracer) Enabled() bool

Enabled returns tracing enabled state

func (*Tracer) GuardedTrace

func (tracer *Tracer) GuardedTrace(a ...interface{}) (exitScopeTrace func())

GuardedTrace execute print calls in lock guarded blocks, enter a scope with trace info and return a function that prints the end of trace info, call with: defer tracer.GuardedTrace()() to cause the initial call and deferred calls to be handled transparently.

func (*Tracer) Off

func (tracer *Tracer) Off() *Tracer

Off turns off and returns *Tracer

func (*Tracer) On

func (tracer *Tracer) On() *Tracer

On turns on and returns *Tracer

func (*Tracer) Printf

func (tracer *Tracer) Printf(format string, args ...interface{})

Printf print formatted string if tracer is on (true)

func (*Tracer) Println

func (tracer *Tracer) Println(c rune, a ...interface{})

Println print depth spaces before a char c (rune)

func (*Tracer) Reset

func (tracer *Tracer) Reset() *Tracer

Reset a Tracer struct to disable tracing and depth to zero and no detail

func (*Tracer) ScopedTrace

func (tracer *Tracer) ScopedTrace(a ...interface{}) (exitScopeTrace func())

ScopedTrace enter a scope with trace info and return a function that prints the end of trace info, call with defer tracer.ScopedTrace()() to cause the initial call and deferred calls to be handled transparently.

func (*Tracer) Space

func (tracer *Tracer) Space() (spaces string)

Space create a space filler string

Jump to

Keyboard shortcuts

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