Skip to main content

xet_runtime/logging/
config.rs

1use std::path::{Path, PathBuf};
2use std::time::Duration;
3
4use crate::core::xet_config;
5use crate::utils::TemplatedPathBuf;
6
7#[derive(Clone, Debug, PartialEq)]
8pub enum LoggingMode {
9    Directory(PathBuf),
10    File(PathBuf),
11    Console,
12}
13
14/// The log directory cleanup configuration.  By default, the values
15/// are loaded from environment variables.
16
17#[derive(Clone, Debug, PartialEq)]
18pub struct LogDirConfig {
19    pub min_deletion_age: Duration,
20    pub max_retention_age: Duration,
21    pub size_limit: u64,
22    pub filename_prefix: String,
23}
24
25impl Default for LogDirConfig {
26    fn default() -> Self {
27        // Load the defaults from the environmental config.
28        Self {
29            min_deletion_age: xet_config().log.dir_min_deletion_age,
30            max_retention_age: xet_config().log.dir_max_retention_age,
31            size_limit: xet_config().log.dir_max_size.as_u64(),
32            filename_prefix: xet_config().log.prefix.to_string(),
33        }
34    }
35}
36
37#[derive(Clone, Debug, PartialEq)]
38pub struct LoggingConfig {
39    pub logging_mode: LoggingMode,
40    pub use_json: bool,
41    pub enable_log_dir_cleanup: bool,
42    pub version: String,
43    pub log_dir_config: LogDirConfig,
44}
45
46impl LoggingConfig {
47    /// Set up logging to a directory.  Note that this can be overwritten by environmental
48    pub fn default_to_directory(version: String, log_directory: impl AsRef<Path>) -> LoggingConfig {
49        // Choose the logging mode.
50        let logging_mode = {
51            if let Some(log_dest) = &xet_config().log.dest {
52                if log_dest.as_str().is_empty() {
53                    LoggingMode::Console
54                } else {
55                    let path = TemplatedPathBuf::evaluate(log_dest);
56
57                    if log_dest.ends_with('/')
58                        || (cfg!(windows) && log_dest.ends_with('\\'))
59                        || (path.exists() && path.is_dir())
60                    {
61                        LoggingMode::Directory(path)
62                    } else {
63                        LoggingMode::File(path)
64                    }
65                }
66            } else {
67                LoggingMode::Directory(log_directory.as_ref().to_path_buf())
68            }
69        };
70
71        let use_json = {
72            if let Some(format) = &xet_config().log.format {
73                format.as_str().to_ascii_lowercase().trim() == "json"
74            } else {
75                logging_mode != LoggingMode::Console
76            }
77        };
78
79        let enable_log_dir_cleanup =
80            matches!(logging_mode, LoggingMode::Directory(_)) && !xet_config().log.dir_disable_cleanup;
81
82        Self {
83            logging_mode,
84            use_json,
85            enable_log_dir_cleanup,
86            version,
87            log_dir_config: LogDirConfig::default(),
88        }
89    }
90}