Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/lestrrat-go/pdebug

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/lestrrat-go/pdebug

  • v0.0.0-20210111095411-35b07dbf089b
  • Source
  • Go
  • Socket score

Version published
Created
Source

go-pdebug

Build Status

GoDoc

Utilities for my print debugging fun. YMMV

Synopsis

optimized

Description

Building with pdebug declares a constant, pdebug.Enabled which you can use to easily compile in/out depending on the presence of a build tag.

func Foo() {
  // will only be available if you compile with `-tags debug`
  if pdebug.Enabled {
    pdebug.Printf("Starting Foo()!
  }
}

Note that using github.com/lestrrat-go/pdebug and -tags debug only compiles in the code. In order to actually show the debug trace, you need to specify an environment variable:

# For example, to show debug code during testing:
PDEBUG_TRACE=1 go test -tags debug

If you want to forcefully show the trace (which is handy when you're debugging/testing), you can use the debug0 tag instead:

go test -tags debug0

Markers

When you want to print debug a chain of function calls, you can use the Marker functions:

func Foo() {
  if pdebug.Enabled {
    g := pdebug.Marker("Foo")
    defer g.End()
  }

  pdebug.Printf("Inside Foo()!")
}

This will cause all of the Printf calls to automatically indent the output so it's visually easier to see where a certain trace log is being generated.

By default it will print something like:

|DEBUG| START Foo
|DEBUG|   Inside Foo()!
|DEBUG| END Foo (1.23μs)

If you want to automatically show the error value you are returning (but only if there is an error), you can use the BindError method:

func Foo() (err error) {
  if pdebug.Enabled {
    g := pdebug.Marker("Foo").BindError(&err)
    defer g.End()
  }

  pdebug.Printf("Inside Foo()!")

  return errors.New("boo")
}

This will print something like:

|DEBUG| START Foo
|DEBUG|   Inside Foo()!
|DEBUG| END Foo (1.23μs): ERROR boo

FAQs

Package last updated on 11 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc