improve logging and make it configurable

This commit is contained in:
Marco Cadetg
2025-08-02 14:39:54 +02:00
parent ba9e234432
commit a7bf42e2dc
3 changed files with 36 additions and 27 deletions

View File

@@ -10,10 +10,7 @@ use std::time::{Duration, Instant, SystemTime};
use crate::network::{
capture::{CaptureConfig, PacketReader, setup_packet_capture},
merge::{
create_connection_from_packet,
merge_packet_into_connection,
},
merge::{create_connection_from_packet, merge_packet_into_connection},
parser::{PacketParser, ParsedPacket, ParserConfig},
platform::create_process_lookup,
services::ServiceLookup,
@@ -187,7 +184,10 @@ impl App {
Ok((capture, device_name)) => {
// Store the actual interface name being used
*current_interface.write().unwrap() = Some(device_name.clone());
info!("Packet capture started successfully on interface: {}", device_name);
info!(
"Packet capture started successfully on interface: {}",
device_name
);
let mut reader = PacketReader::new(capture);
let mut packets_read = 0u64;
let mut last_log = Instant::now();
@@ -208,8 +208,8 @@ impl App {
info!("First packet captured! Size: {} bytes", packet.len());
}
// Log every 100 packets or every 5 seconds
if packets_read % 100 == 0
// Log every 10000 packets or every 5 seconds
if packets_read % 10000 == 0
|| last_log.elapsed() > Duration::from_secs(5)
{
info!("Read {} packets so far", packets_read);
@@ -317,7 +317,7 @@ impl App {
.fetch_add(batch.len() as u64, Ordering::Relaxed);
// Log progress
if total_processed % 100 == 0 || last_log.elapsed() > Duration::from_secs(5) {
if total_processed % 10000 == 0 || last_log.elapsed() > Duration::from_secs(5) {
debug!(
"Processor {}: {} packets processed ({} parsed)",
id, total_processed, parsed_count

View File

@@ -14,11 +14,6 @@ mod network;
mod ui;
fn main() -> Result<()> {
// Set up logging
setup_logging()?;
info!("Starting RustNet Monitor");
// Parse command line arguments
let matches = Command::new("rustnet")
.version("0.1.0")
@@ -54,7 +49,26 @@ fn main() -> Result<()> {
.help("Disable deep packet inspection")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("log-level")
.short('l')
.long("log-level")
.value_name("LEVEL")
.help("Set the log level")
.value_parser(clap::value_parser!(LevelFilter))
.default_value("info")
.required(false),
)
.get_matches();
// Set up logging
setup_logging(
matches
.get_one::<LevelFilter>("log-level")
.cloned()
.unwrap_or(LevelFilter::Info),
)?;
info!("Starting RustNet Monitor");
// Build configuration from command line arguments
let mut config = app::Config::default();
@@ -106,7 +120,7 @@ fn main() -> Result<()> {
Ok(())
}
fn setup_logging() -> Result<()> {
fn setup_logging(level: LevelFilter) -> Result<()> {
// Create logs directory if it doesn't exist
let log_dir = Path::new("logs");
if !log_dir.exists() {
@@ -118,11 +132,7 @@ fn setup_logging() -> Result<()> {
let log_file_path = log_dir.join(format!("rustnet_{}.log", timestamp));
// Initialize the logger
WriteLogger::init(
LevelFilter::Debug,
LogConfig::default(),
File::create(log_file_path)?,
)?;
WriteLogger::init(level, LogConfig::default(), File::create(log_file_path)?)?;
Ok(())
}
@@ -251,13 +261,13 @@ fn run_ui_loop<B: ratatui::prelude::Backend>(
error!("Failed to copy to clipboard: {}", e);
ui_state.clipboard_message = Some((
format!("Failed to copy: {}", e),
std::time::Instant::now()
std::time::Instant::now(),
));
} else {
info!("Copied {} to clipboard", remote_addr);
ui_state.clipboard_message = Some((
format!("Copied {} to clipboard", remote_addr),
std::time::Instant::now()
std::time::Instant::now(),
));
}
}
@@ -265,7 +275,7 @@ fn run_ui_loop<B: ratatui::prelude::Backend>(
error!("Failed to access clipboard: {}", e);
ui_state.clipboard_message = Some((
format!("Clipboard error: {}", e),
std::time::Instant::now()
std::time::Instant::now(),
));
}
}

View File

@@ -1,5 +1,4 @@
use log::{debug, error, info, warn};
use procfs::net::tcp;
use log::{debug, warn};
// network/merge.rs - Connection merging and update utilities
use crate::network::dpi::DpiResult;
@@ -10,7 +9,7 @@ use std::time::{Instant, SystemTime};
/// Update TCP connection state based on observed flags and current state
/// This implements the TCP state machine according to RFC 793
fn update_tcp_state(current_state: TcpState, flags: &TcpFlags, is_outgoing: bool) -> TcpState {
info!(
debug!(
"Updating TCP state: current_state={:?}, flags={:?}, is_outgoing={}",
current_state, flags, is_outgoing
);
@@ -77,7 +76,7 @@ pub fn merge_packet_into_connection(
&parsed.tcp_flags.unwrap(),
parsed.is_outgoing,
);
info!(
debug!(
"Updated TCP state: {:?} -> {:?}",
current_tcp_state, new_tcp_state
);
@@ -113,7 +112,7 @@ pub fn create_connection_from_packet(parsed: &ParsedPacket, now: SystemTime) ->
tcp_flags,
parsed.is_outgoing,
));
info!(
debug!(
"Created connection from packet: {:?} -> {:?}, old state: {:?}, new state: {:?}",
parsed.local_addr, parsed.remote_addr, old_state, conn.protocol_state
);