Move Go code to src/

This commit is contained in:
Pim van Pelt
2025-06-17 00:47:08 +02:00
parent c0bcdd5449
commit 7f81b51c1f
61 changed files with 3 additions and 3 deletions

52
src/logger/logger.go Normal file
View File

@@ -0,0 +1,52 @@
// Copyright 2025, IPng Networks GmbH, Pim van Pelt <pim@ipng.ch>
package logger
import (
"fmt"
"path/filepath"
"runtime"
"govpp-snmp-agentx/config"
)
// getCallerInfo returns caller information in the format "file.go:function"
func getCallerInfo() string {
pc, file, _, ok := runtime.Caller(2) // Skip getCallerInfo and Printf/Debugf
if !ok {
return "unknown:unknown"
}
fn := runtime.FuncForPC(pc)
if fn == nil {
return "unknown:unknown"
}
funcName := filepath.Base(fn.Name())
fileName := filepath.Base(file)
return fmt.Sprintf("%s:%s", fileName, funcName)
}
// Printf logs a message with caller information in SYSLOG style
func Printf(format string, args ...interface{}) {
caller := getCallerInfo()
message := fmt.Sprintf(format, args...)
syslogMessage := fmt.Sprintf("INFO %s %s", caller, message)
fmt.Println(syslogMessage)
}
// Debugf logs a debug message with caller information if global debug is enabled
func Debugf(format string, args ...interface{}) {
if config.Debug {
caller := getCallerInfo()
message := fmt.Sprintf(format, args...)
syslogMessage := fmt.Sprintf("DEBUG %s %s", caller, message)
fmt.Println(syslogMessage)
}
}
// Sync flushes any buffered log entries (no-op for fmt.Println)
func Sync() {
// No buffering with fmt.Println, so this is a no-op
}

113
src/logger/logger_test.go Normal file
View File

@@ -0,0 +1,113 @@
// Copyright 2025, IPng Networks GmbH, Pim van Pelt <pim@ipng.ch>
package logger
import (
"bytes"
"io"
"os"
"strings"
"testing"
"govpp-snmp-agentx/config"
)
func TestPrintf(t *testing.T) {
// Capture stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
Printf("test message: %s", "hello")
// Close writer and restore stdout
w.Close()
os.Stdout = oldStdout
// Read captured output
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
output := buf.String()
// Check output format: "INFO file.go:function message"
if !strings.HasPrefix(output, "INFO ") {
t.Errorf("Expected output to start with 'INFO ', got: %s", output)
}
if !strings.Contains(output, "logger_test.go:logger.TestPrintf") {
t.Errorf("Expected output to contain caller info, got: %s", output)
}
if !strings.Contains(output, "test message: hello") {
t.Errorf("Expected output to contain message, got: %s", output)
}
}
func TestDebugfWithDebugEnabled(t *testing.T) {
// Save original debug state
originalDebug := config.Debug
defer func() { config.Debug = originalDebug }()
// Enable debug
config.Debug = true
// Capture stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
Debugf("debug message: %s", "test")
// Close writer and restore stdout
w.Close()
os.Stdout = oldStdout
// Read captured output
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
output := buf.String()
// Check output format: "DEBUG file.go:function message"
if !strings.HasPrefix(output, "DEBUG ") {
t.Errorf("Expected output to start with 'DEBUG ', got: %s", output)
}
if !strings.Contains(output, "debug message: test") {
t.Errorf("Expected output to contain message, got: %s", output)
}
}
func TestDebugfWithDebugDisabled(t *testing.T) {
// Save original debug state
originalDebug := config.Debug
defer func() { config.Debug = originalDebug }()
// Disable debug
config.Debug = false
// Capture stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
Debugf("debug message: %s", "test")
// Close writer and restore stdout
w.Close()
os.Stdout = oldStdout
// Read captured output
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
output := buf.String()
// Should be empty when debug is disabled
if output != "" {
t.Errorf("Expected no output when debug is disabled, got: %s", output)
}
}
func TestSync(t *testing.T) {
// Test that Sync doesn't panic (it's a no-op now)
Sync()
}