Files
govpp-snmp-agentx/logger/logger.go

44 lines
914 B
Go

package logger
import (
"fmt"
"log"
"path/filepath"
"runtime"
"govpp-snmp-example/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...)
}
}