| 1 | // Copyright ©2011-2012 The bíogo Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package util |
| 6 | |
| 7 | import ( |
| 8 | "runtime" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | type Caller struct { |
| 13 | Package string |
| 14 | Function string |
| 15 | File string |
| 16 | Line int |
| 17 | } |
| 18 | |
| 19 | func GetCaller(skip int) *Caller { |
| 20 | if pc, _, _, ok := runtime.Caller(skip + 1); ok { |
| 21 | function := runtime.FuncForPC(pc) |
| 22 | caller := strings.Split(function.Name(), ".") |
| 23 | file, line := function.FileLine(pc) |
| 24 | return &Caller{ |
| 25 | Package: strings.Join(caller[0:len(caller)-1], "."), |
| 26 | Function: caller[len(caller)-1], |
| 27 | File: file, |
| 28 | Line: line, |
| 29 | } |
| 30 | } |
| 31 | return nil |
| 32 | } |