/
dirtyLogger.go
207 lines (188 loc) · 6.13 KB
/
dirtyLogger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// MIT License
//
// Copyright (c) 2020 Thomas Ziegler <thomas.zglr@googlemail.com>. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// AINC-NOTE-0815
package yaclint
import (
"fmt"
"strings"
"time"
"github.com/swaros/contxt/module/ctxout"
)
// lets create a simple and dirty logger for the linting process
// right now i don't like the idea to use a logger for this.
// this will (maybe) be changed in the future
// but for now i did not find anything matching my needs.
// even the logger interfaces i saw, have a different theorie of logging then
// i have.
// the logger should track all messages and print them at the end of the linting process.
// but there you need to know the context of the message.
// for this you don't need different levels of logging, like Error or info. and you dont't need to push
// the messages to a file or something else.
// this type of logging is for development only. while you are writing code, and not while you try to
// figure out, why the app crashes in a pod running in the cloud.
// you need to know anything what depends on the entry "key" and the context.
// and you don't casre about the level. and you dont need any other information, what dosn't help you to find the issue.
type DirtyLoggerDef interface {
Trace(args ...interface{})
SetTraceFn(traceFn TraceFn)
GetTrace(find ...string) []string
Print(patter ...string) string
}
type TraceFn func(args ...interface{}) // thats all we need right now
const (
DirtyLogMaxLen = 500
)
type DirtyLogger struct {
traceFn TraceFn
logStuff []string
addTime bool
}
func NewDirtyLogger() *DirtyLogger {
return &DirtyLogger{
traceFn: nil,
addTime: true,
}
}
func (dl *DirtyLogger) SetAddTime(addTime bool) *DirtyLogger {
dl.addTime = addTime
return dl
}
func (dl *DirtyLogger) Trace(args ...interface{}) {
if dl.traceFn != nil {
dl.traceFn(args...)
} else {
dl.CreateSimpleTracer()
dl.traceFn("CreateSimpleTracer: there was no traceFn set. so we create a simple one.")
dl.traceFn(args...)
}
}
func (dl *DirtyLogger) GetTrace(find ...string) []string {
if len(find) == 0 {
return dl.logStuff
}
// lets find the strings
var ret []string
for _, s := range dl.logStuff {
for _, f := range find {
if strings.Contains(strings.ToLower(s), strings.ToLower(f)) {
ret = append(ret, s)
}
}
}
return ret
}
func (dl *DirtyLogger) Print(pattern ...string) string {
return strings.Join(dl.GetTrace(pattern...), "\n")
}
func (dl *DirtyLogger) SetTraceFn(traceFn TraceFn) {
dl.traceFn = traceFn
}
func (dl *DirtyLogger) CreateSimpleTracer() *DirtyLogger {
dl.traceFn = func(args ...interface{}) {
addStr := ""
// add the current time first
if dl.addTime {
addStr += time.Now().Format("[15:04:05.000] - ")
}
for _, a := range args {
switch a := a.(type) {
case string:
addStr += a
case MatchToken:
case *MatchToken:
addorRm := "-"
if a.Added {
addorRm = "+"
}
addStr += "[" + addorRm + a.keyToString() + "] "
case []string: // better readable instead of the fmt.Sprint
addStr += "["
for _, s := range a {
addStr += "'" + s + "',"
}
addStr += "]"
default:
addStr += fmt.Sprintf("%v", a)
}
}
if addStr != "" {
// lets add the string to the logStuff
// but respect the max length
if len(dl.logStuff)+1 == DirtyLogMaxLen {
// remove the first element
dl.logStuff = dl.logStuff[1:]
}
// append the new one
dl.logStuff = append(dl.logStuff, addStr)
}
}
return dl
}
func (dl *DirtyLogger) CreateCtxoutTracer() *DirtyLogger {
dl.traceFn = func(args ...interface{}) {
addStr := ""
// add the current time first
if dl.addTime {
addStr += ctxout.ForeDarkGrey + ctxout.BackWhite + time.Now().Format("[15:04:05.000]") + ctxout.CleanTag + " - "
}
for _, a := range args {
switch a := a.(type) {
case bool:
if a {
addStr += ctxout.ForeGreen + ctxout.BackBlack + "true" + ctxout.CleanTag
} else {
addStr += ctxout.ForeRed + ctxout.BackBlack + "false" + ctxout.CleanTag
}
case string:
addStr += ctxout.ForeLightBlue + a + ctxout.CleanTag
case MatchToken:
case *MatchToken:
addorRm := ctxout.ForeRed + "-"
if a.Added {
addorRm = ctxout.ForeGreen + "+"
}
addStr += ctxout.ForeCyan + "[" + addorRm + a.keyToString() + ctxout.CleanTag + ctxout.ForeCyan + "] " + ctxout.CleanTag
addStr += ctxout.ForeDarkGrey + fmt.Sprintf("%v", a.Value) + ctxout.CleanTag
case []string: // better readable instead of the fmt.Sprint
addStr += ctxout.ForeMagenta + ctxout.BackBlack + "["
joinStr := []string{}
for _, s := range a {
joinStr = append(joinStr, ctxout.ForeMagenta+"'"+ctxout.ForeCyan+s+ctxout.ForeMagenta+"'")
}
addStr += strings.Join(joinStr, ctxout.ForeMagenta+",") + "]" + ctxout.CleanTag
default:
addStr += fmt.Sprintf("%v", a)
}
}
if addStr != "" {
// lets add the string to the logStuff
// but respect the max length
if len(dl.logStuff)+1 == DirtyLogMaxLen {
// remove the first element
dl.logStuff = dl.logStuff[1:]
}
// append the new one
dl.logStuff = append(dl.logStuff, ctxout.ToString(ctxout.NewMOWrap(), addStr))
}
}
return dl
}