From fa6e389ed8181a9a74a278a6e41e79a75260a2a5 Mon Sep 17 00:00:00 2001 From: Peter Portante Date: Thu, 20 Apr 2023 18:12:44 -0400 Subject: [PATCH] Add timestamp to client log entries --- gosmee/client.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/gosmee/client.go b/gosmee/client.go index bba77c3..db3f4bb 100644 --- a/gosmee/client.go +++ b/gosmee/client.go @@ -127,18 +127,22 @@ func (c goSmee) parse(data []byte) (payloadMsg, error) { pm.contentType = pv } case "timestamp": - var ts string + var dt time.Time if pv, ok := payloadValue.(float64); ok { + var ts string ts = fmt.Sprintf("%.f", pv) ts = ts[:len(ts)-3] + + tsInt, err := strconv.ParseInt(ts, 10, 64) + if err != nil { + return payloadMsg{}, fmt.Errorf("cannot convert timestamp to int64") + } + dt = time.Unix(tsInt, 0) + } else { + dt = time.Now() } - tsInt, err := strconv.ParseInt(ts, 10, 64) - if err != nil { - return payloadMsg{}, fmt.Errorf("cannot convert timestamp to int64") - } - dt := time.Unix(tsInt, 0) - pm.timestamp = dt.Format("20060102T15h04") + pm.timestamp = dt.Format("2006-01-02T15.04.01.000") } } @@ -254,7 +258,7 @@ func (c goSmee) replayData(pm payloadMsg) error { msg = fmt.Sprintf("%s %s", pm.eventID, msg) } - msg = fmt.Sprintf("%s replayed to %s, status: %s", msg, ansi.Color(c.targetURL, "green+ub"), ansi.Color(fmt.Sprintf("%d", resp.StatusCode), "blue+b")) + msg = fmt.Sprintf("%s %s replayed to %s, status: %s", pm.timestamp, msg, ansi.Color(c.targetURL, "green+ub"), ansi.Color(fmt.Sprintf("%d", resp.StatusCode), "blue+b")) if resp.StatusCode > 299 { msg = fmt.Sprintf("%s, error: %s", msg, resp.Status) }