62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package okit
|
|
|
|
import (
|
|
"bufio"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/golang/protobuf/jsonpb"
|
|
"go.pitz.tech/okit/pb"
|
|
)
|
|
|
|
// Format defines an abstraction for writing entries to stdout/stderr. This is predominantly used in report log, trace,
|
|
// and event information from the process.
|
|
type Format interface {
|
|
Marshal(writer *bufio.Writer, entry *pb.Entry) error
|
|
}
|
|
|
|
// JSONFormat writes entries using JSON encoded protocol buffers.
|
|
type JSONFormat struct {
|
|
Marshaler jsonpb.Marshaler
|
|
}
|
|
|
|
func (f JSONFormat) Marshal(writer *bufio.Writer, entry *pb.Entry) error {
|
|
f.Marshaler.Marshal(writer, entry)
|
|
return writer.WriteByte('\n')
|
|
}
|
|
|
|
// TextFormat writes entries using a custom text format.
|
|
type TextFormat struct{}
|
|
|
|
func (f TextFormat) Marshal(writer *bufio.Writer, entry *pb.Entry) error {
|
|
sink.WriteString(entry.Timestamp.AsTime().Format(time.RFC3339))
|
|
sink.WriteByte('\t')
|
|
sink.WriteString(strings.ToUpper(entry.Kind.String()))
|
|
sink.WriteByte('\t')
|
|
|
|
switch v := entry.GetValue().(type) {
|
|
case *pb.Entry_String_:
|
|
sink.WriteString(v.String_)
|
|
case *pb.Entry_Double:
|
|
sink.WriteString(strconv.FormatFloat(v.Double, 'f', 5, 64))
|
|
case *pb.Entry_Duration:
|
|
sink.WriteString(v.Duration.AsDuration().String())
|
|
}
|
|
sink.WriteByte('\t')
|
|
sink.WriteString(entry.Scope)
|
|
sink.WriteByte('\t')
|
|
|
|
for _, tag := range entry.Tags {
|
|
qs := tag.AsQueryString()
|
|
if qs == "" {
|
|
continue
|
|
}
|
|
|
|
sink.WriteString(qs)
|
|
sink.WriteByte('&')
|
|
}
|
|
|
|
return sink.WriteByte('\n')
|
|
}
|