Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
Nothing yet
- Add timestamps to process output (fgrosse/prox#5)

## [1.1.0] - 2024-07-13
- Improve file name matching when determining parser for value of --procfile flag (fgrosse/prox#8)
Expand Down
4 changes: 3 additions & 1 deletion executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func (e *Executor) Run(ctx context.Context, processes []Process) error {
defer logger.Sync()
go e.monitorContext(ctx, logger)

output := e.newOutput(processes)
// Add a timestamp to the process output, but not to prox's own log
// output.
output := newOutput(processes, e.noColors, newTimestampWriter(e.output))
pp := make([]process, len(processes))
for i, p := range processes {
po := output.next(p)
Expand Down
49 changes: 49 additions & 0 deletions timestamp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package prox

import (
"bytes"
"io"
"sync"
"time"
)

const timestampFormat = "15:04:05"

// timestampWriter prefixes every complete line written to it with the current
// time (HH:MM:SS).
type timestampWriter struct {
w io.Writer
mu sync.Mutex
buf []byte
}

func newTimestampWriter(w io.Writer) *timestampWriter {
return &timestampWriter{w: w}
}

func (t *timestampWriter) Write(p []byte) (int, error) {
t.mu.Lock()
defer t.mu.Unlock()

t.buf = append(t.buf, p...)

for {
i := bytes.IndexByte(t.buf, '\n')
if i == -1 {
break
}

line := t.buf[:i+1]
t.buf = t.buf[i+1:]

stamped := make([]byte, 0, len(line)+9)
stamped = append(stamped, time.Now().Format(timestampFormat)+" "...)
stamped = append(stamped, line...)

if _, err := t.w.Write(stamped); err != nil {
return 0, err
}
}

return len(p), nil
}