log-full 0.0.1

A simple, asynchronous log library
Documentation
//! 配置管理模块
//! 
//! 提供灵活的配置加载和管理功能

use crate::error::{ LogResult};
use std::collections::HashMap;
use std::path::Path;

/// 日志配置结构
#[derive(Debug, Clone)]
pub struct LogConfig {
    /// 日志级别
    pub level: String,
    /// 输出文件路径
    pub file_path: Option<String>,
    /// 是否启用控制台输出
    pub console_output: bool,
    /// 是否启用详细模式
    pub verbose: bool,
    /// 日志格式
    pub format: LogFormat,
    /// 文件轮转配置
    pub rotation: Option<RotationConfig>,
    /// 关键词高亮配置
    pub highlight: HighlightConfig,
    /// 过滤器配置
    pub filters: Vec<FilterConfig>,
    /// 自定义字段
    pub custom_fields: HashMap<String, String>,
}

/// 日志格式配置
#[derive(Debug, Clone)]
pub struct LogFormat {
    /// 时间格式
    pub time_format: String,
    /// 是否显示文件名
    pub show_file: bool,
    /// 是否显示行号
    pub show_line: bool,
    /// 是否显示模块路径
    pub show_module: bool,
    /// 自定义格式模板
    pub template: Option<String>,
}

/// 文件轮转配置
#[derive(Debug, Clone)]
pub struct RotationConfig {
    /// 最大文件大小(字节)
    pub max_size: u64,
    /// 保留的文件数量
    pub max_files: u32,
    /// 是否启用压缩
    pub compress: bool,
}

/// 关键词高亮配置
#[derive(Debug, Clone)]
pub struct HighlightConfig {
    /// 是否启用高亮
    pub enabled: bool,
    /// 关键词列表
    pub keywords: Vec<String>,
    /// 高亮颜色映射
    pub color_map: HashMap<String, String>,
}

/// 过滤器配置
#[derive(Debug, Clone)]
pub struct FilterConfig {
    /// 过滤器类型
    pub filter_type: FilterType,
    /// 过滤规则
    pub rule: String,
    /// 是否启用
    pub enabled: bool,
}

/// 过滤器类型
#[derive(Debug, Clone)]
pub enum FilterType {
    /// 正则表达式过滤
    Regex,
    /// 关键词过滤
    Keyword,
    /// 级别过滤
    Level,
    /// 模块过滤
    Module,
}

impl Default for LogConfig {
    fn default() -> Self {
        Self {
            level: "info".to_string(),
            file_path: None,
            console_output: true,
            verbose: false,
            format: LogFormat::default(),
            rotation: None,
            highlight: HighlightConfig::default(),
            filters: Vec::new(),
            custom_fields: HashMap::new(),
        }
    }
}

impl Default for LogFormat {
    fn default() -> Self {
        Self {
            time_format: "%Y-%m-%d %H:%M:%S".to_string(),
            show_file: true,
            show_line: true,
            show_module: false,
            template: None,
        }
    }
}

impl Default for HighlightConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            keywords: Vec::new(),
            color_map: HashMap::new(),
        }
    }
}

/// 配置构建器
pub struct ConfigBuilder {
    config: LogConfig,
}

impl ConfigBuilder {
    /// 创建新的配置构建器
    pub fn new() -> Self {
        Self {
            config: LogConfig::default(),
        }
    }
    
    /// 设置日志级别
    pub fn level<S: Into<String>>(mut self, level: S) -> Self {
        self.config.level = level.into();
        self
    }
    
    /// 设置输出文件
    pub fn file<P: AsRef<Path>>(mut self, path: P) -> Self {
        self.config.file_path = Some(path.as_ref().to_string_lossy().to_string());
        self
    }
    
    /// 启用/禁用控制台输出
    pub fn console(mut self, enabled: bool) -> Self {
        self.config.console_output = enabled;
        self
    }
    
    /// 启用/禁用详细模式
    pub fn verbose(mut self, enabled: bool) -> Self {
        self.config.verbose = enabled;
        self
    }
    
    /// 设置时间格式
    pub fn time_format<S: Into<String>>(mut self, format: S) -> Self {
        self.config.format.time_format = format.into();
        self
    }
    
    /// 启用文件轮转
    pub fn rotation(mut self, max_size: u64, max_files: u32, compress: bool) -> Self {
        self.config.rotation = Some(RotationConfig {
            max_size,
            max_files,
            compress,
        });
        self
    }
    
    /// 添加关键词高亮
    pub fn highlight_keywords(mut self, keywords: Vec<String>) -> Self {
        self.config.highlight.enabled = !keywords.is_empty();
        self.config.highlight.keywords = keywords;
        self
    }
    
    /// 添加过滤器
    pub fn add_filter(mut self, filter_type: FilterType, rule: String) -> Self {
        self.config.filters.push(FilterConfig {
            filter_type,
            rule,
            enabled: true,
        });
        self
    }
    
    /// 添加自定义字段
    pub fn custom_field<K: Into<String>, V: Into<String>>(mut self, key: K, value: V) -> Self {
        self.config.custom_fields.insert(key.into(), value.into());
        self
    }
    
    /// 构建配置
    pub fn build(self) -> LogConfig {
        self.config
    }
}

/// 配置加载器
pub struct ConfigLoader;

impl ConfigLoader {
    /// 从文件加载配置(简化版本)
    pub fn from_file<P: AsRef<Path>>(_path: P) -> LogResult<LogConfig> {
        // 返回默认配置
        Ok(LogConfig::default())
    }
    
    /// 从环境变量加载配置(简化版本)
    pub fn from_env() -> LogResult<LogConfig> {
        // 返回默认配置
        Ok(LogConfig::default())
    }
    
    /// 保存配置到文件(简化版本)
    pub fn save_to_file<P: AsRef<Path>>(_config: &LogConfig, _path: P) -> LogResult<()> {
        // 简化实现,不执行实际保存
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_config_builder() {
        let config = ConfigBuilder::new()
            .level("debug")
            .file("/tmp/test.log")
            .console(true)
            .verbose(true)
            .highlight_keywords(vec!["ERROR".to_string(), "WARN".to_string()])
            .build();
            
        assert_eq!(config.level, "debug");
        assert_eq!(config.file_path, Some("/tmp/test.log".to_string()));
        assert!(config.console_output);
        assert!(config.verbose);
        assert!(config.highlight.enabled);
        assert_eq!(config.highlight.keywords.len(), 2);
    }
}