Improve logging and error handling

- enable logging by default: debug was a bad name - it's error logging that I
  just want to hide in tests
- don't panic (all the time)
- use a logger. this allows us to add more information - like the path of the
  parsed file!
This commit is contained in:
Niklas Fasching 2018-12-19 20:04:12 +01:00
parent c23f8cc281
commit 7a8e90f786
5 changed files with 28 additions and 29 deletions

View file

@ -4,7 +4,9 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
)
@ -19,7 +21,7 @@ type Document struct {
BufferSettings map[string]string
DefaultSettings map[string]string
Error error
Debug bool
Log *log.Logger
}
type Writer interface {
@ -80,6 +82,7 @@ func NewDocument() *Document {
DefaultSettings: map[string]string{
"TODO": "TODO | DONE",
},
Log: log.New(os.Stderr, "go-org: ", 0),
}
}
@ -118,6 +121,12 @@ func (dIn *Document) Parse(input io.Reader) (d *Document) {
func (d *Document) SetPath(path string) *Document {
d.Path = path
d.Log.SetPrefix(fmt.Sprintf("%s(%s): ", d.Log.Prefix(), path))
return d
}
func (d *Document) Silent() *Document {
d.Log = log.New(ioutil.Discard, "", 0)
return d
}
@ -196,9 +205,7 @@ func (d *Document) parseOne(i int, stop stopFn) (consumed int, node Node) {
if consumed != 0 {
return consumed, node
}
if d.Debug {
log.Printf("Could not parse token %#v: Falling back to treating it as plain text.", d.tokens[i])
}
d.Log.Printf("Could not parse token %#v: Falling back to treating it as plain text.", d.tokens[i])
m := plainTextRegexp.FindStringSubmatch(d.tokens[i].matches[0])
d.tokens[i] = token{"text", len(m[1]), m[2], m}
return d.parseOne(i, stop)

View file

@ -16,7 +16,7 @@ type HTMLWriter struct {
HighlightCodeBlock func(source, lang string) string
FootnotesHeadingTitle string
htmlEscape bool
debug bool
log *log.Logger
}
var emphasisTags = map[string][]string{
@ -65,7 +65,7 @@ func (w *HTMLWriter) nodesAsString(nodes ...Node) string {
}
func (w *HTMLWriter) before(d *Document) {
w.debug = w.debug || d.Debug
w.log = d.Log
}
func (w *HTMLWriter) after(d *Document) {
@ -362,7 +362,7 @@ func (w *HTMLWriter) writeNodeWithMeta(n NodeWithMeta) {
}
}
for _, attributes := range n.Meta.HTMLAttributes {
out = withHTMLAttributes(w.debug, out, attributes...) + "\n"
out = w.withHTMLAttributes(out, attributes...) + "\n"
}
if len(n.Meta.Caption) != 0 {
caption := ""
@ -414,19 +414,15 @@ func (w *HTMLWriter) writeTableColumns(columns []Column, tag string) {
w.WriteString("</tr>\n")
}
func withHTMLAttributes(debug bool, input string, kvs ...string) string {
func (w *HTMLWriter) withHTMLAttributes(input string, kvs ...string) string {
if len(kvs)%2 != 0 {
if debug {
log.Printf("withHTMLAttributes: Len of kvs must be even: %#v", kvs)
}
w.log.Printf("withHTMLAttributes: Len of kvs must be even: %#v", kvs)
return input
}
context := &h.Node{Type: h.ElementNode, Data: "body", DataAtom: atom.Body}
nodes, err := h.ParseFragment(strings.NewReader(strings.TrimSpace(input)), context)
if err != nil || len(nodes) != 1 {
if debug {
log.Printf("withHTMLAttributes: Could not extend attributes of %s: %v (%s)", input, nodes, err)
}
w.log.Printf("withHTMLAttributes: Could not extend attributes of %s: %v (%s)", input, nodes, err)
return input
}
out, node := strings.Builder{}, nodes[0]
@ -435,9 +431,7 @@ func withHTMLAttributes(debug bool, input string, kvs ...string) string {
}
err = h.Render(&out, nodes[0])
if err != nil {
if debug {
log.Printf("withHTMLAttributes: Could not extend attributes of %s: %v (%s)", input, node, err)
}
w.log.Printf("withHTMLAttributes: Could not extend attributes of %s: %v (%s)", input, node, err)
return input
}
return out.String()

View file

@ -9,7 +9,7 @@ func TestHTMLWriter(t *testing.T) {
for _, path := range orgTestFiles() {
expected := fileString(path[:len(path)-len(".org")] + ".html")
reader, writer := strings.NewReader(fileString(path)), NewHTMLWriter()
actual, err := NewDocument().SetPath(path).Parse(reader).Write(writer)
actual, err := NewDocument().Silent().SetPath(path).Parse(reader).Write(writer)
if err != nil {
t.Errorf("%s\n got error: %s", path, err)
continue

View file

@ -2,9 +2,7 @@ package org
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"regexp"
"strings"
@ -119,7 +117,10 @@ func parseKeyword(t token) Keyword {
}
func (d *Document) newInclude(k Keyword) (int, Node) {
resolve := func() Node { panic(fmt.Sprintf("bad include: '#+INCLUDE: %s'", k.Value)) }
resolve := func() Node {
d.Log.Printf("Bad include %#v", k)
return k
}
if m := includeFileRegexp.FindStringSubmatch(k.Value); m != nil {
path, kind, lang := m[1], m[2], m[3]
if !filepath.IsAbs(path) {
@ -128,7 +129,8 @@ func (d *Document) newInclude(k Keyword) (int, Node) {
resolve = func() Node {
bs, err := ioutil.ReadFile(path)
if err != nil {
panic(fmt.Sprintf("bad include '#+INCLUDE: %s': %s", k.Value, err))
d.Log.Printf("Bad include %#v: %s", k, err)
return k
}
return Block{strings.ToUpper(kind), []string{lang}, d.parseRawInline(string(bs))}
}
@ -143,16 +145,12 @@ func (d *Document) loadSetupFile(k Keyword) (int, Node) {
}
bs, err := ioutil.ReadFile(path)
if err != nil {
if d.Debug {
log.Printf("Bad setup file: %#v: %s", k, err)
}
d.Log.Printf("Bad setup file: %#v: %s", k, err)
return 1, k
}
setupDocument := NewDocument().Parse(bytes.NewReader(bs))
if err := setupDocument.Error; err != nil {
if d.Debug {
log.Printf("Bad setup file: %#v: %s", k, err)
}
d.Log.Printf("Bad setup file: %#v: %s", k, err)
return 1, k
}
for k, v := range setupDocument.BufferSettings {

View file

@ -14,7 +14,7 @@ func TestOrgWriter(t *testing.T) {
for _, path := range orgTestFiles() {
expected := fileString(path[:len(path)-len(".org")] + ".pretty_org")
reader, writer := strings.NewReader(fileString(path)), NewOrgWriter()
actual, err := NewDocument().SetPath(path).Parse(reader).Write(writer)
actual, err := NewDocument().Silent().SetPath(path).Parse(reader).Write(writer)
if err != nil {
t.Errorf("%s\n got error: %s", path, err)
continue