Created
August 21, 2016 03:16
-
-
Save odeke-em/d73b783c937f46fcd15fe17ef9127984 to your computer and use it in GitHub Desktop.
File, Function name and line information for debugging call sites
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 | |
import ( | |
"fmt" | |
"runtime" | |
) | |
func debugPrintf(fmt_ string, args ...interface{}) { | |
programCounter, file, line, _ := runtime.Caller(1) | |
fn := runtime.FuncForPC(programCounter) | |
prefix := fmt.Sprintf("[%s:%s %d] %s", file, fn.Name(), line, fmt_) | |
fmt.Printf(prefix, args...) | |
fmt.Println() | |
} | |
func foo() { | |
debugPrintf("calling here") | |
} | |
func foo2() { | |
debugPrintf("called here") | |
foo() | |
} | |
func main() { | |
foo2() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment