common/herrors: Make the file error log format configurable
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 27 Oct 2018 15:19:36 +0000 (17:19 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 27 Oct 2018 17:14:16 +0000 (19:14 +0200)
Will check for an OS environment variable named `HUGO_FILE_LOG_FORMAT`.

The tokens available are `:file` (absolute filename), `:line` (line number) and `:col` (column number).

The default format is: \":file::line::col\"`

How to set OS environment variables is OS specific, but an example on Unix setting an alternative format when running Hugo:

```bash
env HUGO_FILE_LOG_FORMAT="\":file::line\"" hugo
```

The above will include filename and line number quoted.

Fixes #5352

common/herrors/error_locator.go
common/herrors/error_locator_test.go

index cb59b65a3b83b32e003e0d9b6b33b2f88201f8fa..5d3f079be6e93ca948ee8bae46b209eb08a69ab4 100644 (file)
@@ -27,12 +27,55 @@ import (
        "github.com/spf13/afero"
 )
 
-var fileErrorFormat = "\"%s:%d:%d\": %s"
+var fileErrorFormatFunc func(e ErrorContext) string
 
-func init() {
-       if terminal.IsTerminal(os.Stdout) {
-               fileErrorFormat = terminal.Notice("\"%s:%d:%d\"") + ": %s"
+func createFileLogFormatter(formatStr string) func(e ErrorContext) string {
+
+       if formatStr == "" {
+               formatStr = "\":file::line::col\""
+       }
+
+       var identifiers = []string{":file", ":line", ":col"}
+       var identifiersFound []string
+
+       for i := range formatStr {
+               for _, id := range identifiers {
+                       if strings.HasPrefix(formatStr[i:], id) {
+                               identifiersFound = append(identifiersFound, id)
+                       }
+               }
        }
+
+       replacer := strings.NewReplacer(":file", "%s", ":line", "%d", ":col", "%d")
+       format := replacer.Replace(formatStr)
+
+       f := func(e ErrorContext) string {
+               args := make([]interface{}, len(identifiersFound))
+               for i, id := range identifiersFound {
+                       switch id {
+                       case ":file":
+                               args[i] = e.Filename
+                       case ":line":
+                               args[i] = e.LineNumber
+                       case ":col":
+                               args[i] = e.ColumnNumber
+                       }
+               }
+
+               msg := fmt.Sprintf(format, args...)
+
+               if terminal.IsTerminal(os.Stdout) {
+                       return terminal.Notice(msg)
+               }
+
+               return msg
+       }
+
+       return f
+}
+
+func init() {
+       fileErrorFormatFunc = createFileLogFormatter(os.Getenv("HUGO_FILE_LOG_FORMAT"))
 }
 
 // LineMatcher contains the elements used to match an error to a line
@@ -85,7 +128,7 @@ type ErrorWithFileContext struct {
 }
 
 func (e *ErrorWithFileContext) Error() string {
-       return fmt.Sprintf(fileErrorFormat, e.Filename, e.LineNumber, e.ColumnNumber, e.cause.Error())
+       return fileErrorFormatFunc(e.ErrorContext) + ": " + e.cause.Error()
 }
 
 func (e *ErrorWithFileContext) Cause() error {
index e7bc3cb190ef813e81f9fbf7949ff858b1da1445..84c0faf89ab7e7369818585e67310e7f8ad3f9b1 100644 (file)
@@ -21,6 +21,18 @@ import (
        "github.com/stretchr/testify/require"
 )
 
+func TestCreateFileLogFormatter(t *testing.T) {
+       assert := require.New(t)
+
+       ctx := ErrorContext{Filename: "/my/file.txt", LineNumber: 12, ColumnNumber: 13}
+
+       assert.Equal("/my/file.txt|13|12", createFileLogFormatter(":file|:col|:line")(ctx))
+       assert.Equal("13|/my/file.txt|12", createFileLogFormatter(":col|:file|:line")(ctx))
+       assert.Equal("好:13", createFileLogFormatter("好::col")(ctx))
+       assert.Equal("\"/my/file.txt:12:13\"", createFileLogFormatter("")(ctx))
+
+}
+
 func TestErrorLocator(t *testing.T) {
        assert := require.New(t)