Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Add interface for custom source code loaders #207

Merged
merged 2 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func NewStacktraceFrame(pc uintptr, file string, line, context int, appPackagePr
}

if context > 0 {
contextLines, lineIdx := fileContext(file, line, context)
contextLines, lineIdx := sourceCodeLoader.Load(file, line, context)
if len(contextLines) > 0 {
for i, line := range contextLines {
switch {
Expand All @@ -157,7 +157,7 @@ func NewStacktraceFrame(pc uintptr, file string, line, context int, appPackagePr
}
}
} else if context == -1 {
contextLine, _ := fileContext(file, line, 0)
contextLine, _ := sourceCodeLoader.Load(file, line, 0)
if len(contextLine) > 0 {
frame.ContextLine = string(contextLine[0])
}
Expand Down Expand Up @@ -191,24 +191,36 @@ func splitFunctionName(name string) (string, string) {
return pack, name
}

var fileCacheLock sync.Mutex
var fileCache = make(map[string][][]byte)
type SourceCodeLoader interface {
Load(filename string, line, context int) ([][]byte, int)
}

var sourceCodeLoader SourceCodeLoader = fsLoader{cache: make(map[string][][]byte)}

func SetSourceCodeLoader(loader SourceCodeLoader) {
sourceCodeLoader = loader
}

type fsLoader struct {
mu sync.Mutex
cache map[string][][]byte
}

func fileContext(filename string, line, context int) ([][]byte, int) {
fileCacheLock.Lock()
defer fileCacheLock.Unlock()
lines, ok := fileCache[filename]
func (fs fsLoader) Load(filename string, line, context int) ([][]byte, int) {
fs.mu.Lock()
defer fs.mu.Unlock()
lines, ok := fs.cache[filename]
if !ok {
data, err := ioutil.ReadFile(filename)
if err != nil {
// cache errors as nil slice: code below handles it correctly
// otherwise when missing the source or running as a different user, we try
// reading the file on each error which is unnecessary
fileCache[filename] = nil
fs.cache[filename] = nil
return nil, 0
}
lines = bytes.Split(data, []byte{'\n'})
fileCache[filename] = lines
fs.cache[filename] = lines
}

if lines == nil {
Expand Down
13 changes: 7 additions & 6 deletions stacktrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func TestFunctionName(t *testing.T) {
}

func TestSplitFunctionName(t *testing.T) {
tests := []struct{
in string
tests := []struct {
in string
pack, name string
}{
{"", "", ""},
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestNewStacktrace_noFrames(t *testing.T) {

func TestFileContext(t *testing.T) {
// reset the cache
fileCache = make(map[string][][]byte)
sourceCodeLoader = fsLoader{cache: make(map[string][][]byte)}

tempdir, err := ioutil.TempDir("", "")
if err != nil {
Expand Down Expand Up @@ -200,13 +200,14 @@ func TestFileContext(t *testing.T) {
{noPermissionPath, 0, 0},
}
for i, test := range tests {
lines, index := fileContext(test.path, 1, 0)
lines, index := sourceCodeLoader.Load(test.path, 1, 0)
if !(len(lines) == test.expectedLines && index == test.expectedIndex) {
t.Errorf("%d: fileContext(%#v, 1, 0) = %v, %v; expected len()=%d, %d",
i, test.path, lines, index, test.expectedLines, test.expectedIndex)
}
if len(fileCache) != i+1 {
t.Errorf("%d: result was not cached; len(fileCached)=%d", i, len(fileCache))
cacheLen := len(sourceCodeLoader.(fsLoader).cache)
if cacheLen != i+1 {
t.Errorf("%d: result was not cached; len=%d", i, cacheLen)
}
}
}