github.com/v2fly/tools@v0.100.0/internal/event/export/printer.go (about)

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package export
     6  
     7  import (
     8  	"io"
     9  
    10  	"github.com/v2fly/tools/internal/event/core"
    11  	"github.com/v2fly/tools/internal/event/keys"
    12  	"github.com/v2fly/tools/internal/event/label"
    13  )
    14  
    15  type Printer struct {
    16  	buffer [128]byte
    17  }
    18  
    19  func (p *Printer) WriteEvent(w io.Writer, ev core.Event, lm label.Map) {
    20  	buf := p.buffer[:0]
    21  	if !ev.At().IsZero() {
    22  		w.Write(ev.At().AppendFormat(buf, "2006/01/02 15:04:05 "))
    23  	}
    24  	msg := keys.Msg.Get(lm)
    25  	io.WriteString(w, msg)
    26  	if err := keys.Err.Get(lm); err != nil {
    27  		if msg != "" {
    28  			io.WriteString(w, ": ")
    29  		}
    30  		io.WriteString(w, err.Error())
    31  	}
    32  	for index := 0; ev.Valid(index); index++ {
    33  		l := ev.Label(index)
    34  		if !l.Valid() || l.Key() == keys.Msg || l.Key() == keys.Err {
    35  			continue
    36  		}
    37  		io.WriteString(w, "\n\t")
    38  		io.WriteString(w, l.Key().Name())
    39  		io.WriteString(w, "=")
    40  		l.Key().Format(w, buf, l)
    41  	}
    42  	io.WriteString(w, "\n")
    43  }