Created
November 11, 2023 19:45
-
-
Save ucirello/0b1205a1166a14c36cb61b8655974e8a to your computer and use it in GitHub Desktop.
depthlogger by bradfitz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main // by Brad Fitzpatrick https://play.golang.org/p/PM0Fx-o6Drz | |
| import ( | |
| "fmt" | |
| "log" | |
| "os" | |
| "runtime" | |
| "strings" | |
| ) | |
| func init() { | |
| log.SetOutput(depthLogger{}) | |
| } | |
| func main() { | |
| log.Printf("in main") | |
| defer log.Printf("out of main") | |
| foo() | |
| } | |
| func foo() { | |
| log.Printf("in foo") | |
| defer log.Printf("out of foo") | |
| bar() | |
| } | |
| func bar() { | |
| log.Printf("in foo") | |
| defer log.Printf("out of foo") | |
| } | |
| type depthLogger struct{} | |
| func (depthLogger) Write(p []byte) (int, error) { | |
| fmt.Fprintf(os.Stderr, "%s%s%s", p[:19], strings.Repeat(" ", depth()*2), p[19:]) | |
| return len(p), nil | |
| } | |
| func depth() int { | |
| var p [50]uintptr | |
| return runtime.Callers(9, p[:]) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment