Skip to content

Commit

Permalink
fix(logging): fix direct access to logger (#1980)
Browse files Browse the repository at this point in the history
## Problem
Because the logger provided in the Options can be nil, directly
interacting with it can cause panics.

## Solution
This change fixes this issue by using the various logging methods
provided by Options that wrap the Logger field.
  • Loading branch information
PapaCharlie committed Jul 17, 2023
1 parent 4df8389 commit da1dcac
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (item *Item) yieldItemValue() ([]byte, func(), error) {
db := item.txn.db
result, cb, err := db.vlog.Read(vp, item.slice)
if err != nil {
db.opt.Logger.Errorf("Unable to read: Key: %v, Version : %v, meta: %v, userMeta: %v"+
db.opt.Errorf("Unable to read: Key: %v, Version : %v, meta: %v, userMeta: %v"+
" Error: %v", key, item.version, item.meta, item.userMeta, err)
var txn *Txn
if db.opt.managedTxns {
Expand All @@ -191,7 +191,7 @@ func (item *Item) yieldItemValue() ([]byte, func(), error) {
if item.meta&bitValuePointer > 0 {
vp.Decode(item.vptr)
}
db.opt.Logger.Errorf("Key: %v, Version : %v, meta: %v, userMeta: %v valuePointer: %+v",
db.opt.Errorf("Key: %v, Version : %v, meta: %v, userMeta: %v valuePointer: %+v",
item.Key(), item.version, item.meta, item.userMeta, vp)
}
}
Expand Down
2 changes: 1 addition & 1 deletion stream_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (sw *StreamWriter) Write(buf *z.Buffer) error {
for streamId := range closedStreams {
writer, ok := sw.writers[streamId]
if !ok {
sw.db.opt.Logger.Warningf("Trying to close stream: %d, but no sorted "+
sw.db.opt.Warningf("Trying to close stream: %d, but no sorted "+
"writer found for it", streamId)
continue
}
Expand Down
2 changes: 1 addition & 1 deletion value.go
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ func (vlog *valueLog) Read(vp valuePointer, _ *y.Slice) ([]byte, func(), error)
}
}
if uint32(len(kv)) < h.klen+h.vlen {
vlog.db.opt.Logger.Errorf("Invalid read: vp: %+v", vp)
vlog.db.opt.Errorf("Invalid read: vp: %+v", vp)
return nil, nil, errors.Errorf("Invalid read: Len: %d read at:[%d:%d]",
len(kv), h.klen, h.klen+h.vlen)
}
Expand Down

0 comments on commit da1dcac

Please sign in to comment.