forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
167 lines (137 loc) · 4.07 KB
/
helpers.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
package application
import (
"cf"
"cf/terminal"
"code.google.com/p/gogoprotobuf/proto"
"fmt"
"github.com/cloudfoundry/loggregatorlib/logmessage"
"regexp"
"strings"
"time"
)
const (
TIMESTAMP_FORMAT = "2006-01-02T15:04:05.00-0700"
)
func NewLogMessage(msgText, appGuid, sourceName string, timestamp time.Time) (msg *logmessage.Message) {
messageType := logmessage.LogMessage_ERR
logMsg := logmessage.LogMessage{
Message: []byte(msgText),
AppId: proto.String(appGuid),
MessageType: &messageType,
SourceName: proto.String(sourceName),
Timestamp: proto.Int64(timestamp.UnixNano()),
}
data, _ := proto.Marshal(&logMsg)
msg, _ = logmessage.ParseMessage(data)
return
}
func simpleLogMessageOutput(msg *logmessage.Message) (msgText string) {
logMsg := msg.GetLogMessage()
msgText = string(logMsg.GetMessage())
reg, err := regexp.Compile("[\n\r]+$")
if err != nil {
return
}
msgText = reg.ReplaceAllString(msgText, "")
return
}
func logMessageOutput(msg *logmessage.Message) string {
logHeader, coloredLogHeader := extractLogHeader(msg)
logMsg := msg.GetLogMessage()
logContent := extractLogContent(logMsg, logHeader)
return fmt.Sprintf("%s%s", coloredLogHeader, logContent)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func extractLogHeader(msg *logmessage.Message) (logHeader, coloredLogHeader string) {
logMsg := msg.GetLogMessage()
sourceName := logMsg.GetSourceName()
sourceID := logMsg.GetSourceId()
t := time.Unix(0, logMsg.GetTimestamp())
timeFormat := TIMESTAMP_FORMAT
timeString := t.Format(timeFormat)
logHeader = fmt.Sprintf("%s [%s]", timeString, sourceName)
coloredLogHeader = terminal.LogSysHeaderColor(logHeader)
if sourceName == "App" {
logHeader = fmt.Sprintf("%s [%s/%s]", timeString, sourceName, sourceID)
coloredLogHeader = terminal.LogAppHeaderColor(logHeader)
}
// Calculate padding
longestHeader := fmt.Sprintf("%s [App/0] ", timeFormat)
expectedHeaderLength := len(longestHeader)
padding := strings.Repeat(" ", max(0, expectedHeaderLength-len(logHeader)))
logHeader = logHeader + padding
coloredLogHeader = coloredLogHeader + padding
return
}
func extractLogContent(logMsg *logmessage.LogMessage, logHeader string) (logContent string) {
msgText := string(logMsg.GetMessage())
reg, err := regexp.Compile("[\n\r]+$")
if err == nil {
msgText = reg.ReplaceAllString(msgText, "")
}
msgLines := strings.Split(msgText, "\n")
padding := strings.Repeat(" ", len(logHeader))
coloringFunc := terminal.LogStdoutColor
logType := "OUT"
if logMsg.GetMessageType() == logmessage.LogMessage_ERR {
coloringFunc = terminal.LogStderrColor
logType = "ERR"
}
logContent = fmt.Sprintf("%s %s", logType, msgLines[0])
for _, msgLine := range msgLines[1:] {
logContent = fmt.Sprintf("%s\n%s%s", logContent, padding, msgLine)
}
logContent = coloringFunc(logContent)
return
}
func coloredAppState(app cf.ApplicationFields) string {
appState := strings.ToLower(app.State)
if app.RunningInstances == 0 {
if appState == "stopped" {
return appState
} else {
return terminal.CrashedColor(appState)
}
}
if app.RunningInstances < app.InstanceCount {
return terminal.WarningColor(appState)
}
return appState
}
func coloredAppInstances(app cf.ApplicationFields) string {
healthString := fmt.Sprintf("%d/%d", app.RunningInstances, app.InstanceCount)
if app.RunningInstances == 0 {
if strings.ToLower(app.State) == "stopped" {
return healthString
} else {
return terminal.CrashedColor(healthString)
}
}
if app.RunningInstances < app.InstanceCount {
return terminal.WarningColor(healthString)
}
return healthString
}
func coloredInstanceState(instance cf.AppInstanceFields) (colored string) {
state := string(instance.State)
switch state {
case "started", "running":
colored = "running"
case "stopped":
colored = terminal.StoppedColor("stopped")
case "flapping":
colored = terminal.CrashedColor("crashing")
case "down":
colored = terminal.CrashedColor("down")
case "starting":
colored = terminal.AdvisoryColor("starting")
default:
colored = terminal.WarningColor(state)
}
return
}