| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package report |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
|
| 20 |
"github.com/coreos/vcontext/path" |
| 21 |
"github.com/coreos/vcontext/tree" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
type EntryKind interface { |
| 26 |
String() string |
| 27 |
IsFatal() bool |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
type Report struct { |
| 32 |
Entries []Entry |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
func (r *Report) Merge(child Report) { |
| 37 |
r.Entries = append(r.Entries, child.Entries...) |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
func getDeepestNode(n tree.Node, c path.ContextPath) tree.Node { |
| 42 |
if child, err := n.Get(c); err != nil { |
| 43 |
return getDeepestNode(n, c.Pop()) |
| 44 |
} else { |
| 45 |
return child |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
func (r *Report) Correlate(n tree.Node) { |
| 52 |
for i, e := range r.Entries { |
| 53 |
r.Entries[i].Marker = getDeepestNode(n, e.Context).GetMarker() |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
func (r Report) IsFatal() bool { |
| 59 |
for _, e := range r.Entries { |
| 60 |
if e.Kind.IsFatal() { |
| 61 |
return true |
| 62 |
} |
| 63 |
} |
| 64 |
return false |
| 65 |
} |
| 66 |
|
| 67 |
func (r Report) String() string { |
| 68 |
str := "" |
| 69 |
for _, e := range r.Entries { |
| 70 |
str += e.String() + "\n" |
| 71 |
} |
| 72 |
return str |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
type Entry struct { |
| 77 |
|
| 78 |
Kind EntryKind |
| 79 |
Message string |
| 80 |
|
| 81 |
|
| 82 |
Context path.ContextPath |
| 83 |
|
| 84 |
|
| 85 |
Marker tree.Marker |
| 86 |
} |
| 87 |
|
| 88 |
func (e Entry) String() string { |
| 89 |
at := "" |
| 90 |
switch { |
| 91 |
case e.Marker.StartP != nil && e.Context.Len() != 0: |
| 92 |
at = fmt.Sprintf(" at %s, %s", e.Context.String(), e.Marker.String()) |
| 93 |
case e.Marker.StartP != nil: |
| 94 |
at = fmt.Sprintf(" at %s", e.Marker.String()) |
| 95 |
case e.Context.Len() != 0: |
| 96 |
at = fmt.Sprintf(" at %s", e.Context.String()) |
| 97 |
} |
| 98 |
|
| 99 |
return fmt.Sprintf("%s%s: %s", e.Kind.String(), at, e.Message) |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
type Kind int |
| 104 |
|
| 105 |
const ( |
| 106 |
Error Kind = iota |
| 107 |
Warn Kind = iota |
| 108 |
Info Kind = iota |
| 109 |
) |
| 110 |
|
| 111 |
func (k Kind) String() string { |
| 112 |
switch k { |
| 113 |
case Error: |
| 114 |
return "error" |
| 115 |
case Warn: |
| 116 |
return "warning" |
| 117 |
case Info: |
| 118 |
return "info" |
| 119 |
default: |
| 120 |
return "unknown severity" |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
func (k Kind) IsFatal() bool { |
| 125 |
return k == Error |
| 126 |
} |
| 127 |
|
| 128 |
func (r *Report) AddOn(c path.ContextPath, err error, k EntryKind) { |
| 129 |
if err == nil { |
| 130 |
return |
| 131 |
} |
| 132 |
r.Entries = append(r.Entries, Entry{ |
| 133 |
Message: err.Error(), |
| 134 |
Context: c.Copy(), |
| 135 |
Kind: k, |
| 136 |
}) |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
func (r *Report) AddOnError(c path.ContextPath, err error) { |
| 141 |
r.AddOn(c, err, Error) |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
func (r *Report) AddOnWarn(c path.ContextPath, err error) { |
| 147 |
r.AddOn(c, err, Warn) |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
func (r *Report) AddOnInfo(c path.ContextPath, err error) { |
| 152 |
r.AddOn(c, err, Info) |
| 153 |
} |