Browse Source

fix line number (#25)

* fix line number

* fix tests
tags/v2.0.1
Achille 2 years ago
parent
commit
7f4f914232
5 changed files with 33 additions and 9 deletions
  1. 1
    1
      ecslogs/handler_test.go
  2. 4
    7
      source.go
  3. 12
    0
      source_default.go
  4. 15
    0
      source_go17.go
  5. 1
    1
      source_test.go

+ 1
- 1
ecslogs/handler_test.go View File

@@ -43,7 +43,7 @@ func TestHandler(t *testing.T) {
b.Reset()
h.HandleEvent(e)

if s := b.String(); s != `{"level":"ERROR","time":"2017-01-01T23:42:00.123Z","info":{"source":"github.com/segmentio/events/ecslogs/handler_test.go:19","errors":[{"type":"*errors.errorString","error":"EOF","stack":["github.com/segmentio/events/ecslogs/handler_test.go:41","testing/testing.go:611","runtime/asm_amd64.s:2087"]}]},"data":{"name":"Luke","from":"Han"},"message":"Hello Luke!"}
if s := b.String(); s != `{"level":"ERROR","time":"2017-01-01T23:42:00.123Z","info":{"source":"github.com/segmentio/events/ecslogs/handler_test.go:19","errors":[{"type":"*errors.errorString","error":"EOF","stack":["github.com/segmentio/events/ecslogs/handler_test.go:41","testing/testing.go:657","runtime/asm_amd64.s:2197"]}]},"data":{"name":"Luke","from":"Han"},"message":"Hello Luke!"}
` {
// This test is sensitive, it may break if the Go version changes or
// if this file is edited (because the number of lines may not be

+ 4
- 7
source.go View File

@@ -1,17 +1,14 @@
package events

import (
"runtime"
"strings"
)
import "strings"

// SourceForPC returns the file and line given a program counter address.
// The file path is in the canonical form for Go programs, starting with
// the package path.
func SourceForPC(pc uintptr) (file string, line int) {
fn := runtime.FuncForPC(pc)
file, line = fn.FileLine(pc)
file = trimGOPATH(fn.Name(), file)
var name string
file, line, name = fileLineFunc(pc)
file = trimGOPATH(name, file)
return
}


+ 12
- 0
source_default.go View File

@@ -0,0 +1,12 @@
// +build !go1.7

package events

import "runtime"

func fileLineFunc(pc uintptr) (file string, line int, name string) {
fn := runtime.FuncForPC(pc)
file, line = fn.FileLine(pc)
name = fn.Name()
return
}

+ 15
- 0
source_go17.go View File

@@ -0,0 +1,15 @@
// +build go1.7

package events

import "runtime"

func fileLineFunc(pc uintptr) (file string, line int, name string) {
caller := [1]uintptr{pc}
frames := runtime.CallersFrames(caller[:])
f, _ := frames.Next()
file = f.File
line = f.Line
name = f.Func.Name()
return
}

+ 1
- 1
source_test.go View File

@@ -15,7 +15,7 @@ func TestSourceForPC(t *testing.T) {
t.Error("bad file:", file)
}

if line != 12 {
if line != 10 {
t.Error("bad line:", line)
}
}

Loading…
Cancel
Save