blob: a7b726ab094c5fdda39ad38f92d53e44b0b02dad [file] [log] [blame]
David K. Bainbridge528b3182017-01-23 08:51:59 -08001// Copyright 2013, 2014 Canonical Ltd.
2// Licensed under the LGPLv3, see LICENCE file for details.
3
4package errors
5
6import (
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.
13var prefixSize int
14
15// goPath is the deduced path based on the location of this file as compiled.
16var goPath string
17
18func 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
33func trimGoPath(filename string) string {
34 if strings.HasPrefix(filename, goPath) {
35 return filename[prefixSize:]
36 }
37 return filename
38}