Skip to main content

dm_database_sqllog2db/config/
root.rs

1use super::error_log::ErrorLogConfig;
2use super::exporter::ExporterConfig;
3use super::logging::LoggingConfig;
4use super::sqllog::SqllogConfig;
5use crate::error::{ConfigError, Error, Result};
6use crate::pipeline::{FiltersFeature, NormalizeConfig, OutputConfig};
7use crate::stats::config::StatsConfig;
8use serde::Deserialize;
9use std::io;
10use std::path::Path;
11
12#[derive(Debug, Deserialize, Clone, Default)]
13pub struct Config {
14    #[serde(default)]
15    pub sqllog: SqllogConfig,
16    #[serde(default)]
17    pub logging: LoggingConfig,
18    #[serde(default)]
19    pub exporter: ExporterConfig,
20    #[serde(default)]
21    pub replace_parameters: Option<NormalizeConfig>,
22    #[serde(default)]
23    pub filter: Option<FiltersFeature>,
24    #[serde(default)]
25    pub output: Option<OutputConfig>,
26    #[serde(default)]
27    pub stats: StatsConfig,
28    #[serde(default)]
29    pub error: Option<ErrorLogConfig>,
30    /// watch 触发时设为 true,使 `write_error_log` 以追加模式打开文件。run 路径默认 false(覆盖写)。
31    #[serde(skip)]
32    pub append_error_log: bool,
33}
34
35impl Config {
36    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
37        let path = path.as_ref();
38        let content = std::fs::read_to_string(path).map_err(|e| {
39            if e.kind() == io::ErrorKind::NotFound {
40                Error::Config(ConfigError::NotFound(path.to_path_buf()))
41            } else {
42                Error::Io(e)
43            }
44        })?;
45        toml::from_str(&content).map_err(|e| {
46            Error::Config(ConfigError::ParseFailed {
47                path: path.to_path_buf(),
48                reason: e.to_string(),
49            })
50        })
51    }
52}