|
1
|
|
|
/* |
|
2
|
|
|
Package e2hformat is the formatter's package of the Enhanced Error Handling module |
|
3
|
|
|
*/ |
|
4
|
|
|
package e2hformat |
|
5
|
|
|
|
|
6
|
|
|
import ( |
|
7
|
|
|
"fmt" |
|
8
|
|
|
"strings" |
|
9
|
|
|
) |
|
10
|
|
|
|
|
11
|
|
|
// Level defines log levels. |
|
12
|
|
|
type Format int8 |
|
13
|
|
|
type HidingMethod int8 |
|
14
|
|
|
|
|
15
|
|
|
const ( |
|
16
|
|
|
// Disabled disables the logger. |
|
17
|
|
|
Format_Raw Format = iota |
|
18
|
|
|
|
|
19
|
|
|
// Mensajes de muy baja frecuencia que se deben mostrar siempre (como el copyright) |
|
20
|
|
|
Format_JSON |
|
21
|
|
|
) |
|
22
|
|
|
|
|
23
|
|
|
const ( |
|
24
|
|
|
HidingMethod_None HidingMethod = iota |
|
25
|
|
|
|
|
26
|
|
|
HidingMethod_FullBaseline |
|
27
|
|
|
|
|
28
|
|
|
HidingMethod_ToFolder |
|
29
|
|
|
) |
|
30
|
|
|
|
|
31
|
|
|
type Params struct { |
|
32
|
|
|
Beautify bool |
|
33
|
|
|
InvertCallstack bool |
|
34
|
|
|
PathHidingMethod HidingMethod |
|
35
|
|
|
PathHidingValue string |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
type Formatter interface { |
|
39
|
|
|
Source(err error) string |
|
40
|
|
|
Format(err error, params Params) string |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
func NewFormatter(format Format) (Formatter, error) { |
|
44
|
|
|
|
|
45
|
|
|
switch format { |
|
46
|
|
|
case Format_Raw: |
|
47
|
|
|
return newRawFormatter(), nil |
|
48
|
|
|
case Format_JSON: |
|
49
|
|
|
return newJSONFormatter(), nil |
|
50
|
|
|
default: |
|
51
|
|
|
return nil, fmt.Errorf("unknown format [%d]", format) |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// This function format the sourcefile according to the provided params |
|
57
|
|
|
func formatSourceFile(file string, hidingMethod HidingMethod, hidingValue string) string { |
|
58
|
|
|
switch hidingMethod { |
|
59
|
|
|
case HidingMethod_FullBaseline: |
|
60
|
|
|
return removePathSegment(file, hidingValue) |
|
61
|
|
|
case HidingMethod_ToFolder: |
|
62
|
|
|
return removeBeforeFolder(file, hidingValue) |
|
63
|
|
|
default: //HidingMethod_None |
|
64
|
|
|
return file |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// Utility funtion that removes the first part of the filepath til the end of `baseline` path argument |
|
69
|
|
|
func removePathSegment(file string, baseline string) string { |
|
70
|
|
|
|
|
71
|
|
|
prettyCaller := strings.ReplaceAll(file, baseline, "") |
|
72
|
|
|
if len(prettyCaller) > 0 { |
|
73
|
|
|
return prettyCaller |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return file |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Utility funtion that removes the first part of the filepath til found the folder indicated in `newRootFolder` argument |
|
80
|
|
|
func removeBeforeFolder(file string, newRootFolder string) string { |
|
81
|
|
|
|
|
82
|
|
|
fileParts := strings.Split(file, newRootFolder) |
|
83
|
|
|
if len(fileParts) < 2 { |
|
84
|
|
|
return file |
|
85
|
|
|
} |
|
86
|
|
|
return newRootFolder + fileParts[1] |
|
87
|
|
|
} |
|
88
|
|
|
|