2016-11-15 20:30:34 +01:00
|
|
|
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
|
|
|
|
package diag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2016-11-17 02:52:14 +01:00
|
|
|
"io"
|
2016-11-15 20:30:34 +01:00
|
|
|
"os"
|
2016-11-16 03:00:43 +01:00
|
|
|
"path/filepath"
|
2016-11-15 20:30:34 +01:00
|
|
|
"strconv"
|
2016-11-16 01:30:10 +01:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2017-02-10 02:26:49 +01:00
|
|
|
"github.com/reconquest/loreley"
|
|
|
|
|
|
|
|
"github.com/marapongo/mu/pkg/util/contract"
|
2016-11-15 20:30:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Sink facilitates pluggable diagnostics messages.
|
|
|
|
type Sink interface {
|
|
|
|
// Count fetches the total number of diagnostics issued (errors plus warnings).
|
|
|
|
Count() int
|
|
|
|
// Errors fetches the number of errors issued.
|
|
|
|
Errors() int
|
|
|
|
// Warnings fetches the number of warnings issued.
|
|
|
|
Warnings() int
|
2016-11-22 18:40:09 +01:00
|
|
|
// Success returns true if this sink is currently error-free.
|
|
|
|
Success() bool
|
2016-11-15 20:30:34 +01:00
|
|
|
|
|
|
|
// Error issues a new error diagnostic.
|
|
|
|
Errorf(diag *Diag, args ...interface{})
|
|
|
|
// Warning issues a new warning diagnostic.
|
|
|
|
Warningf(diag *Diag, args ...interface{})
|
2016-11-16 04:16:02 +01:00
|
|
|
|
|
|
|
// Stringify stringifies a diagnostic in the usual way (e.g., "error: MU123: Mu.yaml:7:39: error goes here\n").
|
2017-02-10 02:26:49 +01:00
|
|
|
Stringify(diag *Diag, cat Category, args ...interface{}) string
|
2017-02-12 18:38:19 +01:00
|
|
|
// StringifyLocation stringifies a source document location.
|
|
|
|
StringifyLocation(doc *Document, loc *Location) string
|
2017-02-10 02:26:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Category dictates the kind of diagnostic.
|
|
|
|
type Category string
|
|
|
|
|
|
|
|
const (
|
|
|
|
Error Category = "error"
|
|
|
|
Warning = "warning"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FormatOptions controls the output style and content.
|
|
|
|
type FormatOptions struct {
|
|
|
|
Pwd string // the working directory.
|
|
|
|
Colors bool // if true, output will be colorized.
|
2016-11-15 20:30:34 +01:00
|
|
|
}
|
|
|
|
|
2017-01-28 00:42:39 +01:00
|
|
|
// DefaultSink returns a default sink that simply logs output to stderr/stdout.
|
2017-02-10 02:26:49 +01:00
|
|
|
func DefaultSink(opts FormatOptions) Sink {
|
|
|
|
return newDefaultSink(opts, os.Stderr, os.Stdout)
|
2016-11-17 02:52:14 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 02:26:49 +01:00
|
|
|
func newDefaultSink(opts FormatOptions, errorW io.Writer, warningW io.Writer) *defaultSink {
|
|
|
|
return &defaultSink{opts: opts, errorW: errorW, warningW: warningW}
|
2016-11-15 20:30:34 +01:00
|
|
|
}
|
|
|
|
|
2016-11-16 17:19:26 +01:00
|
|
|
const DefaultSinkIDPrefix = "MU"
|
|
|
|
|
2016-11-16 01:30:10 +01:00
|
|
|
// defaultSink is the default sink which logs output to stderr/stdout.
|
|
|
|
type defaultSink struct {
|
2017-02-10 02:26:49 +01:00
|
|
|
opts FormatOptions // a set of options that control output style and content.
|
|
|
|
errors int // the number of errors that have been issued.
|
|
|
|
errorW io.Writer // the output stream to use for errors.
|
|
|
|
warnings int // the number of warnings that have been issued.
|
|
|
|
warningW io.Writer // the output stream to use for warnings.
|
2016-11-15 20:30:34 +01:00
|
|
|
}
|
|
|
|
|
2016-11-16 01:30:10 +01:00
|
|
|
func (d *defaultSink) Count() int {
|
2016-11-15 20:30:34 +01:00
|
|
|
return d.errors + d.warnings
|
|
|
|
}
|
|
|
|
|
2016-11-16 01:30:10 +01:00
|
|
|
func (d *defaultSink) Errors() int {
|
2016-11-15 20:30:34 +01:00
|
|
|
return d.errors
|
|
|
|
}
|
|
|
|
|
2016-11-16 01:30:10 +01:00
|
|
|
func (d *defaultSink) Warnings() int {
|
2016-11-15 20:30:34 +01:00
|
|
|
return d.warnings
|
|
|
|
}
|
|
|
|
|
2016-11-22 18:40:09 +01:00
|
|
|
func (d *defaultSink) Success() bool {
|
|
|
|
return d.errors == 0
|
|
|
|
}
|
|
|
|
|
2016-11-16 01:30:10 +01:00
|
|
|
func (d *defaultSink) Errorf(diag *Diag, args ...interface{}) {
|
2017-02-10 02:26:49 +01:00
|
|
|
msg := d.Stringify(diag, Error, args...)
|
2016-11-16 01:30:10 +01:00
|
|
|
if glog.V(3) {
|
2016-11-22 18:20:23 +01:00
|
|
|
glog.V(3).Infof("defaultSink::Error(%v)", msg[:len(msg)-1])
|
2016-11-16 01:30:10 +01:00
|
|
|
}
|
2016-11-17 02:52:14 +01:00
|
|
|
fmt.Fprintf(d.errorW, msg)
|
|
|
|
d.errors++
|
2016-11-15 20:30:34 +01:00
|
|
|
}
|
|
|
|
|
2016-11-16 01:30:10 +01:00
|
|
|
func (d *defaultSink) Warningf(diag *Diag, args ...interface{}) {
|
2017-02-10 02:26:49 +01:00
|
|
|
msg := d.Stringify(diag, Warning, args...)
|
2016-11-16 01:30:10 +01:00
|
|
|
if glog.V(4) {
|
2016-11-22 18:20:23 +01:00
|
|
|
glog.V(4).Infof("defaultSink::Warning(%v)", msg[:len(msg)-1])
|
2016-11-16 01:30:10 +01:00
|
|
|
}
|
2016-11-17 02:52:14 +01:00
|
|
|
fmt.Fprintf(d.warningW, msg)
|
|
|
|
d.warnings++
|
2016-11-15 20:30:34 +01:00
|
|
|
}
|
|
|
|
|
2017-02-16 13:15:07 +01:00
|
|
|
const colorLeft = "<{%"
|
|
|
|
const colorRight = "%}>"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Change the Loreley delimiters from { and }, to something more complex, to avoid accidental collisions.
|
|
|
|
loreley.DelimLeft = colorLeft
|
|
|
|
loreley.DelimRight = colorRight
|
|
|
|
}
|
|
|
|
|
|
|
|
func colorString(s string) string {
|
|
|
|
return colorLeft + s + colorRight
|
|
|
|
}
|
|
|
|
|
2017-02-10 02:26:49 +01:00
|
|
|
func (d *defaultSink) Stringify(diag *Diag, cat Category, args ...interface{}) string {
|
2016-11-15 20:30:34 +01:00
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
2017-02-10 02:26:49 +01:00
|
|
|
// First print the location if there is one.
|
2017-02-12 18:38:19 +01:00
|
|
|
if diag.Doc != nil || diag.Loc != nil {
|
|
|
|
buffer.WriteString(d.StringifyLocation(diag.Doc, diag.Loc))
|
2016-11-15 20:30:34 +01:00
|
|
|
buffer.WriteString(": ")
|
2017-02-10 02:26:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now print the message category's prefix (error/warning).
|
|
|
|
if d.opts.Colors {
|
|
|
|
switch cat {
|
|
|
|
case Error:
|
2017-02-16 13:15:07 +01:00
|
|
|
buffer.WriteString(colorString("fg 1")) // red
|
2017-02-10 02:26:49 +01:00
|
|
|
case Warning:
|
2017-02-16 13:15:07 +01:00
|
|
|
buffer.WriteString(colorString("fg 11")) // bright yellow
|
2017-02-10 02:26:49 +01:00
|
|
|
default:
|
|
|
|
contract.Failf("Unrecognized diagnostic category: %v", cat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.WriteString(string(cat))
|
|
|
|
|
|
|
|
if diag.ID > 0 {
|
|
|
|
buffer.WriteString(" ")
|
|
|
|
buffer.WriteString(DefaultSinkIDPrefix)
|
|
|
|
buffer.WriteString(strconv.Itoa(int(diag.ID)))
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.WriteString(": ")
|
|
|
|
|
|
|
|
if d.opts.Colors {
|
2017-02-16 13:15:07 +01:00
|
|
|
buffer.WriteString(colorString("reset"))
|
2017-02-10 02:26:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, actually print the message itself.
|
|
|
|
if d.opts.Colors {
|
2017-02-16 13:15:07 +01:00
|
|
|
buffer.WriteString(colorString("fg 7")) // white
|
2016-11-15 20:30:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer.WriteString(fmt.Sprintf(diag.Message, args...))
|
2017-02-10 02:26:49 +01:00
|
|
|
|
|
|
|
if d.opts.Colors {
|
2017-02-16 13:15:07 +01:00
|
|
|
buffer.WriteString(colorString("reset"))
|
2017-02-10 02:26:49 +01:00
|
|
|
}
|
|
|
|
|
2016-11-15 20:30:34 +01:00
|
|
|
buffer.WriteRune('\n')
|
|
|
|
|
2016-11-23 21:30:02 +01:00
|
|
|
// TODO[marapongo/mu#15]: support Clang-style expressive diagnostics. This would entail, for example, using the
|
|
|
|
// buffer within the target document, to demonstrate the offending line/column range of code.
|
2016-11-15 20:30:34 +01:00
|
|
|
|
2017-02-10 02:26:49 +01:00
|
|
|
s := buffer.String()
|
|
|
|
|
|
|
|
// If colorization was requested, compile and execute the directives now.
|
|
|
|
if d.opts.Colors {
|
|
|
|
var err error
|
|
|
|
s, err = loreley.CompileAndExecuteToString(s, nil, nil)
|
2017-02-16 13:15:07 +01:00
|
|
|
contract.Assertf(err == nil, "Expected no errors during string format operation; str=%v, err=%v", s, err)
|
2017-02-10 02:26:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
2016-11-15 20:30:34 +01:00
|
|
|
}
|
2017-02-12 18:38:19 +01:00
|
|
|
|
|
|
|
func (d *defaultSink) StringifyLocation(doc *Document, loc *Location) string {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
|
|
|
if doc != nil {
|
|
|
|
if d.opts.Colors {
|
2017-02-16 13:15:07 +01:00
|
|
|
buffer.WriteString(colorString("fg 6")) // cyan
|
2017-02-12 18:38:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
file := doc.File
|
|
|
|
if d.opts.Pwd != "" {
|
|
|
|
// If a PWD is available, try to create a relative path.
|
|
|
|
rel, err := filepath.Rel(d.opts.Pwd, file)
|
|
|
|
if err == nil {
|
|
|
|
file = rel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer.WriteString(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
if loc != nil && !loc.IsEmpty() {
|
|
|
|
buffer.WriteRune('(')
|
|
|
|
buffer.WriteString(strconv.Itoa(loc.Start.Line))
|
|
|
|
buffer.WriteRune(',')
|
|
|
|
buffer.WriteString(strconv.Itoa(loc.Start.Column))
|
|
|
|
buffer.WriteRune(')')
|
|
|
|
}
|
|
|
|
|
|
|
|
var s string
|
|
|
|
if doc != nil || loc != nil {
|
|
|
|
if d.opts.Colors {
|
2017-02-16 13:15:07 +01:00
|
|
|
buffer.WriteString(colorString("reset"))
|
2017-02-12 18:38:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s = buffer.String()
|
|
|
|
|
|
|
|
// If colorization was requested, compile and execute the directives now.
|
|
|
|
if d.opts.Colors {
|
|
|
|
var err error
|
|
|
|
s, err = loreley.CompileAndExecuteToString(s, nil, nil)
|
2017-02-16 13:15:07 +01:00
|
|
|
contract.Assertf(err == nil, "Expected no errors during string format operation; str=%v, err=%v", s, err)
|
2017-02-12 18:38:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|