mirror of
https://github.com/youwen5/iamb.git
synced 2025-06-20 05:39:52 -07:00
Allow log level to be configured (#58)
This commit is contained in:
parent
db35581d07
commit
14fe916d94
3 changed files with 49 additions and 2 deletions
|
@ -11,6 +11,7 @@ use std::process;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use matrix_sdk::ruma::{OwnedUserId, UserId};
|
use matrix_sdk::ruma::{OwnedUserId, UserId};
|
||||||
use serde::{de::Error as SerdeError, de::Visitor, Deserialize, Deserializer};
|
use serde::{de::Error as SerdeError, de::Visitor, Deserialize, Deserializer};
|
||||||
|
use tracing::Level;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use modalkit::tui::{
|
use modalkit::tui::{
|
||||||
|
@ -108,6 +109,47 @@ pub enum ConfigError {
|
||||||
Invalid(#[from] serde_json::Error),
|
Invalid(#[from] serde_json::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
|
pub struct LogLevel(pub Level);
|
||||||
|
pub struct LogLevelVisitor;
|
||||||
|
|
||||||
|
impl From<LogLevel> for Level {
|
||||||
|
fn from(level: LogLevel) -> Level {
|
||||||
|
level.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Visitor<'de> for LogLevelVisitor {
|
||||||
|
type Value = LogLevel;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
formatter.write_str("a valid log level (e.g. \"warn\" or \"debug\")")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: SerdeError,
|
||||||
|
{
|
||||||
|
match value {
|
||||||
|
"info" => Ok(LogLevel(Level::INFO)),
|
||||||
|
"debug" => Ok(LogLevel(Level::DEBUG)),
|
||||||
|
"warn" => Ok(LogLevel(Level::WARN)),
|
||||||
|
"error" => Ok(LogLevel(Level::ERROR)),
|
||||||
|
"trace" => Ok(LogLevel(Level::TRACE)),
|
||||||
|
_ => Err(E::custom("Could not parse log level")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for LogLevel {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
deserializer.deserialize_str(LogLevelVisitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub struct UserColor(pub Color);
|
pub struct UserColor(pub Color);
|
||||||
pub struct UserColorVisitor;
|
pub struct UserColorVisitor;
|
||||||
|
@ -180,6 +222,7 @@ fn merge_users(a: Option<UserOverrides>, b: Option<UserOverrides>) -> Option<Use
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct TunableValues {
|
pub struct TunableValues {
|
||||||
|
pub log_level: Level,
|
||||||
pub reaction_display: bool,
|
pub reaction_display: bool,
|
||||||
pub reaction_shortcode_display: bool,
|
pub reaction_shortcode_display: bool,
|
||||||
pub read_receipt_send: bool,
|
pub read_receipt_send: bool,
|
||||||
|
@ -193,6 +236,7 @@ pub struct TunableValues {
|
||||||
|
|
||||||
#[derive(Clone, Default, Deserialize)]
|
#[derive(Clone, Default, Deserialize)]
|
||||||
pub struct Tunables {
|
pub struct Tunables {
|
||||||
|
pub log_level: Option<LogLevel>,
|
||||||
pub reaction_display: Option<bool>,
|
pub reaction_display: Option<bool>,
|
||||||
pub reaction_shortcode_display: Option<bool>,
|
pub reaction_shortcode_display: Option<bool>,
|
||||||
pub read_receipt_send: Option<bool>,
|
pub read_receipt_send: Option<bool>,
|
||||||
|
@ -207,6 +251,7 @@ pub struct Tunables {
|
||||||
impl Tunables {
|
impl Tunables {
|
||||||
fn merge(self, other: Self) -> Self {
|
fn merge(self, other: Self) -> Self {
|
||||||
Tunables {
|
Tunables {
|
||||||
|
log_level: self.log_level.or(other.log_level),
|
||||||
reaction_display: self.reaction_display.or(other.reaction_display),
|
reaction_display: self.reaction_display.or(other.reaction_display),
|
||||||
reaction_shortcode_display: self
|
reaction_shortcode_display: self
|
||||||
.reaction_shortcode_display
|
.reaction_shortcode_display
|
||||||
|
@ -223,6 +268,7 @@ impl Tunables {
|
||||||
|
|
||||||
fn values(self) -> TunableValues {
|
fn values(self) -> TunableValues {
|
||||||
TunableValues {
|
TunableValues {
|
||||||
|
log_level: self.log_level.map(Level::from).unwrap_or(Level::INFO),
|
||||||
reaction_display: self.reaction_display.unwrap_or(true),
|
reaction_display: self.reaction_display.unwrap_or(true),
|
||||||
reaction_shortcode_display: self.reaction_shortcode_display.unwrap_or(false),
|
reaction_shortcode_display: self.reaction_shortcode_display.unwrap_or(false),
|
||||||
read_receipt_send: self.read_receipt_send.unwrap_or(true),
|
read_receipt_send: self.read_receipt_send.unwrap_or(true),
|
||||||
|
|
|
@ -15,7 +15,6 @@ use std::time::Duration;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use tokio::sync::Mutex as AsyncMutex;
|
use tokio::sync::Mutex as AsyncMutex;
|
||||||
use tracing::{self, Level};
|
|
||||||
use tracing_subscriber::FmtSubscriber;
|
use tracing_subscriber::FmtSubscriber;
|
||||||
|
|
||||||
use matrix_sdk::ruma::OwnedUserId;
|
use matrix_sdk::ruma::OwnedUserId;
|
||||||
|
@ -534,7 +533,7 @@ fn main() -> IambResult<()> {
|
||||||
|
|
||||||
let subscriber = FmtSubscriber::builder()
|
let subscriber = FmtSubscriber::builder()
|
||||||
.with_writer(appender)
|
.with_writer(appender)
|
||||||
.with_max_level(Level::INFO)
|
.with_max_level(settings.tunables.log_level)
|
||||||
.finish();
|
.finish();
|
||||||
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
|
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ use matrix_sdk::ruma::{
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use modalkit::tui::style::{Color, Style};
|
use modalkit::tui::style::{Color, Style};
|
||||||
use tokio::sync::mpsc::unbounded_channel;
|
use tokio::sync::mpsc::unbounded_channel;
|
||||||
|
use tracing::Level;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -171,6 +172,7 @@ pub fn mock_dirs() -> DirectoryValues {
|
||||||
pub fn mock_tunables() -> TunableValues {
|
pub fn mock_tunables() -> TunableValues {
|
||||||
TunableValues {
|
TunableValues {
|
||||||
default_room: None,
|
default_room: None,
|
||||||
|
log_level: Level::INFO,
|
||||||
reaction_display: true,
|
reaction_display: true,
|
||||||
reaction_shortcode_display: false,
|
reaction_shortcode_display: false,
|
||||||
read_receipt_send: true,
|
read_receipt_send: true,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue