-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlog_view.go
59 lines (46 loc) · 1.17 KB
/
log_view.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
package view
import (
"fmt"
"math"
"os"
"time"
"github.com/jumppad-labs/jumppad/pkg/clients/logger"
)
type LogView struct {
logger logger.Logger
statusMessage string
statusTimer bool
statusTimerStart time.Time
}
func NewLogView() (*LogView, error) {
c := &LogView{}
c.logger = logger.NewLogger(os.Stdout, logger.LogLevelDebug)
return c, nil
}
// Display starts the view, this is a blocking function
func (c *LogView) Display() error {
for {
if c.statusTimer {
et := time.Since(c.statusTimerStart)
elapsed := fmt.Sprintf("%ds", int(math.Round(float64(et)/float64(time.Second))))
c.Logger().Info(c.statusMessage, "elapsed_time", elapsed)
}
time.Sleep(1 * time.Second)
}
}
// Logger returns the logger used by the view
func (c *LogView) Logger() logger.Logger {
return c.logger
}
// UpdateStatus shows the current status message, if withTimer is set
// the elapsed time that the the status has been shown for will also
// be displayed
func (c *LogView) UpdateStatus(message string, withTimer bool) {
c.statusTimer = withTimer
if withTimer {
c.statusMessage = message
c.statusTimerStart = time.Now()
return
}
c.Logger().Info(message)
}