Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render raw message #24

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions log_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ type LogEntryMessage struct {
scopes []string
}

type RawLogEntryMessage struct {
Level LogLevel
message string
scopes []string
}

func (entry RawLogEntryMessage) GetMessage() string {
return fmt.Sprint(entry.message)
}

func (entry RawLogEntryMessage) GetScopes() []string {
return entry.scopes
}

func NewLogEntryMessage(scopes []string, level LogLevel, format string, a ...interface{}) *LogEntryMessage {
return &LogEntryMessage{
Level: level,
Expand All @@ -67,6 +81,14 @@ func NewLogEntryMessage(scopes []string, level LogLevel, format string, a ...int
}
}

func NewRawLogEntryMessage(scopes []string, level LogLevel, message string) *RawLogEntryMessage {
return &RawLogEntryMessage{
Level: level,
message: message,
scopes: scopes,
}
}

func (entry *LogEntryMessage) GetMessage() string {
return fmt.Sprintf(entry.format, entry.arguments...)
}
Expand Down
13 changes: 13 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ type genericLogEntry struct {
LogStarted *LogScopeStarted
LogFinished *LogScopeFinished
LogEntry *LogEntryMessage
RawLogEntry *RawLogEntryMessage
}

type LogRendered interface {
RenderScopeStarted(entry *LogScopeStarted)
RenderScopeFinished(entry *LogScopeFinished)
RenderMessage(entry *LogEntryMessage)
RenderRawMessage(entry *RawLogEntryMessage)
}

type Logger struct {
Expand Down Expand Up @@ -65,6 +67,9 @@ func (logger *Logger) streamEntries(renderer LogRendered) {
if entry.LogEntry != nil {
renderer.RenderMessage(entry.LogEntry)
}
if entry.RawLogEntry != nil {
renderer.RenderRawMessage(entry.RawLogEntry)
}
}
}

Expand All @@ -80,6 +85,14 @@ func (logger *Logger) Infof(format string, args ...interface{}) {
logger.Logf(InfoLevel, format, args...)
}

func (logger *Logger) Write(p []byte) (n int, err error) {
logger.entriesChannel <- &genericLogEntry{
RawLogEntry: NewRawLogEntryMessage(logger.scopes, InfoLevel, string(p)),
}

return len(p), err
}

func (logger *Logger) Warnf(format string, args ...interface{}) {
logger.Logf(WarnLevel, format, args...)
}
Expand Down
4 changes: 4 additions & 0 deletions renderers/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (r *InteractiveRenderer) RenderMessage(entry *echelon.LogEntryMessage) {
findScopedNode(entry.GetScopes(), r).AppendDescription(entry.GetMessage() + "\n")
}

func (r *InteractiveRenderer) RenderRawMessage(entry *echelon.RawLogEntryMessage) {
findScopedNode(entry.GetScopes(), r).AppendDescription(entry.GetMessage())
}

func (r *InteractiveRenderer) StartDrawing() {
_ = console.PrepareTerminalEnvironment()
// don't wrap lines since it breaks incremental redraws
Expand Down
16 changes: 10 additions & 6 deletions renderers/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (r SimpleRenderer) RenderScopeStarted(entry *echelon.LogScopeStarted) {
r.startTimes[timeKey] = time.Now()
lastScope := scopes[level-1]
message := terminal.GetColoredText(r.colors.NeutralColor, fmt.Sprintf("Started %s", quotedIfNeeded(lastScope)))
r.RenderRawMessage(message)
r.WriteRawMessage(message)
}

func (r SimpleRenderer) RenderScopeFinished(entry *echelon.LogScopeFinished) {
Expand All @@ -68,26 +68,30 @@ func (r SimpleRenderer) RenderScopeFinished(entry *echelon.LogScopeFinished) {
case echelon.FinishTypeSucceeded:
message := fmt.Sprintf("%s succeeded in %s!", quotedIfNeeded(lastScope), formatedDuration)
coloredMessage := terminal.GetColoredText(r.colors.SuccessColor, message)
r.RenderRawMessage(coloredMessage)
r.WriteRawMessage(coloredMessage)
case echelon.FinishTypeFailed:
message := fmt.Sprintf("%s failed in %s!", quotedIfNeeded(lastScope), formatedDuration)
coloredMessage := terminal.GetColoredText(r.colors.FailureColor, message)
r.RenderRawMessage(coloredMessage)
r.WriteRawMessage(coloredMessage)
case echelon.FinishTypeSkipped:
message := fmt.Sprintf("%s skipped in %s!", quotedIfNeeded(lastScope), formatedDuration)
coloredMessage := terminal.GetColoredText(r.colors.NeutralColor, message)
r.RenderRawMessage(coloredMessage)
r.WriteRawMessage(coloredMessage)
}
}

func (r SimpleRenderer) RenderMessage(entry *echelon.LogEntryMessage) {
r.RenderRawMessage(entry.GetMessage())
r.WriteRawMessage(entry.GetMessage())
}

func (r SimpleRenderer) RenderRawMessage(message string) {
func (r SimpleRenderer) WriteRawMessage(message string) {
_, _ = r.out.Write([]byte(message + "\n"))
}

func (r SimpleRenderer) RenderRawMessage(entry *echelon.RawLogEntryMessage) {
_, _ = r.out.Write([]byte(entry.GetMessage()))
}

func (r SimpleRenderer) ScopeHasStarted(scopes []string) bool {
level := len(scopes)
if level == 0 {
Expand Down
2 changes: 2 additions & 0 deletions renderers/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ func (*StubRenderer) RenderScopeStarted(entry *echelon.LogScopeStarted) {}
func (*StubRenderer) RenderScopeFinished(entry *echelon.LogScopeFinished) {}

func (*StubRenderer) RenderMessage(entry *echelon.LogEntryMessage) {}

func (*StubRenderer) Render(entry *echelon.LogEntryMessage) {}