forked from getgauge/gauge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executionLogger.go
224 lines (190 loc) · 6.11 KB
/
executionLogger.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of Gauge.
// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Gauge. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"github.com/getgauge/gauge/gauge_messages"
"github.com/getgauge/gauge/logger"
"github.com/wsxiaoys/terminal"
"strings"
)
type executionLogger interface {
Write([]byte) (int, error)
Text(string)
PrintError(string)
SpecHeading(string)
ScenarioHeading(string)
Comment(*comment)
Step(*step)
StepStarting(*step)
StepFinished(*step, bool)
Table(*table)
Critical(string, ...interface{})
Warning(string, ...interface{})
Info(string, ...interface{})
Debug(string, ...interface{})
Error(string, ...interface{})
ConceptStarting(*gauge_messages.ProtoConcept)
ConceptFinished(*gauge_messages.ProtoConcept)
}
var currentLogger executionLogger
type coloredLogger struct {
linesAfterLastStep int
isInsideStep bool
indentation int
}
type pluginLogger struct {
pluginName string
}
func (writer *pluginLogger) Write(b []byte) (int, error) {
message := string(b)
prefixedMessage := addPrefixToEachLine(message, fmt.Sprintf("[%s Plugin] : ", writer.pluginName))
gaugeConsoleWriter := getCurrentExecutionLogger()
_, err := gaugeConsoleWriter.Write([]byte(prefixedMessage))
return len(message), err
}
func addPrefixToEachLine(text string, template string) string {
lines := strings.Split(text, "\n")
prefixedLines := make([]string, 0)
for i, line := range lines {
if (i == len(lines)-1) && line == "" {
prefixedLines = append(prefixedLines, line)
} else {
prefixedLines = append(prefixedLines, template+line)
}
}
return strings.Join(prefixedLines, "\n")
}
func newColoredConsoleWriter() *coloredLogger {
return &coloredLogger{linesAfterLastStep: 0, isInsideStep: false, indentation: 0}
}
func getCurrentExecutionLogger() executionLogger {
if currentLogger == nil {
if *simpleConsoleOutput {
currentLogger = newSimpleConsoleWriter()
} else {
currentLogger = newColoredConsoleWriter()
}
}
return currentLogger
}
func (writer *coloredLogger) Write(b []byte) (int, error) {
message := indent(string(b), writer.indentation)
if writer.isInsideStep {
writer.linesAfterLastStep += strings.Count(message, "\n")
}
fmt.Print(message)
return len(b), nil
}
func (writer *coloredLogger) Text(value string) {
writer.Write([]byte(value))
}
func (writer *coloredLogger) PrintError(value string) {
if writer.isInsideStep {
writer.linesAfterLastStep += strings.Count(value, "\n")
}
terminal.Stdout.Colorf("@r%s", value)
}
func (writer *coloredLogger) Critical(formatString string, args ...interface{}) {
logger.Log.Critical(formatString, args)
}
func (writer *coloredLogger) Info(formatString string, args ...interface{}) {
logger.Log.Info(formatString, args)
}
func (writer *coloredLogger) Warning(formatString string, args ...interface{}) {
logger.Log.Warning(formatString, args)
}
func (writer *coloredLogger) Debug(formatString string, args ...interface{}) {
logger.Log.Debug(formatString, args)
}
func (writer *coloredLogger) Error(formatString string, args ...interface{}) {
logger.Log.Error(formatString, args)
}
func (writer *coloredLogger) SpecHeading(heading string) {
formattedHeading := formatSpecHeading(heading)
writer.Write([]byte(formattedHeading))
}
func (writer *coloredLogger) Comment(comment *comment) {
writer.Write([]byte(formatComment(comment)))
}
func (writer *coloredLogger) ScenarioHeading(scenarioHeading string) {
formattedHeading := formatScenarioHeading(scenarioHeading)
writer.Write([]byte(fmt.Sprintf("\n%s", formattedHeading)))
}
func (writer *coloredLogger) writeContextStep(step *step) {
writer.Step(step)
}
func (writer *coloredLogger) Step(step *step) {
stepText := formatStep(step)
terminal.Stdout.Colorf("@b%s", stepText)
writer.isInsideStep = true
writer.linesAfterLastStep = 0
}
func (writer *coloredLogger) ConceptStarting(protoConcept *gauge_messages.ProtoConcept) {
conceptText := indent(formatConcept(protoConcept), writer.indentation)
terminal.Stdout.Colorf("@b%s", conceptText)
writer.indentation += 4
}
func (writer *coloredLogger) ConceptFinished(protoConcept *gauge_messages.ProtoConcept) {
writer.indentation -= 4
}
func (writer *coloredLogger) StepStarting(step *step) {
stepText := formatStep(step)
terminal.Stdout.Colorf("@b%s", stepText)
writer.isInsideStep = true
writer.linesAfterLastStep = 0
}
//todo: pass protostep instead
func (writer *coloredLogger) StepFinished(step *step, failed bool) {
stepText := indent(formatStep(step), writer.indentation)
linesInStepText := strings.Count(stepText, "\n")
if linesInStepText == 0 {
linesInStepText = 1
}
linesToMoveUp := writer.linesAfterLastStep + linesInStepText
terminal.Stdout.Up(linesToMoveUp)
if failed {
terminal.Stdout.Colorf("@r%s", stepText)
} else {
terminal.Stdout.Colorf("@g%s", stepText)
}
terminal.Stdout.Down(linesToMoveUp)
writer.isInsideStep = false
}
func (writer *coloredLogger) Table(table *table) {
formattedTable := indent(formatTable(table), writer.indentation)
terminal.Stdout.Colorf("@m%s", formattedTable)
}
func indent(message string, indentation int) string {
if indentation == 0 {
return message
}
lines := strings.Split(message, "\n")
prefixedLines := make([]string, 0)
spaces := getEmptySpacedString(indentation)
for i, line := range lines {
if (i == len(lines)-1) && line == "" {
prefixedLines = append(prefixedLines, line)
} else {
prefixedLines = append(prefixedLines, spaces+line)
}
}
return strings.Join(prefixedLines, "\n")
}
func getEmptySpacedString(numOfSpaces int) string {
text := ""
for i := 0; i < numOfSpaces; i++ {
text += " "
}
return text
}