From 6baebd5da4e12acd27a10aa9a41919239ecab773 Mon Sep 17 00:00:00 2001 From: Tom Fleet Date: Wed, 17 Jan 2024 17:12:38 +0000 Subject: [PATCH] Improve package docs (#2) * Improve package docs * Clarify comment --- README.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- debug.go | 17 +++++++++++++---- debug_test.go | 10 +++++----- 3 files changed, 65 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 9b96481..9879d77 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,12 @@ [![CI](https://github.com/FollowTheProcess/debug/workflows/CI/badge.svg)](https://github.com/FollowTheProcess/debug/actions?query=workflow%3ACI) [![codecov](https://codecov.io/gh/FollowTheProcess/debug/branch/main/graph/badge.svg)](https://codecov.io/gh/FollowTheProcess/debug) -Like Rust's dbg macro, but for Go! - -> [!WARNING] -> **Debug is in early development and is not yet ready for use** +Like Rust's [dbg macro], but for Go! ## Project Description +`debug` provides a simple, elegant mechanism to streamline "print debugging", inspired by Rust's [dbg macro] + ## Installation ```shell @@ -22,9 +21,53 @@ go get github.com/FollowTheProcess/debug@latest ## Quickstart +### Before + +```go +package main + +import "fmt" + +func main() { + something := "hello" + fmt.Printf("something = %s\n", something) +} +``` + +```shell +something = something +``` + +- Not ideal, need to manually type variable name +- No source info, could easily get lost in a large program +- Need to deal with fmt verbs + +### With `debug.Debug` + +```go +package main + +import "github.com/FollowTheProcess/debug" + +func main() { + something := "hello" + debug.Debug(something) +} +``` + +```shell +DEBUG: [/Users/you/projects/myproject/main.go:7:3] something = hello +``` + +- Source info, yay! 🎉 +- Variable name is handled for you +- Can handle any valid go expression or value +- No fmt print verbs + ### Credits This package was created with [copier] and the [FollowTheProcess/go_copier] project template. [copier]: https://copier.readthedocs.io/en/stable/ [FollowTheProcess/go_copier]: https://github.com/FollowTheProcess/go_copier +[dbg macro]: https://doc.rust-lang.org/stable/std/macro.dbg.html diff --git a/debug.go b/debug.go index c4d6916..3c359f3 100644 --- a/debug.go +++ b/debug.go @@ -1,4 +1,7 @@ -// Package debug is a placeholder for something cool. +// Package debug provides an easy mechanism to enable and streamline "print debugging" +// inspired by Rust's [dbg macro]. +// +// [dbg macro]: https://doc.rust-lang.org/stable/std/macro.dbg.html package debug import ( @@ -12,7 +15,12 @@ import ( "runtime" ) -// Debug prints useful debugging information of any value or expression to the console. +// Debug prints the file, line number and the value to stderr in a nice human-readable format. +// +// Any internal error is printed to stderr and the process halted. +// +// something := "hello" +// debug.Debug(something) // DEBUG: [/Users/you/projects/myproject/main.go:30:3] something = hello func Debug(value any) { _, file, line, ok := runtime.Caller(1) // Skip: 1 so this file gets skipped if !ok { @@ -25,6 +33,7 @@ func Debug(value any) { f, err := parser.ParseFile(fset, file, nil, parser.ParseComments) if err != nil { fmt.Fprintf(os.Stderr, "DEBUG: Failed to parse %s: %v\n", file, err) + return } ast.Inspect(f, func(node ast.Node) bool { @@ -36,14 +45,14 @@ func Debug(value any) { // We already know the line from runtime.Caller so that gives us an easy filter straight away if start.Line == line { - // If the node we're currently visiting is a call expression + // If the node we're currently visiting is a call expression, i.e. a function call if parsed, ok := node.(*ast.CallExpr); ok { // If it's specifically a call to debug.Debug if isDebugCall(parsed.Fun) { arg := parsed.Args[0] // Debug takes a single argument buf := &bytes.Buffer{} printer.Fprint(buf, fset, arg) - fmt.Printf("DEBUG: [%v] %v = %v\n", fset.Position(parsed.Fun.Pos()), buf.String(), value) + fmt.Fprintf(os.Stderr, "DEBUG: [%v] %v = %v\n", fset.Position(parsed.Fun.Pos()), buf.String(), value) return false // Found it } } diff --git a/debug_test.go b/debug_test.go index 74a2f92..a19b93d 100644 --- a/debug_test.go +++ b/debug_test.go @@ -30,7 +30,7 @@ func TestDebug(t *testing.T) { debug.Debug(aString) } - out := captureStdout(t, runDebug) + out := captureStderr(t, runDebug) want := fmt.Sprintf(`DEBUG: [%[1]s:28:3] anInt = 2 DEBUG: [%[1]s:29:3] aBool = true @@ -40,11 +40,11 @@ DEBUG: [%[1]s:30:3] aString = hello world test.Diff(t, out, want) } -func captureStdout(t *testing.T, printer func()) string { +func captureStderr(t *testing.T, printer func()) string { t.Helper() - old := os.Stdout // Backup of the real one + old := os.Stderr // Backup of the real one defer func() { - os.Stdout = old // Set it back even if we error later + os.Stderr = old // Set it back even if we error later }() r, w, err := os.Pipe() @@ -53,7 +53,7 @@ func captureStdout(t *testing.T, printer func()) string { } // Set stdout to our new pipe - os.Stdout = w + os.Stderr = w capture := make(chan string) // Copy in a goroutine so printing can't block forever -- knotmirror 0.0.1