summaryrefslogtreecommitdiff
path: root/internal/io/dlog/loggers/stdout.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/io/dlog/loggers/stdout.go')
-rw-r--r--internal/io/dlog/loggers/stdout.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/internal/io/dlog/loggers/stdout.go b/internal/io/dlog/loggers/stdout.go
new file mode 100644
index 0000000..9738323
--- /dev/null
+++ b/internal/io/dlog/loggers/stdout.go
@@ -0,0 +1,73 @@
+package loggers
+
+import (
+ "context"
+ "fmt"
+ "sync"
+ "time"
+)
+
+type stdout struct {
+ bufferCh chan string
+ pauseCh chan struct{}
+ resumeCh chan struct{}
+}
+
+func newStdout() *stdout {
+ return &stdout{
+ bufferCh: make(chan string, 100),
+ pauseCh: make(chan struct{}),
+ resumeCh: make(chan struct{}),
+ }
+}
+
+func (s *stdout) Start(ctx context.Context, wg *sync.WaitGroup) {
+ pause := func(ctx context.Context) {
+ select {
+ case <-s.resumeCh:
+ return
+ case <-ctx.Done():
+ return
+ }
+ }
+
+ go func() {
+ defer wg.Done()
+
+ for {
+ select {
+ case message := <-s.bufferCh:
+ fmt.Println(message)
+ case <-s.pauseCh:
+ pause(ctx)
+ case <-ctx.Done():
+ s.Flush()
+ return
+ }
+ }
+ }()
+}
+
+func (s *stdout) Log(now time.Time, message string) {
+ s.bufferCh <- message
+}
+
+func (s *stdout) LogWithColors(now time.Time, message, coloredMessage string) {
+ s.bufferCh <- coloredMessage
+}
+
+func (s *stdout) Flush() {
+ for {
+ select {
+ case message := <-s.bufferCh:
+ fmt.Println(message)
+ default:
+ return
+ }
+ }
+}
+
+func (s *stdout) Pause() { s.pauseCh <- struct{}{} }
+func (s *stdout) Resume() { s.resumeCh <- struct{}{} }
+func (s *stdout) Rotate() {}
+func (stdout) SupportsColors() bool { return true }