git » chasquid » commit cf36003

trace: Add an EventLog

author Alberto Bertogli
2016-10-09 17:58:48 UTC
committer Alberto Bertogli
2016-10-09 23:51:05 UTC
parent 112e492c3a68f511644ecc16baf6d207f048c055

trace: Add an EventLog

This patch adds an EventLog wrapper to the trace module, which will be
used in the future to track long-lived objects.

internal/trace/trace.go +41 -0

diff --git a/internal/trace/trace.go b/internal/trace/trace.go
index bcddf04..6f705b2 100644
--- a/internal/trace/trace.go
+++ b/internal/trace/trace.go
@@ -80,3 +80,44 @@ func (t *Trace) Error(err error) error {
 func (t *Trace) Finish() {
 	t.t.Finish()
 }
+
+type EventLog struct {
+	family string
+	title  string
+	e      nettrace.EventLog
+}
+
+func NewEventLog(family, title string) *EventLog {
+	return &EventLog{family, title, nettrace.NewEventLog(family, title)}
+}
+
+func (e *EventLog) Printf(format string, a ...interface{}) {
+	e.e.Printf(format, a...)
+
+	if glog.V(0) {
+		msg := fmt.Sprintf("%s %s: %s", e.family, e.title,
+			quote(fmt.Sprintf(format, a...)))
+		glog.InfoDepth(1, msg)
+	}
+}
+
+func (e *EventLog) Debugf(format string, a ...interface{}) {
+	e.e.Printf(format, a...)
+
+	if glog.V(2) {
+		msg := fmt.Sprintf("%s %s: %s", e.family, e.title,
+			quote(fmt.Sprintf(format, a...)))
+		glog.InfoDepth(1, msg)
+	}
+}
+
+func (e *EventLog) Errorf(format string, a ...interface{}) error {
+	err := fmt.Errorf(format, a...)
+	e.e.Errorf("error: %v", err)
+
+	if glog.V(0) {
+		msg := fmt.Sprintf("%s %s: error: %v", e.family, e.title, err)
+		glog.InfoDepth(1, msg)
+	}
+	return err
+}