Files
govpp-snmp-agentx/logger/logger.go
2025-06-10 23:52:49 +02:00

46 lines
980 B
Go

// Copyright 2025, IPng Networks GmbH, Pim van Pelt <pim@ipng.ch>
package logger
import (
"fmt"
"log"
"path/filepath"
"runtime"
"govpp-snmp-agentx/config"
)
// 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 global debug is enabled
func Debugf(format string, args ...interface{}) {
if config.Debug {
logf(format, args...)
}
}