Created
December 23, 2017 04:25
-
-
Save vikaschoudhary16/ac640cdbbb7f30bf435e8e651c5851d5 to your computer and use it in GitHub Desktop.
golang file_line caller prints
This file contains 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
Add this to pkg and this print: | |
This file contains 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 resourcequota | |
import "path/filepath" | |
import "runtime" | |
import "fmt" | |
func file_line() string { | |
// notice that we're using 1, so it will actually log the where | |
// the error happened, 0 = this function, we don't want that. | |
pc, fn, line, _ := runtime.Caller(1) | |
return fmt.Sprintf("%s[%s:%d]::: ", runtime.FuncForPC(pc).Name(), filepath.Base(fn), line) | |
} | |
func caller() string { | |
// notice that we're using 1, so it will actually log the where | |
// the error happened, 0 = this function, we don't want that. | |
pc, fn, line, _ := runtime.Caller(2) | |
return fmt.Sprintf("Called From: %s[%s:%d] ", runtime.FuncForPC(pc).Name(), filepath.Base(fn), line) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment