now i'm using zap

main
Nick Dumas 1 year ago
parent fa80f3ceea
commit 4748dab2ff

@ -3,10 +3,11 @@ package wikilink
import ( import (
"fmt" "fmt"
"log"
"strings" "strings"
// "unicode" // "unicode"
"unicode/utf8" "unicode/utf8"
"go.uber.org/zap"
) )
const ( const (
@ -35,6 +36,7 @@ const (
func Lex(name, input string) *Lexer { func Lex(name, input string) *Lexer {
l := &Lexer{ l := &Lexer{
L: zap.NewExample().Sugar().Named("lexer"),
name: name, name: name,
input: input, input: input,
state: lexText, state: lexText,
@ -72,6 +74,7 @@ func (l *Lexer) backup() {
} }
type Lexer struct { type Lexer struct {
L *zap.SugaredLogger
name, input string name, input string
start, pos, width int start, pos, width int
state stateFn state stateFn
@ -103,18 +106,29 @@ func (l *Lexer) acceptRun(valid string) {
func (l *Lexer) emit(t ItemType) { func (l *Lexer) emit(t ItemType) {
i := Item{t, l.input[l.start:l.pos]} i := Item{t, l.input[l.start:l.pos]}
log.Printf("emitting Item: %s\n", i.String()) L := l.L.With(
zap.Int("pos", l.pos),
zap.Int("width", l.width),
).Named("emit")
L.Debugw("emitting item",
zap.String("item", i.String()),
)
l.items <- i l.items <- i
l.start = l.pos l.start = l.pos
} }
func (l *Lexer) errorf(format string, args ...interface{}) stateFn { func (l *Lexer) errorf(format string, args ...interface{}) stateFn {
log.Printf("entering errorf: %q\n", format) L := l.L.Named("errorf")
l.items <- Item{ errorItem := Item{
ItemError, ItemError,
fmt.Sprintf(format, args...), fmt.Sprintf(format, args...),
} }
L.Debugw("emitting errorItem",
zap.String("error", errorItem.String()),
)
l.items <- errorItem
return nil return nil
} }

@ -93,6 +93,7 @@ func Test_Lexer(t *testing.T) {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
// t.Parallel() // t.Parallel()
l := wikilink.Lex("testLexer", tc.in) l := wikilink.Lex("testLexer", tc.in)
defer l.L.Sync()
for _, e := range tc.expected { for _, e := range tc.expected {
n := l.NextItem() n := l.NextItem()
if e.Typ != n.Typ { if e.Typ != n.Typ {

Loading…
Cancel
Save