path.go 849 B

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