Add a logger that auto-includes file+callsite
This commit is contained in:
@ -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
|
||||
}
|
||||
|
42
logger/logger.go
Normal file
42
logger/logger.go
Normal file
@ -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...)
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
Reference in New Issue
Block a user