From 29d041745284f6a005dba8685d9d209bd83e7830 Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Mon, 9 Jun 2025 18:06:59 +0200 Subject: [PATCH] Add a logger that auto-includes file+callsite --- ifmib/ifmib.go | 27 +++++++++------------------ logger/logger.go | 42 ++++++++++++++++++++++++++++++++++++++++++ vppstats/stats.go | 12 ++++++------ 3 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 logger/logger.go diff --git a/ifmib/ifmib.go b/ifmib/ifmib.go index 8bd6e9f..56bea75 100644 --- a/ifmib/ifmib.go +++ b/ifmib/ifmib.go @@ -2,7 +2,6 @@ package ifmib import ( "fmt" - "log" "sync" "time" @@ -10,6 +9,8 @@ import ( "github.com/posteo/go-agentx/pdu" "github.com/posteo/go-agentx/value" "go.fd.io/govpp/api" + + "govpp-snmp-example/logger" ) // IF-MIB OID bases: @@ -85,9 +86,7 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) { m.mutex.Lock() defer m.mutex.Unlock() - if m.debug { - log.Printf("Updating IF-MIB with %d interfaces", len(interfaceStats.Interfaces)) - } + logger.Debugf(m.debug, "Updating IF-MIB with %d interfaces", len(interfaceStats.Interfaces)) // Clear existing entries m.handler = &agentx.ListHandler{} @@ -95,9 +94,7 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) { // Add new entries for _, iface := range interfaceStats.Interfaces { - if m.debug { - log.Printf("Processing interface %d (%s)", iface.InterfaceIndex, iface.InterfaceName) - } + logger.Debugf(m.debug, "Processing interface %d (%s)", iface.InterfaceIndex, iface.InterfaceName) m.stats[iface.InterfaceIndex] = &iface m.addInterfaceToMIB(&iface) } @@ -108,12 +105,10 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) { } if m.ifXTableSession != nil { m.ifXTableSession.Handler = m.handler - log.Printf("Updated session handlers with new IF-MIB data") + logger.Printf("Updated session handlers with new IF-MIB data") } - if m.debug { - log.Printf("IF-MIB now contains %d interfaces", len(m.stats)) - } + logger.Debugf(m.debug, "IF-MIB now contains %d interfaces", len(m.stats)) } func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) { @@ -125,9 +120,7 @@ func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) { // Add ifXTable (extended interface table) entries m.addIfXTable(iface, idx) - if m.debug { - log.Printf("Added interface %d (%s) to IF-MIB with SNMP index %d", iface.InterfaceIndex, iface.InterfaceName, idx) - } + logger.Debugf(m.debug, "Added interface %d (%s) to IF-MIB with SNMP index %d", iface.InterfaceIndex, iface.InterfaceName, idx) } func (m *InterfaceMIB) addIfEntry(iface *api.InterfaceCounters, idx int) { @@ -344,9 +337,7 @@ func (m *InterfaceMIB) RegisterWithClient(client *agentx.Client) error { return fmt.Errorf("failed to register ifXTable: %v", err) } - if m.debug { - log.Printf("Registered IF-MIB ifEntry at OID %s", ifEntryOID) - log.Printf("Registered IF-MIB ifXTable at OID %s", ifXTableOID) - } + logger.Debugf(m.debug, "Registered IF-MIB ifEntry at OID %s", ifEntryOID) + logger.Debugf(m.debug, "Registered IF-MIB ifXTable at OID %s", ifXTableOID) return nil } diff --git a/logger/logger.go b/logger/logger.go new file mode 100644 index 0000000..ada8fd4 --- /dev/null +++ b/logger/logger.go @@ -0,0 +1,42 @@ +package logger + +import ( + "fmt" + "log" + "path/filepath" + "runtime" +) + +// logf logs a message with automatic caller information (file:function) +func logf(format string, args ...interface{}) { + pc, file, _, ok := runtime.Caller(2) + if !ok { + log.Printf(format, args...) + return + } + + fn := runtime.FuncForPC(pc) + if fn == nil { + log.Printf(format, args...) + return + } + + funcName := filepath.Base(fn.Name()) + fileName := filepath.Base(file) + + prefix := fmt.Sprintf("%s:%s", fileName, funcName) + message := fmt.Sprintf(format, args...) + log.Printf("%s %s", prefix, message) +} + +// Printf logs a message with caller information +func Printf(format string, args ...interface{}) { + logf(format, args...) +} + +// Debugf logs a debug message with caller information if debug is true +func Debugf(debug bool, format string, args ...interface{}) { + if debug { + logf(format, args...) + } +} \ No newline at end of file diff --git a/vppstats/stats.go b/vppstats/stats.go index 959bf84..2c2efbd 100644 --- a/vppstats/stats.go +++ b/vppstats/stats.go @@ -7,6 +7,8 @@ import ( "go.fd.io/govpp/adapter/statsclient" "go.fd.io/govpp/api" "go.fd.io/govpp/core" + + "govpp-snmp-example/logger" ) type StatsCallback func(*api.InterfaceStats) @@ -17,9 +19,7 @@ func StartStatsRoutine(statsSocketPath string, period time.Duration, callback St } func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback, debug bool) { - if debug { - log.Printf("Starting VPP stats routine with socket: %s, period: %v", statsSocketPath, period) - } + logger.Debugf(debug, "Starting VPP stats routine with socket: %s, period: %v", statsSocketPath, period) // Create stats client client := statsclient.NewStatsClient(statsSocketPath) @@ -52,17 +52,17 @@ func queryInterfaceStats(c *core.StatsConnection, callback StatsCallback, debug // Use the GetInterfaceStats method - this is the correct approach if err := c.GetInterfaceStats(stats); err != nil { - log.Printf("Failed to get interface stats: %v", err) + logger.Printf("Failed to get interface stats: %v", err) return } // Always log basic info - log.Printf("Retrieved stats for %d interfaces", len(stats.Interfaces)) + logger.Printf("Retrieved stats for %d interfaces", len(stats.Interfaces)) // Debug logging for individual interfaces if debug { for _, iface := range stats.Interfaces { - log.Printf("Interface %d (%s): RX %d pkts/%d bytes, TX %d pkts/%d bytes", + logger.Debugf(debug, "Interface %d (%s): RX %d pkts/%d bytes, TX %d pkts/%d bytes", iface.InterfaceIndex, iface.InterfaceName, iface.Rx.Packets, iface.Rx.Bytes, iface.Tx.Packets, iface.Tx.Bytes)