postfix-log-parser 0.2.0

高性能模块化Postfix日志解析器,经3.2GB生产数据验证,SMTPD事件100%准确率
Documentation
use postfix_log_parser::master_parser::MasterParser;
use std::io::{self, BufRead};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let parser = MasterParser::new();
    let stdin = io::stdin();

    let mut total_lines = 0;
    let mut parsed_successfully = 0;
    let mut postfix_script_events = 0;

    println!("🧪 测试 POSTFIX-SCRIPT 组件解析器");
    println!("从标准输入读取日志行...\n");

    for line in stdin.lock().lines() {
        let line = line?;
        total_lines += 1;

        if line.trim().is_empty() {
            continue;
        }

        let result = parser.parse(&line);

        if let Some(event) = &result.event {
            parsed_successfully += 1;

            if event.component == "postfix-script" {
                postfix_script_events += 1;
                println!(
                    "✅ POSTFIX-SCRIPT 事件 #{}: {}",
                    postfix_script_events,
                    event.event.event_type()
                );
                println!("   原始日志: {}", line);
                println!("   置信度: {:.2}", result.confidence);

                // 显示事件详情
                if let postfix_log_parser::events::ComponentEvent::PostfixScript(ps_event) =
                    &event.event
                {
                    match ps_event {
                        postfix_log_parser::events::postfix_script::PostfixScriptEvent::SystemOperation { operation, .. } => {
                            println!("   操作类型: {:?}", operation);
                        }
                        postfix_log_parser::events::postfix_script::PostfixScriptEvent::FatalError { error, .. } => {
                            println!("   致命错误: {:?}", error);
                        }
                        postfix_log_parser::events::postfix_script::PostfixScriptEvent::Warning { warning, .. } => {
                            println!("   警告类型: {:?}", warning);
                        }
                    }
                }
                println!();
            }
        }

        // 每处理100行显示一次进度
        if total_lines % 100 == 0 {
            print!(
                "\r📊 已处理: {} 行, 成功解析: {}, POSTFIX-SCRIPT: {}",
                total_lines, parsed_successfully, postfix_script_events
            );
            std::io::Write::flush(&mut std::io::stdout()).unwrap();
        }
    }

    println!("\n\n📈 最终统计:");
    println!("  总行数: {}", total_lines);
    println!("  成功解析: {}", parsed_successfully);
    println!("  POSTFIX-SCRIPT 事件: {}", postfix_script_events);
    println!(
        "  解析成功率: {:.1}%",
        parsed_successfully as f64 / total_lines as f64 * 100.0
    );

    if postfix_script_events > 0 {
        println!(
            "  POSTFIX-SCRIPT 占比: {:.1}%",
            postfix_script_events as f64 / parsed_successfully as f64 * 100.0
        );
    }

    Ok(())
}