chore: Add verbose logging to local capture for debugging purposes

This commit is contained in:
Jose Luis Montañes Ojados
2026-01-19 15:28:16 +01:00
parent b8984f25a0
commit 4fa44fb9c7
2 changed files with 16 additions and 2 deletions

Binary file not shown.

View File

@@ -9,6 +9,7 @@ import (
"strings" "strings"
"sync" "sync"
"telephony-inspector/internal/logger"
"telephony-inspector/internal/sip" "telephony-inspector/internal/sip"
) )
@@ -50,6 +51,8 @@ func (c *LocalCapturer) Start(iface string, port int) error {
args := []string{"-l", "-nn", "-A", "-s", "0", "-i", iface, "port", fmt.Sprintf("%d", port)} args := []string{"-l", "-nn", "-A", "-s", "0", "-i", iface, "port", fmt.Sprintf("%d", port)}
c.cmd = exec.CommandContext(ctx, "tcpdump", args...) c.cmd = exec.CommandContext(ctx, "tcpdump", args...)
logger.Info("Starting local capture: tcpdump %v", args)
stdout, err := c.cmd.StdoutPipe() stdout, err := c.cmd.StdoutPipe()
if err != nil { if err != nil {
c.mu.Lock() c.mu.Lock()
@@ -73,6 +76,8 @@ func (c *LocalCapturer) Start(iface string, port int) error {
return fmt.Errorf("failed to start tcpdump: %w", err) return fmt.Errorf("failed to start tcpdump: %w", err)
} }
logger.Info("Local capture started successfully")
// Process stdout in goroutine // Process stdout in goroutine
go c.processStream(stdout) go c.processStream(stdout)
@@ -90,6 +95,7 @@ func (c *LocalCapturer) Stop() {
if !c.running { if !c.running {
return return
} }
logger.Info("Stopping local capture")
c.running = false c.running = false
if c.cancel != nil { if c.cancel != nil {
@@ -130,9 +136,11 @@ func (c *LocalCapturer) processStream(r io.Reader) {
} }
line := scanner.Text() line := scanner.Text()
// logger.Debug("Stdout: %s", line) // Commented out to reduce noise, enable if needed
// Detect start of SIP message // Detect start of SIP message
if isSIPStart(line) { if isSIPStart(line) {
logger.Debug("SIP Start detected: %s", line)
// If we were building a message, parse it // If we were building a message, parse it
if buffer.Len() > 0 { if buffer.Len() > 0 {
c.parseAndEmit(buffer.String()) c.parseAndEmit(buffer.String())
@@ -159,8 +167,10 @@ func (c *LocalCapturer) processErrors(r io.Reader) {
text := scanner.Text() text := scanner.Text()
// tcpdump prints "listening on..." to stderr, ignore it // tcpdump prints "listening on..." to stderr, ignore it
if strings.Contains(text, "listening on") { if strings.Contains(text, "listening on") {
logger.Info("tcpdump: %s", text)
continue continue
} }
logger.Error("tcpdump stderr: %s", text)
if c.OnError != nil { if c.OnError != nil {
c.OnError(fmt.Errorf("tcpdump: %s", text)) c.OnError(fmt.Errorf("tcpdump: %s", text))
} }
@@ -170,12 +180,16 @@ func (c *LocalCapturer) processErrors(r io.Reader) {
func (c *LocalCapturer) parseAndEmit(raw string) { func (c *LocalCapturer) parseAndEmit(raw string) {
packet, err := sip.Parse(raw) packet, err := sip.Parse(raw)
if err != nil { if err != nil {
logger.Error("Error parsing SIP packet: %v", err)
if c.OnError != nil { if c.OnError != nil {
c.OnError(err) c.OnError(err)
} }
return return
} }
if packet != nil && c.OnPacket != nil { if packet != nil {
c.OnPacket(packet) logger.Debug("Packet parsed: %s %s -> %s", packet.Method, packet.SourceIP, packet.DestIP)
if c.OnPacket != nil {
c.OnPacket(packet)
}
} }
} }