Logging #26

Open
leon wants to merge 33 commits from Logging into main
3 changed files with 53 additions and 69 deletions
Showing only changes of commit cbfa4cbfdf - Show all commits

View File

@ -5,15 +5,13 @@
use getset::Getters;
/**
* A immutable record of all the information needed to connect to the SQL database.
*/
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, PartialEq, Eq, Getters, Debug)]
#[getset(get = "pub")]
pub struct DbConfig
{
pub struct DbConfig {
/// Database connection address.
/// Is an option to allow constructing a default config during compile time.<br>
addr: String,
@ -21,15 +19,13 @@ pub struct DbConfig
port: u16,
}
impl Default for DbConfig
{
fn default() -> Self
{
Self {
addr: String::from("localhost"),
port: 6969
}
}
impl Default for DbConfig {
fn default() -> Self {
Self {
addr: String::from("localhost"),
port: 6969,
}
}
}
/**
@ -38,28 +34,24 @@ impl Default for DbConfig
#[derive(Default, Debug)]
pub struct DbConfigBuilder(DbConfig);
impl DbConfigBuilder
{
/// Get a new [`DbConfigBuilder`]
#[must_use]
pub fn new() -> Self
{
Self::default()
}
impl DbConfigBuilder {
/// Get a new [`DbConfigBuilder`]
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Set the address to the location of the database.
#[must_use]
pub fn set_addr(mut self, addr: impl Into<String>) -> Self
{
self.0.addr = addr.into();
self
}
/// Set the address to the location of the database.
#[must_use]
pub fn set_addr(mut self, addr: impl Into<String>) -> Self {
self.0.addr = addr.into();
self
}
/// Set the port to the port the database uses.
#[must_use]
pub fn set_port(mut self, port: impl Into<u16>) -> Self
{
self.0.port = port.into();
self
}
/// Set the port to the port the database uses.
#[must_use]
pub fn set_port(mut self, port: impl Into<u16>) -> Self {
self.0.port = port.into();
self
}
}

View File

@ -2,8 +2,8 @@
* Containing all singleton and thread-safe structs related to configuring `WANessa`.
*/
pub mod log;
pub mod db;
pub mod log;
use crate::db::DbConfig;
use crate::log::LogSettings;
@ -18,4 +18,5 @@ use once_cell::sync::Lazy;
pub static DB_CONFIG: Lazy<Arc<DbConfig>> = Lazy::new(|| Arc::new(DbConfig::default()));
/// Singelton [`LogSettings`].
pub static LOG_SETTINGS: Lazy<RwLock<LogSettings>> = Lazy::new(|| RwLock::new(LogSettings::default()));
pub static LOG_SETTINGS: Lazy<RwLock<LogSettings>> =
Lazy::new(|| RwLock::new(LogSettings::default()));

View File

@ -12,16 +12,13 @@ use std::path::PathBuf;
use getset::Getters;
use getset::Setters;
/**
* All settings relating to how the project logs information.
*/
#[allow(clippy::struct_excessive_bools)]
#[derive(Clone, PartialEq, Eq, Getters, Setters, Debug)]
#[getset(get = "pub", set = "pub")]
pub struct LogSettings
{
pub struct LogSettings {
/// See [`LogVerbosity`].<br>
verbosity: LogVerbosity,
/// Logs UTC time and date of message, if true.<br>
@ -32,7 +29,7 @@ pub struct LogSettings
/// Logs location in code, where the message was logged, if true.<br>
location: bool,
/// If `Some(path)` tries to also write the log to `path` in addition to stderr/stderr.<br>
#[getset(skip)]
#[getset(skip)]
path: Option<PathBuf>,
/// Logs to standard out, if true.<br>
stdout: bool,
@ -40,36 +37,31 @@ pub struct LogSettings
stderr: bool,
}
impl LogSettings
{
/// Setter for log path, including syntactic sugar for the [Option] enum.
pub fn set_path(&mut self, path: impl Into<Option<PathBuf>>)
{
self.path = path.into();
}
impl LogSettings {
/// Setter for log path, including syntactic sugar for the [Option] enum.
pub fn set_path(&mut self, path: impl Into<Option<PathBuf>>) {
self.path = path.into();
}
/// Getter for the log path.
#[must_use]
pub fn path(&self) -> Option<&Path>
{
self.path.as_deref()
}
/// Getter for the log path.
#[must_use]
pub fn path(&self) -> Option<&Path> {
self.path.as_deref()
}
}
impl Default for LogSettings
{
fn default() -> Self
{
Self {
verbosity: Warning,
time: false,
time_format: String::from("%F-%T:%f"),
location: false,
stdout: true,
stderr: true,
path: None,
}
}
impl Default for LogSettings {
fn default() -> Self {
Self {
verbosity: Warning,
time: false,
time_format: String::from("%F-%T:%f"),
location: false,
stdout: true,
stderr: true,
path: None,
}
}
}
/**
@ -104,4 +96,3 @@ impl Ord for LogVerbosity {
(*self as usize).cmp(&(*other as usize))
}
}