2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2019-01-02 18:07:49 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync"
|
2019-01-08 15:24:15 +00:00
|
|
|
"time"
|
2019-01-02 18:07:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PrefixWriter writes to the specified output with prefixes.
|
|
|
|
type PrefixWriter struct {
|
|
|
|
root *prefixWriter
|
|
|
|
maxline int
|
2020-09-10 14:37:16 +01:00
|
|
|
nowFunc func() time.Time
|
2019-01-02 18:07:49 +00:00
|
|
|
|
2019-01-08 15:24:15 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
prefixlen int
|
|
|
|
dst io.Writer
|
|
|
|
}
|
|
|
|
|
2020-09-10 14:37:16 +01:00
|
|
|
const (
|
|
|
|
maxIDLength = 10
|
|
|
|
timeFormat = "15:04:05.000"
|
|
|
|
emptyTimeField = " "
|
|
|
|
)
|
2019-01-08 15:24:15 +00:00
|
|
|
|
|
|
|
func max(a, b int) int {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPrefixWriter creates a writer than can prefix all lines written to it.
|
2020-09-10 14:37:16 +01:00
|
|
|
func NewPrefixWriter(defaultPrefix string, maxLineLen int, dst io.Writer) *PrefixWriter {
|
2019-01-02 18:07:49 +00:00
|
|
|
writer := &PrefixWriter{
|
2020-09-10 14:37:16 +01:00
|
|
|
maxline: maxLineLen,
|
2019-01-02 18:07:49 +00:00
|
|
|
dst: dst,
|
2020-09-10 14:37:16 +01:00
|
|
|
nowFunc: time.Now,
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
|
|
|
writer.root = writer.Prefixed(defaultPrefix).(*prefixWriter)
|
|
|
|
return writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// prefixWriter is the implementation that handles buffering and prefixing.
|
|
|
|
type prefixWriter struct {
|
|
|
|
*PrefixWriter
|
|
|
|
prefix string
|
2019-06-19 20:30:48 +01:00
|
|
|
|
|
|
|
local sync.Mutex
|
2019-01-08 15:24:15 +00:00
|
|
|
id string
|
2019-01-02 18:07:49 +00:00
|
|
|
buffer []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefixed returns a new writer that has writes with specified prefix.
|
|
|
|
func (writer *PrefixWriter) Prefixed(prefix string) io.Writer {
|
|
|
|
writer.mu.Lock()
|
2019-01-08 15:24:15 +00:00
|
|
|
writer.prefixlen = max(writer.prefixlen, len(prefix))
|
2019-01-02 18:07:49 +00:00
|
|
|
writer.mu.Unlock()
|
|
|
|
|
2019-06-19 20:30:48 +01:00
|
|
|
return &prefixWriter{
|
|
|
|
PrefixWriter: writer,
|
|
|
|
prefix: prefix,
|
|
|
|
id: "",
|
|
|
|
buffer: make([]byte, 0, writer.maxline),
|
|
|
|
}
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements io.Writer that prefixes lines.
|
|
|
|
func (writer *PrefixWriter) Write(data []byte) (int, error) {
|
|
|
|
return writer.root.Write(data)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Write implements io.Writer that prefixes lines.
|
2019-01-02 18:07:49 +00:00
|
|
|
func (writer *prefixWriter) Write(data []byte) (int, error) {
|
|
|
|
if len(data) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2019-06-19 20:30:48 +01:00
|
|
|
writer.local.Lock()
|
|
|
|
defer writer.local.Unlock()
|
|
|
|
|
2019-01-08 15:24:15 +00:00
|
|
|
var newID string
|
|
|
|
if writer.id == "" {
|
2020-04-15 14:57:14 +01:00
|
|
|
if start := bytes.Index(data, []byte("Node ")); start > 0 {
|
|
|
|
if end := bytes.Index(data[start:], []byte(" started")); end > 0 {
|
2019-01-08 15:24:15 +00:00
|
|
|
newID = string(data[start+5 : start+end])
|
|
|
|
if len(newID) > maxIDLength {
|
|
|
|
newID = newID[:maxIDLength]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 18:07:49 +00:00
|
|
|
buffer := data
|
|
|
|
|
|
|
|
// buffer everything that hasn't been written yet
|
|
|
|
if len(writer.buffer) > 0 {
|
2019-05-29 14:14:25 +01:00
|
|
|
buffer = append(writer.buffer, data...) // nolint gocritic
|
2019-01-02 18:07:49 +00:00
|
|
|
defer func() {
|
|
|
|
writer.buffer = buffer
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
defer func() {
|
|
|
|
if len(buffer) > 0 {
|
|
|
|
writer.buffer = append(writer.buffer, buffer...)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.mu.Lock()
|
|
|
|
defer writer.mu.Unlock()
|
|
|
|
|
2019-01-08 15:24:15 +00:00
|
|
|
if newID != "" {
|
|
|
|
writer.id = newID
|
|
|
|
}
|
|
|
|
|
2019-01-02 18:07:49 +00:00
|
|
|
prefix := writer.prefix
|
2019-01-08 15:24:15 +00:00
|
|
|
id := writer.id
|
2020-09-10 14:37:16 +01:00
|
|
|
timeText := writer.nowFunc().Format(timeFormat)
|
2019-01-02 18:07:49 +00:00
|
|
|
for len(buffer) > 0 {
|
2020-09-10 14:37:16 +01:00
|
|
|
pos := bytes.IndexByte(buffer, '\n')
|
|
|
|
insertbreak := false
|
|
|
|
|
|
|
|
// did not find a linebreak
|
|
|
|
if pos < 0 {
|
|
|
|
// wait for more data, if we haven't reached maxline
|
2019-01-02 18:07:49 +00:00
|
|
|
if len(buffer) < writer.maxline {
|
|
|
|
return len(data), nil
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 14:37:16 +01:00
|
|
|
|
|
|
|
// try to find a nice place where to break the line
|
2019-01-02 18:07:49 +00:00
|
|
|
if pos < 0 || pos > writer.maxline {
|
2020-09-10 14:37:16 +01:00
|
|
|
pos = writer.maxline - 1
|
2019-01-02 18:07:49 +00:00
|
|
|
for p := pos; p >= writer.maxline*2/3; p-- {
|
2020-09-10 14:37:16 +01:00
|
|
|
// is there a space we can break on?
|
2019-01-02 18:07:49 +00:00
|
|
|
if buffer[p] == ' ' {
|
|
|
|
pos = p
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 14:37:16 +01:00
|
|
|
insertbreak = true
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 15:24:15 +00:00
|
|
|
_, err := fmt.Fprintf(writer.dst, "%-*s %-*s %s | ", writer.prefixlen, prefix, maxIDLength, id, timeText)
|
2019-01-02 18:07:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return len(data), err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = writer.dst.Write(buffer[:pos])
|
|
|
|
buffer = buffer[pos:]
|
2020-09-10 14:37:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return len(data), err
|
|
|
|
}
|
|
|
|
_, err = writer.dst.Write([]byte{'\n'})
|
2019-01-02 18:07:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return len(data), err
|
|
|
|
}
|
|
|
|
|
2020-09-10 14:37:16 +01:00
|
|
|
// remove the linebreak from buffer, if it's not an insert
|
|
|
|
if !insertbreak && len(buffer) > 0 {
|
|
|
|
buffer = buffer[1:]
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prefix = ""
|
2019-01-08 15:24:15 +00:00
|
|
|
id = ""
|
2020-09-10 14:37:16 +01:00
|
|
|
timeText = emptyTimeField
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return len(data), nil
|
|
|
|
}
|