82 lines
3.2 KiB
Rust
82 lines
3.2 KiB
Rust
//! # IdleFox Configuration Module
|
|
//!
|
|
//! This module provides a centralized configuration system for IdleFox, allowing you to load and manage your settings in an efficient and flexible manner.
|
|
//!
|
|
//! The primary goal of this module is to provide a robust and reliable way to store and retrieve configuration data from the `.idlefox.toml` file.
|
|
//! If the file does not exist or cannot be loaded, it will automatically create a default version for you.
|
|
//!
|
|
//! ### Key Features
|
|
//!
|
|
//! * **Config Loading**: Seamlessly load your existing config from the `.idlefox.toml` file, ensuring that all settings are preserved and up-to-date.
|
|
//! * **Default Config Creation**: Automatically generate a default configuration if the specified file cannot be loaded or is missing.
|
|
//! * **Legacy Data Import**: Safely import old data from outdated configurations to minimize disruptions during upgrades.
|
|
//! * **Faulty Data Recovery**: Implement measures to recover from importing faulty data, ensuring that your IdleFox instance remains stable and functional.
|
|
//!
|
|
//! ### Usage
|
|
//!
|
|
//! To use this module effectively, you can follow these steps:
|
|
//!
|
|
//! 1. Use the `init_logger()` function from the IdleFox logging module (as shown below) to initialize the logger with your custom log level and output destination.
|
|
//!
|
|
//! Example usage:
|
|
//!
|
|
//! ```rust
|
|
//! # use idlefox::config;
|
|
//! # fn main() {
|
|
//! // Initialize the logger with a custom log level of info
|
|
//! config::init_logger().expect("Failed to initialize logger");
|
|
//! // Log some messages at different levels (e.g., debug, warn, error)
|
|
//! let message = "This is an example message";
|
|
//! log::warn!("{}", message);
|
|
//! log::error!("{}", message);
|
|
//! # }
|
|
//!
|
|
|
|
use std::fs::File;
|
|
|
|
use log::LevelFilter;
|
|
use serde::{Deserialize, Serialize};
|
|
use simplelog::{ColorChoice, CombinedLogger, Config, TermLogger, TerminalMode, WriteLogger};
|
|
|
|
use crate::error_handling::IdleError;
|
|
|
|
/// The global config
|
|
/// TODO: Update this doc as the config module is done
|
|
#[derive(Serialize, Deserialize)]
|
|
pub(crate) struct GlobalConfig {}
|
|
|
|
/// Initializes a logger for logging purposes.
|
|
///
|
|
/// This function sets up two loggers: one for terminal output (with warning level) and another for file-based logging (with info level).
|
|
///
|
|
/// The terminal logger will display warnings, while the file logger will write all logs at or above the info level to an "idlefox.log" file in the current working directory.
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```
|
|
/// idlefox::config::init_logger().expect("Failed to initialize logger");
|
|
/// // Log a message at the info level.
|
|
/// log::info!("This will be written to idlefox.log");
|
|
///
|
|
/// // Log a warning at the warn level, which should appear on screen as well.
|
|
/// log::warn!("Warning: This will be written on file and on screen");
|
|
/// ```
|
|
///
|
|
pub fn init_logger() -> Result<(), IdleError> {
|
|
CombinedLogger::init(vec![
|
|
TermLogger::new(
|
|
LevelFilter::Warn,
|
|
Config::default(),
|
|
TerminalMode::Mixed,
|
|
ColorChoice::Auto,
|
|
),
|
|
WriteLogger::new(
|
|
LevelFilter::Info,
|
|
Config::default(),
|
|
// TODO: Maybe add la date du jour de lancement ?
|
|
File::create("idlefox.log").unwrap(),
|
|
),
|
|
])
|
|
.map_err(IdleError::LoggerInitFailure)
|
|
}
|