From cf289bdc8d2eaee30d98431e8b7d3ec97c178a92 Mon Sep 17 00:00:00 2001 From: Marc Frei Date: Mon, 1 Apr 2024 07:04:27 +0200 Subject: [PATCH] Work on logging of NTP values --- net/ntp/logging.go | 22 ++++++++++++++++++++++ timeservicex.go | 8 ++++++++ 2 files changed, 30 insertions(+) diff --git a/net/ntp/logging.go b/net/ntp/logging.go index 7723b4ab..bf96cb9a 100644 --- a/net/ntp/logging.go +++ b/net/ntp/logging.go @@ -1,9 +1,21 @@ package ntp import ( + "log/slog" + "go.uber.org/zap/zapcore" ) +type Time32LogValuer struct { + T Time32 +} + +func (v Time32LogValuer) LogValue() slog.Value { + return slog.GroupValue( + slog.Uint64("Seconds", uint64(v.T.Seconds)), + slog.Uint64("Fraction", uint64(v.T.Fraction))) +} + type Time32Marshaler struct { T Time32 } @@ -14,6 +26,16 @@ func (m Time32Marshaler) MarshalLogObject(enc zapcore.ObjectEncoder) error { return nil } +type Time64LogValuer struct { + T Time64 +} + +func (v Time64LogValuer) LogValue() slog.Value { + return slog.GroupValue( + slog.Uint64("Seconds", uint64(v.T.Seconds)), + slog.Uint64("Fraction", uint64(v.T.Fraction))) +} + type Time64Marshaler struct { T Time64 } diff --git a/timeservicex.go b/timeservicex.go index 218f7fef..105cf178 100644 --- a/timeservicex.go +++ b/timeservicex.go @@ -3,10 +3,12 @@ package main import ( + "context" "log/slog" "time" "example.com/scion-time/driver/clock" + "example.com/scion-time/net/ntp" ) func runX() { @@ -18,4 +20,10 @@ func runX() { log.Debug("local clock", slog.Time("now", clk.Now())) clk.Step(-1 * time.Second) log.Debug("local clock", slog.Time("now", clk.Now())) + + now64 := ntp.Time64FromTime(time.Now()) + log.LogAttrs(context.Background(), slog.LevelDebug, "test", + slog.Any("now", now64)) + log.LogAttrs(context.Background(), slog.LevelDebug, "test", + slog.Any("now", ntp.Time64LogValuer{T: now64})) }