Skip to content

Commit

Permalink
daily kata
Browse files Browse the repository at this point in the history
  • Loading branch information
jreisinger committed Jan 26, 2024
1 parent 4b07555 commit 6d76735
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 deletions.
1 change: 1 addition & 0 deletions katas.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* 2024-01-26: logger
* 2024-01-25: logger
* 2024-01-23: search, counter, logger
* 2024-01-17: counter
Expand Down
13 changes: 2 additions & 11 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"time"
)

const timeLayout = time.TimeOnly

type Logger struct {
logs chan string
wg sync.WaitGroup
Expand All @@ -32,24 +30,17 @@ func New(w io.Writer, buf int) *Logger {
go func() {
defer l.wg.Done()
for log := range l.logs {
fmt.Fprintf(w, "%s: %s\n", time.Now().Format(timeLayout), log)
fmt.Fprintf(w, "%s: %s\n", time.Now().Format(time.TimeOnly), log)
}
}()

return &l
}

// Stop stops accepting logs and waits for logs buffer to be written.
func (l *Logger) Stop() {
close(l.logs)
l.wg.Wait()
}

// Write writes the log. If the log buffer is full it prints a warning and exits.
func (l *Logger) Write(log string) {
select {
case l.logs <- log:
default:
fmt.Fprintf(os.Stderr, "%s: dropping logs\n", time.Now().Format(timeLayout))
fmt.Fprintf(os.Stderr, "%s: dropping logs\n", time.Now().Format(time.TimeOnly))
}
}

0 comments on commit 6d76735

Please sign in to comment.