summaryrefslogtreecommitdiff
path: root/internal/io/dlog/loggers/stdout.go
blob: b4e695ab1dee3f7c554147eaeec6864dc724e020 (plain)
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
package loggers

import (
	"context"
	"fmt"
	"sync"
	"time"
)

type stdout struct {
	pauseCh  chan struct{}
	resumeCh chan struct{}
	mutex    sync.Mutex
}

var _ Logger = (*stdout)(nil)

func newStdout() *stdout {
	return &stdout{
		pauseCh:  make(chan struct{}),
		resumeCh: make(chan struct{}),
	}
}

func (s *stdout) Start(ctx context.Context, wg *sync.WaitGroup) {
	wg.Done()
}

func (s *stdout) Log(now time.Time, message string) {
	s.log(message, true)
}

func (s *stdout) LogWithColors(now time.Time, message, coloredMessage string) {
	s.log(coloredMessage, true)
}

func (s *stdout) Raw(now time.Time, message string) {
	s.log(message, false)
}

func (s *stdout) RawWithColors(now time.Time, message, coloredMessage string) {
	s.log(coloredMessage, false)
}

func (s *stdout) log(message string, nl bool) {
	s.mutex.Lock()
	select {
	case <-s.pauseCh:
		// Wait for Resume without holding the mutex: the prompt path calls
		// dlog after the user answers while Pause is still active; holding the
		// mutex here would deadlock (Info blocks on Lock, Resume never runs).
		s.mutex.Unlock()
		<-s.resumeCh
		s.mutex.Lock()
	default:
	}
	defer s.mutex.Unlock()

	if nl {
		fmt.Println(message)
		return
	}
	fmt.Print(message)
}

func (s *stdout) Pause()  { s.pauseCh <- struct{}{} }
func (s *stdout) Resume() { s.resumeCh <- struct{}{} }

func (s *stdout) Flush() {
	// This is empty because it isn't doing anything but has to satisfy the interface.
}

func (s *stdout) Rotate() {
	// This is empty because it isn't doing anything but has to satisfy the interface.
}

func (*stdout) SupportsColors() bool { return true }