Commit 14fe916d94

14fe916d949001255b8ae7545e4ef365fddf63fe

parent: db35581d07

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-03-13 23:10 UTC

Allow log level to be configured (#58)

Layout: unified · split

src/config.rs +46
@@ -11,6 +11,7 @@ use std::process;
11use clap::Parser; 11use clap::Parser;
12use matrix_sdk::ruma::{OwnedUserId, UserId}; 12use matrix_sdk::ruma::{OwnedUserId, UserId};
13use serde::{de::Error as SerdeError, de::Visitor, Deserialize, Deserializer}; 13use serde::{de::Error as SerdeError, de::Visitor, Deserialize, Deserializer};
14use tracing::Level;
14use url::Url; 15use url::Url;
15 16
16use modalkit::tui::{ 17use modalkit::tui::{
@@ -108,6 +109,47 @@ pub enum ConfigError {
108 Invalid(#[from] serde_json::Error), 109 Invalid(#[from] serde_json::Error),
109} 110}
110 111
112#[derive(Clone, Debug, Eq, PartialEq)]
113pub struct LogLevel(pub Level);
114pub struct LogLevelVisitor;
115
116impl From<LogLevel> for Level {
117 fn from(level: LogLevel) -> Level {
118 level.0
119 }
120}
121
122impl<'de> Visitor<'de> for LogLevelVisitor {
123 type Value = LogLevel;
124
125 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
126 formatter.write_str("a valid log level (e.g. \"warn\" or \"debug\")")
127 }
128
129 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
130 where
131 E: SerdeError,
132 {
133 match value {
134 "info" => Ok(LogLevel(Level::INFO)),
135 "debug" => Ok(LogLevel(Level::DEBUG)),
136 "warn" => Ok(LogLevel(Level::WARN)),
137 "error" => Ok(LogLevel(Level::ERROR)),
138 "trace" => Ok(LogLevel(Level::TRACE)),
139 _ => Err(E::custom("Could not parse log level")),
140 }
141 }
142}
143
144impl<'de> Deserialize<'de> for LogLevel {
145 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
146 where
147 D: Deserializer<'de>,
148 {
149 deserializer.deserialize_str(LogLevelVisitor)
150 }
151}
152
111#[derive(Clone, Debug, Eq, PartialEq)] 153#[derive(Clone, Debug, Eq, PartialEq)]
112pub struct UserColor(pub Color); 154pub struct UserColor(pub Color);
113pub struct UserColorVisitor; 155pub struct UserColorVisitor;
@@ -180,6 +222,7 @@ fn merge_users(a: Option<UserOverrides>, b: Option<UserOverrides>) -> Option<Use
180 222
181#[derive(Clone)] 223#[derive(Clone)]
182pub struct TunableValues { 224pub struct TunableValues {
225 pub log_level: Level,
183 pub reaction_display: bool, 226 pub reaction_display: bool,
184 pub reaction_shortcode_display: bool, 227 pub reaction_shortcode_display: bool,
185 pub read_receipt_send: bool, 228 pub read_receipt_send: bool,
@@ -193,6 +236,7 @@ pub struct TunableValues {
193 236
194#[derive(Clone, Default, Deserialize)] 237#[derive(Clone, Default, Deserialize)]
195pub struct Tunables { 238pub struct Tunables {
239 pub log_level: Option<LogLevel>,
196 pub reaction_display: Option<bool>, 240 pub reaction_display: Option<bool>,
197 pub reaction_shortcode_display: Option<bool>, 241 pub reaction_shortcode_display: Option<bool>,
198 pub read_receipt_send: Option<bool>, 242 pub read_receipt_send: Option<bool>,
@@ -207,6 +251,7 @@ pub struct Tunables {
207impl Tunables { 251impl Tunables {
208 fn merge(self, other: Self) -> Self { 252 fn merge(self, other: Self) -> Self {
209 Tunables { 253 Tunables {
254 log_level: self.log_level.or(other.log_level),
210 reaction_display: self.reaction_display.or(other.reaction_display), 255 reaction_display: self.reaction_display.or(other.reaction_display),
211 reaction_shortcode_display: self 256 reaction_shortcode_display: self
212 .reaction_shortcode_display 257 .reaction_shortcode_display
@@ -223,6 +268,7 @@ impl Tunables {
223 268
224 fn values(self) -> TunableValues { 269 fn values(self) -> TunableValues {
225 TunableValues { 270 TunableValues {
271 log_level: self.log_level.map(Level::from).unwrap_or(Level::INFO),
226 reaction_display: self.reaction_display.unwrap_or(true), 272 reaction_display: self.reaction_display.unwrap_or(true),
227 reaction_shortcode_display: self.reaction_shortcode_display.unwrap_or(false), 273 reaction_shortcode_display: self.reaction_shortcode_display.unwrap_or(false),
228 read_receipt_send: self.read_receipt_send.unwrap_or(true), 274 read_receipt_send: self.read_receipt_send.unwrap_or(true),
src/main.rs +1 −2
@@ -15,7 +15,6 @@ use std::time::Duration;
15 15
16use clap::Parser; 16use clap::Parser;
17use tokio::sync::Mutex as AsyncMutex; 17use tokio::sync::Mutex as AsyncMutex;
18use tracing::{self, Level};
19use tracing_subscriber::FmtSubscriber; 18use tracing_subscriber::FmtSubscriber;
20 19
21use matrix_sdk::ruma::OwnedUserId; 20use matrix_sdk::ruma::OwnedUserId;
@@ -534,7 +533,7 @@ fn main() -> IambResult<()> {
534 533
535 let subscriber = FmtSubscriber::builder() 534 let subscriber = FmtSubscriber::builder()
536 .with_writer(appender) 535 .with_writer(appender)
537 .with_max_level(Level::INFO) 536 .with_max_level(settings.tunables.log_level)
538 .finish(); 537 .finish();
539 tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); 538 tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
540 539
src/tests.rs +2
@@ -17,6 +17,7 @@ use matrix_sdk::ruma::{
17use lazy_static::lazy_static; 17use lazy_static::lazy_static;
18use modalkit::tui::style::{Color, Style}; 18use modalkit::tui::style::{Color, Style};
19use tokio::sync::mpsc::unbounded_channel; 19use tokio::sync::mpsc::unbounded_channel;
20use tracing::Level;
20use url::Url; 21use url::Url;
21 22
22use crate::{ 23use crate::{
@@ -171,6 +172,7 @@ pub fn mock_dirs() -> DirectoryValues {
171pub fn mock_tunables() -> TunableValues { 172pub fn mock_tunables() -> TunableValues {
172 TunableValues { 173 TunableValues {
173 default_room: None, 174 default_room: None,
175 log_level: Level::INFO,
174 reaction_display: true, 176 reaction_display: true,
175 reaction_shortcode_display: false, 177 reaction_shortcode_display: false,
176 read_receipt_send: true, 178 read_receipt_send: true,