David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 1 | // Copyright 2013, 2014 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package errors |
| 5 | |
| 6 | import ( |
| 7 | "runtime" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | // prefixSize is used internally to trim the user specific path from the |
| 12 | // front of the returned filenames from the runtime call stack. |
| 13 | var prefixSize int |
| 14 | |
| 15 | // goPath is the deduced path based on the location of this file as compiled. |
| 16 | var goPath string |
| 17 | |
| 18 | func init() { |
| 19 | _, file, _, ok := runtime.Caller(0) |
| 20 | if file == "?" { |
| 21 | return |
| 22 | } |
| 23 | if ok { |
| 24 | // We know that the end of the file should be: |
| 25 | // github.com/juju/errors/path.go |
| 26 | size := len(file) |
| 27 | suffix := len("github.com/juju/errors/path.go") |
| 28 | goPath = file[:size-suffix] |
| 29 | prefixSize = len(goPath) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func trimGoPath(filename string) string { |
| 34 | if strings.HasPrefix(filename, goPath) { |
| 35 | return filename[prefixSize:] |
| 36 | } |
| 37 | return filename |
| 38 | } |