diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index bd0524e09..62cc20c9b 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -1,6 +1,6 @@ { "ImportPath": "github.com/jbenet/go-ipfs", - "GoVersion": "go1.3", + "GoVersion": "go1.3.3", "Packages": [ "./..." ], @@ -60,6 +60,10 @@ "ImportPath": "github.com/coreos/go-semver/semver", "Rev": "6fe83ccda8fb9b7549c9ab4ba47f47858bc950aa" }, + { + "ImportPath": "github.com/facebookgo/stack", + "Rev": "4da6d991fc3c389efa512151354d643eb5fae4e2" + }, { "ImportPath": "github.com/facebookgo/stackerr", "Rev": "060fbf9364c89acd41bf710e9e92915a90e7a5b5" diff --git a/Godeps/_workspace/src/github.com/facebookgo/stack/.travis.yml b/Godeps/_workspace/src/github.com/facebookgo/stack/.travis.yml new file mode 100644 index 000000000..2cc62c5e8 --- /dev/null +++ b/Godeps/_workspace/src/github.com/facebookgo/stack/.travis.yml @@ -0,0 +1,24 @@ +language: go + +go: + - 1.2 + - 1.3 + +matrix: + fast_finish: true + +before_install: + - go get -v code.google.com/p/go.tools/cmd/vet + - go get -v github.com/golang/lint/golint + - go get -v code.google.com/p/go.tools/cmd/cover + +install: + - go install -race -v std + - go get -race -t -v ./... + - go install -race -v ./... + +script: + - go vet ./... + - $HOME/gopath/bin/golint . + - go test -cpu=2 -race -v ./... + - go test -cpu=2 -covermode=atomic ./... diff --git a/Godeps/_workspace/src/github.com/facebookgo/stack/readme.md b/Godeps/_workspace/src/github.com/facebookgo/stack/readme.md new file mode 100644 index 000000000..680f7ad41 --- /dev/null +++ b/Godeps/_workspace/src/github.com/facebookgo/stack/readme.md @@ -0,0 +1,4 @@ +stack [![Build Status](https://secure.travis-ci.org/facebookgo/stack.png)](http://travis-ci.org/facebookgo/stack) +===== + +Documentation: https://godoc.org/github.com/facebookgo/stack diff --git a/Godeps/_workspace/src/github.com/facebookgo/stack/stack.go b/Godeps/_workspace/src/github.com/facebookgo/stack/stack.go new file mode 100644 index 000000000..c045daf57 --- /dev/null +++ b/Godeps/_workspace/src/github.com/facebookgo/stack/stack.go @@ -0,0 +1,197 @@ +// Package stack provides utilities to capture and pass around stack traces. +// +// This is useful for building errors that know where they originated from, to +// track where a certain log event occured and so on. +package stack + +import ( + "bytes" + "fmt" + "os" + "path/filepath" + "runtime" + "strings" +) + +const maxStackSize = 32 + +// Frame identifies a file, line & function name in the stack. +type Frame struct { + File string + Line int + Name string +} + +// String provides the standard file:line representation. +func (f Frame) String() string { + return fmt.Sprintf("%s:%d %s", f.File, f.Line, f.Name) +} + +// Stack represents an ordered set of Frames. +type Stack []Frame + +// String provides the standard multi-line stack trace. +func (s Stack) String() string { + var b bytes.Buffer + writeStack(&b, s) + return b.String() +} + +// Multi represents a number of Stacks. This is useful to allow tracking a +// value as it travels thru code. +type Multi struct { + stacks []Stack +} + +// Stacks returns the tracked Stacks. +func (m *Multi) Stacks() []Stack { + return m.stacks +} + +// Add the given Stack to this Multi. +func (m *Multi) Add(s Stack) { + m.stacks = append(m.stacks, s) +} + +// AddCallers adds the Callers Stack to this Multi. The argument skip is +// the number of stack frames to ascend, with 0 identifying the caller of +// Callers. +func (m *Multi) AddCallers(skip int) { + m.Add(Callers(skip + 1)) +} + +// String provides a human readable multi-line stack trace. +func (m *Multi) String() string { + var b bytes.Buffer + for i, s := range m.stacks { + if i != 0 { + fmt.Fprintf(&b, "\n(Stack %d)\n", i+1) + } + writeStack(&b, s) + } + return b.String() +} + +// Caller returns a single Frame for the caller. The argument skip is the +// number of stack frames to ascend, with 0 identifying the caller of Callers. +func Caller(skip int) Frame { + pc, file, line, _ := runtime.Caller(skip + 1) + fun := runtime.FuncForPC(pc) + return Frame{ + File: StripGOPATH(file), + Line: line, + Name: StripPackage(fun.Name()), + } +} + +// Callers returns a Stack of Frames for the callers. The argument skip is the +// number of stack frames to ascend, with 0 identifying the caller of Callers. +func Callers(skip int) Stack { + pcs := make([]uintptr, maxStackSize) + num := runtime.Callers(skip+2, pcs) + stack := make(Stack, num) + for i, pc := range pcs[:num] { + fun := runtime.FuncForPC(pc) + file, line := fun.FileLine(pc) + stack[i].File = StripGOPATH(file) + stack[i].Line = line + stack[i].Name = StripPackage(fun.Name()) + } + return stack +} + +// CallersMulti returns a Multi which includes one Stack for the +// current callers. The argument skip is the number of stack frames to ascend, +// with 0 identifying the caller of CallersMulti. +func CallersMulti(skip int) *Multi { + m := new(Multi) + m.AddCallers(skip + 1) + return m +} + +func writeStack(b *bytes.Buffer, s Stack) { + var width int + for _, f := range s { + if l := len(f.File) + numDigits(f.Line) + 1; l > width { + width = l + } + } + last := len(s) - 1 + for i, f := range s { + b.WriteString(f.File) + b.WriteRune(rune(':')) + n, _ := fmt.Fprintf(b, "%d", f.Line) + for i := width - len(f.File) - n; i != 0; i-- { + b.WriteRune(rune(' ')) + } + b.WriteString(f.Name) + if i != last { + b.WriteRune(rune('\n')) + } + } +} + +func numDigits(i int) int { + var n int + for { + n++ + i = i / 10 + if i == 0 { + return n + } + } +} + +// This can be set by a build script. It will be the colon separated equivalent +// of the environment variable. +var gopath string + +// This is the processed version based on either the above variable set by the +// build or from the GOPATH environment variable. +var gopaths []string + +func init() { + // prefer the variable set at build time, otherwise fallback to the + // environment variable. + if gopath == "" { + gopath = os.Getenv("GOPATH") + } + + for _, p := range strings.Split(gopath, ":") { + if p != "" { + gopaths = append(gopaths, filepath.Join(p, "src")+"/") + } + } + + // Also strip GOROOT for maximum cleanliness + gopaths = append(gopaths, filepath.Join(runtime.GOROOT(), "src", "pkg")+"/") +} + +// StripGOPATH strips the GOPATH prefix from the file path f. +// In development, this will be done using the GOPATH environment variable. +// For production builds, where the GOPATH environment will not be set, the +// GOPATH can be included in the binary by passing ldflags, for example: +// +// GO_LDFLAGS="$GO_LDFLAGS -X github.com/facebookgo/stack.gopath $GOPATH" +// go install "-ldflags=$GO_LDFLAGS" my/pkg +func StripGOPATH(f string) string { + for _, p := range gopaths { + if strings.HasPrefix(f, p) { + return f[len(p):] + } + } + return f +} + +// StripPackage strips the package name from the given Func.Name. +func StripPackage(n string) string { + slashI := strings.LastIndex(n, "/") + if slashI == -1 { + slashI = 0 // for built-in packages + } + dotI := strings.Index(n[slashI:], ".") + if dotI == -1 { + return n + } + return n[slashI+dotI+1:] +} diff --git a/Godeps/_workspace/src/github.com/facebookgo/stack/stack_test.go b/Godeps/_workspace/src/github.com/facebookgo/stack/stack_test.go new file mode 100644 index 000000000..a6bdd880a --- /dev/null +++ b/Godeps/_workspace/src/github.com/facebookgo/stack/stack_test.go @@ -0,0 +1,102 @@ +package stack_test + +import ( + "regexp" + "strings" + "testing" + + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/facebookgo/stack" +) + +func indirect1() stack.Stack { + return stack.Callers(0) +} + +func indirect2() stack.Stack { + return indirect1() +} + +func indirect3() stack.Stack { + return indirect2() +} + +func TestCallers(t *testing.T) { + s := indirect3() + matches := []string{ + "^github.com/facebookgo/stack/stack_test.go:12 +indirect1$", + "^github.com/facebookgo/stack/stack_test.go:16 +indirect2$", + "^github.com/facebookgo/stack/stack_test.go:20 +indirect3$", + "^github.com/facebookgo/stack/stack_test.go:24 +TestCallers$", + } + match(t, s.String(), matches) +} + +func TestCallersMulti(t *testing.T) { + m := stack.CallersMulti(0) + const expected = "github.com/facebookgo/stack/stack_test.go:35 TestCallersMulti" + first := m.Stacks()[0][0].String() + if first != expected { + t.Fatalf(`expected "%s" got "%s"`, expected, first) + } +} + +func TestCallersMultiWithTwo(t *testing.T) { + m := stack.CallersMulti(0) + m.AddCallers(0) + matches := []string{ + "^github.com/facebookgo/stack/stack_test.go:44 +TestCallersMultiWithTwo$", + "", + "", + `^\(Stack 2\)$`, + "^github.com/facebookgo/stack/stack_test.go:46 +TestCallersMultiWithTwo$", + } + match(t, m.String(), matches) +} + +type typ struct{} + +func (m typ) indirect1() stack.Stack { + return stack.Callers(0) +} + +func (m typ) indirect2() stack.Stack { + return m.indirect1() +} + +func (m typ) indirect3() stack.Stack { + return m.indirect2() +} + +func TestCallersWithStruct(t *testing.T) { + var m typ + s := m.indirect3() + matches := []string{ + "^github.com/facebookgo/stack/stack_test.go:59 +typ.indirect1$", + "^github.com/facebookgo/stack/stack_test.go:63 +typ.indirect2$", + "^github.com/facebookgo/stack/stack_test.go:67 +typ.indirect3$", + "^github.com/facebookgo/stack/stack_test.go:72 +TestCallersWithStruct$", + } + match(t, s.String(), matches) +} + +func TestCaller(t *testing.T) { + f := stack.Caller(0) + const expected = "github.com/facebookgo/stack/stack_test.go:83 TestCaller" + if f.String() != expected { + t.Fatalf(`expected "%s" got "%s"`, expected, f) + } +} + +func match(t testing.TB, s string, matches []string) { + lines := strings.Split(s, "\n") + for i, m := range matches { + if !regexp.MustCompile(m).MatchString(lines[i]) { + t.Fatalf( + "did not find expected match \"%s\" on line %d in:\n%s", + m, + i, + s, + ) + } + } +} diff --git a/Godeps/_workspace/src/github.com/facebookgo/stackerr/stackerr.go b/Godeps/_workspace/src/github.com/facebookgo/stackerr/stackerr.go index e0d2e209c..e5f1dde05 100644 --- a/Godeps/_workspace/src/github.com/facebookgo/stackerr/stackerr.go +++ b/Godeps/_workspace/src/github.com/facebookgo/stackerr/stackerr.go @@ -6,7 +6,7 @@ import ( "errors" "fmt" - "github.com/facebookgo/stack" + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/facebookgo/stack" ) // Error provides the wrapper that adds multiple Stacks to an error. Each Stack diff --git a/Godeps/_workspace/src/github.com/facebookgo/stackerr/stackerr_test.go b/Godeps/_workspace/src/github.com/facebookgo/stackerr/stackerr_test.go index a160e675b..fb9d07e6b 100644 --- a/Godeps/_workspace/src/github.com/facebookgo/stackerr/stackerr_test.go +++ b/Godeps/_workspace/src/github.com/facebookgo/stackerr/stackerr_test.go @@ -7,7 +7,7 @@ import ( "strings" "testing" - "github.com/facebookgo/stackerr" + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/facebookgo/stackerr" ) func TestNew(t *testing.T) {