Skip to content

Commit

Permalink
Merge pull request #16 from Bose/write-err-fix
Browse files Browse the repository at this point in the history
make it easier to find buffer overruns by returning an error with the complete unwritten buffer
  • Loading branch information
Jim Lambert authored May 15, 2019
2 parents 00154a8 + 6f23e83 commit ef68466
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"
"os"
"runtime"
"time"
Expand Down Expand Up @@ -82,7 +83,9 @@ func main() {
c.JSON(200, "Hello world!")
})

r.Run(":29090")
if err := r.Run(":29090"); err != nil {
log.Println("Run error: ", err)
}
}

func newSpanFromContext(c *gin.Context, operationName string) opentracing.Span {
Expand Down
2 changes: 1 addition & 1 deletion logBuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (b *LogBuffer) Write(data []byte) (n int, err error) {
newData := bytes.TrimSuffix(data, []byte("\n"))

if len(newData)+b.Buff.Len() > int(b.MaxSize) {
return 0, fmt.Errorf("write failed: buffer MaxSize = %d, current len = %d, attempted to write len = %d", b.MaxSize, b.Buff.Len(), len(newData))
return 0, fmt.Errorf("write failed: buffer MaxSize = %d, current len = %d, attempted to write len = %d, data == %s", b.MaxSize, b.Buff.Len(), len(newData), newData)
}
return b.Buff.Write(append(newData, []byte(",")...))
}
Expand Down
3 changes: 3 additions & 0 deletions logBuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ func TestLogBuffer_Write(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotN, err := tt.b.Write(tt.data)
if err != nil {
t.Log("LogBuffer.Write() error == ", err)
}
if (err != nil) != tt.wantErr {
t.Errorf("LogBuffer.Write() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit ef68466

Please sign in to comment.