forked from signal18/replication-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.go
150 lines (133 loc) · 4.48 KB
/
display.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
// replication-manager - Replication Manager Monitoring and CLI for MariaDB and MySQL
// Copyright 2017 Signal 18 SARL
// Authors: Guillaume Lefranc <guillaume@signal18.io>
// Stephane Varoqui <svaroqui@gmail.com>
// This source code is licensed under the GNU General Public License, version 3.
// Redistribution/Reuse of this code is permitted under the GNU v3 license, as
// an additional term, ALL code must carry the original Author(s) credit in comment form.
// See LICENSE in this directory for the integral text.
// display.go
package cluster
import (
"fmt"
"io"
"time"
"github.com/signal18/replication-manager/httplog"
log "github.com/sirupsen/logrus"
"github.com/nsf/termbox-go"
)
// Log levels
const (
LvlInfo = "INFO"
LvlWarn = "WARN"
LvlErr = "ERROR"
LvlDbg = "DEBUG"
)
func (cluster *Cluster) display() {
if cluster.cfgGroup != cluster.cfgGroupDisplay {
return
}
cluster.tlog.Print()
}
func (cluster *Cluster) DisplayHelp() {
cluster.LogPrint("HELP : Ctrl-D Print debug information")
cluster.LogPrint("HELP : Ctrl-F Manual Failover")
cluster.LogPrint("HELP : Ctrl-I Toggle automatic/manual failover mode")
cluster.LogPrint("HELP : Ctrl-R Set slaves read-only")
cluster.LogPrint("HELP : Ctrl-S Switchover")
cluster.LogPrint("HELP : Ctrl-N Next Cluster")
cluster.LogPrint("HELP : Ctrl-P Previous Cluster")
cluster.LogPrint("HELP : Ctrl-Q Quit")
cluster.LogPrint("HELP : Ctrl-C Quit")
cluster.LogPrint("HELP : Ctrl-W Set slaves read-write")
}
func (cluster *Cluster) printTb(x, y int, fg, bg termbox.Attribute, msg string) {
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
x++
}
}
func (cluster *Cluster) printfTb(x, y int, fg, bg termbox.Attribute, format string, args ...interface{}) {
s := fmt.Sprintf(format, args...)
cluster.printTb(x, y, fg, bg, s)
}
func (cluster *Cluster) LogPrint(msg ...interface{}) {
stamp := fmt.Sprint(time.Now().Format("2006/01/02 15:04:05"))
if cluster.conf.LogFile != "" {
s := fmt.Sprint(stamp, " [", cluster.cfgGroup, "] ", fmt.Sprint(msg...))
io.WriteString(cluster.logPtr, fmt.Sprintln(s))
}
if cluster.tlog.Len > 0 {
s := fmt.Sprint("[", cluster.cfgGroup, "] ", fmt.Sprint(msg...))
cluster.tlog.Add(s)
cluster.display()
}
if cluster.conf.Daemon {
s := fmt.Sprint("[", cluster.cfgGroup, "] ", fmt.Sprint(msg...))
log.Println(s)
}
}
func (cluster *Cluster) LogPrintf(level string, format string, args ...interface{}) {
stamp := fmt.Sprint(time.Now().Format("2006/01/02 15:04:05"))
padright := func(str, pad string, lenght int) string {
for {
str += pad
if len(str) > lenght {
return str[0:lenght]
}
}
}
cliformat := format
format = "[" + cluster.cfgGroup + "] " + padright(level, " ", 5) + " - " + format
if level == "DEBUG" && cluster.conf.LogLevel <= 1 {
// Only print debug messages if loglevel > 1
} else {
if cluster.conf.LogFile != "" {
f := fmt.Sprintln(stamp, format)
io.WriteString(cluster.logPtr, fmt.Sprintf(f, args...))
}
if cluster.tlog != nil && cluster.tlog.Len > 0 {
cluster.tlog.Add(fmt.Sprintf(format, args...))
cluster.display()
}
if cluster.conf.HttpServ {
msg := httplog.Message{
Group: cluster.cfgGroup,
Level: level,
Timestamp: stamp,
Text: fmt.Sprintf(cliformat, args...),
}
cluster.htlog.Add(msg)
}
}
if cluster.conf.Daemon {
// wrap logrus levels
switch level {
case "ERROR":
log.WithField("cluster", cluster.cfgGroup).Errorf(cliformat, args...)
case "INFO":
log.WithField("cluster", cluster.cfgGroup).Infof(cliformat, args...)
case "DEBUG":
log.WithField("cluster", cluster.cfgGroup).Debugf(cliformat, args...)
case "WARN":
log.WithField("cluster", cluster.cfgGroup).Warnf(cliformat, args...)
case "TEST":
log.WithFields(log.Fields{"cluster": cluster.cfgGroup, "type": "test"}).Infof(cliformat, args...)
case "BENCH":
log.WithFields(log.Fields{"cluster": cluster.cfgGroup, "type": "benchmark"}).Infof(cliformat, args...)
case "ALERT":
log.WithFields(log.Fields{"cluster": cluster.cfgGroup, "type": "alert"}).Warnf(cliformat, args...)
case "STATE":
status := cliformat[0:6]
code := cliformat[7:15]
err := cliformat[18:]
if status == "OPENED" {
log.WithFields(log.Fields{"cluster": cluster.cfgGroup, "type": "state", "status": status, "code": code}).Warnf(err, args...)
} else {
log.WithFields(log.Fields{"cluster": cluster.cfgGroup, "type": "state", "status": status, "code": code}).Infof(err, args...)
}
default:
log.Printf(cliformat, args...)
}
}
}