Commit 29ddc963d0
Unregistered key
Layout: unified · split
Cargo.lock +13
| @@ -3174,6 +3174,15 @@ dependencies = [ | |||
| 3174 | "syn 2.0.117", | 3174 | "syn 2.0.117", |
| 3175 | ] | 3175 | ] |
| 3176 | 3176 | ||
| 3177 | [[package]] | ||
| 3178 | name = "matchers" | ||
| 3179 | version = "0.2.0" | ||
| 3180 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 3181 | checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" | ||
| 3182 | dependencies = [ | ||
| 3183 | "regex-automata", | ||
| 3184 | ] | ||
| 3185 | |||
| 3177 | [[package]] | 3186 | [[package]] |
| 3178 | name = "matchit" | 3187 | name = "matchit" |
| 3179 | version = "0.8.4" | 3188 | version = "0.8.4" |
| @@ -6039,10 +6048,14 @@ version = "0.3.23" | |||
| 6039 | source = "registry+https://github.com/rust-lang/crates.io-index" | 6048 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 6040 | checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" | 6049 | checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" |
| 6041 | dependencies = [ | 6050 | dependencies = [ |
| 6051 | "matchers", | ||
| 6042 | "nu-ansi-term", | 6052 | "nu-ansi-term", |
| 6053 | "once_cell", | ||
| 6054 | "regex-automata", | ||
| 6043 | "sharded-slab", | 6055 | "sharded-slab", |
| 6044 | "smallvec", | 6056 | "smallvec", |
| 6045 | "thread_local", | 6057 | "thread_local", |
| 6058 | "tracing", | ||
| 6046 | "tracing-core", | 6059 | "tracing-core", |
| 6047 | "tracing-log", | 6060 | "tracing-log", |
| 6048 | ] | 6061 | ] |
Cargo.toml +1 −1
| @@ -62,7 +62,7 @@ thiserror = "2.0" | |||
| 62 | toml = "1.1" | 62 | toml = "1.1" |
| 63 | tracing = "0.1.36" | 63 | tracing = "0.1.36" |
| 64 | tracing-appender = "0.2.2" | 64 | tracing-appender = "0.2.2" |
| 65 | tracing-subscriber = "0.3.16" | 65 | tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } |
| 66 | unicode-segmentation = "1.7" | 66 | unicode-segmentation = "1.7" |
| 67 | unicode-width = "0.2" | 67 | unicode-width = "0.2" |
| 68 | url = {version = "2.2.2", features = ["serde"]} | 68 | url = {version = "2.2.2", features = ["serde"]} |
docs/iamb.1 +12
| @@ -295,6 +295,18 @@ Produces no effect in | |||
| 295 | but will display aliens from Space Invaders in Matrix clients that support doing so. | 295 | but will display aliens from Space Invaders in Matrix clients that support doing so. |
| 296 | .El | 296 | .El |
| 297 | 297 | ||
| 298 | .Sh "ENVIRONMENT" | ||
| 299 | The behaviour of | ||
| 300 | .Nm | ||
| 301 | is affected by the following environment variables. | ||
| 302 | .Bl -tag -width Ds | ||
| 303 | .It Sy "RUST_LOG" | ||
| 304 | Overwrites the value set by the | ||
| 305 | .Sy log_level | ||
| 306 | config value. | ||
| 307 | The syntax is defined at | ||
| 308 | .Lk https://docs.rs/tracing-subscriber/0.3.16/tracing_subscriber/filter/struct.EnvFilter.html | ||
| 309 | |||
| 298 | .Sh EXAMPLES | 310 | .Sh EXAMPLES |
| 299 | .Ss Example 1: Starting with a specific profile | 311 | .Ss Example 1: Starting with a specific profile |
| 300 | To start with a profile named | 312 | To start with a profile named |
docs/iamb.5 +12 −1
| @@ -163,8 +163,19 @@ Possible values are: | |||
| 163 | .Dq Sy trace , | 163 | .Dq Sy trace , |
| 164 | .Dq Sy debug , | 164 | .Dq Sy debug , |
| 165 | .Dq Sy info , | 165 | .Dq Sy info , |
| 166 | .Dq Sy warn , and | 166 | .Dq Sy warn |
| 167 | (the default), and | ||
| 167 | .Dq Sy error . | 168 | .Dq Sy error . |
| 169 | Also supports the syntax defined by | ||
| 170 | .Lk https://docs.rs/tracing_subscriber/0.3.16/tracing_subscriber/filter/struct.EnvFilter.html | ||
| 171 | |||
| 172 | .It Sy max_log_files | ||
| 173 | Specifies how many daily log files should be saved. | ||
| 174 | Set this to | ||
| 175 | .Sy 0 | ||
| 176 | to disable automatically deleting log files. | ||
| 177 | Defaults to | ||
| 178 | .Sy 7 . | ||
| 168 | 179 | ||
| 169 | .It Sy message_shortcode_display | 180 | .It Sy message_shortcode_display |
| 170 | Defines whether or not Emoji characters in messages should be replaced by their | 181 | Defines whether or not Emoji characters in messages should be replaced by their |
src/config.rs +7 −45
| @@ -17,7 +17,6 @@ use ratatui::style::{Color, Modifier as StyleModifier, Style}; | |||
| 17 | use ratatui::text::Span; | 17 | use ratatui::text::Span; |
| 18 | use ratatui_image::picker::ProtocolType; | 18 | use ratatui_image::picker::ProtocolType; |
| 19 | use serde::{de::Error as SerdeError, de::Visitor, Deserialize, Deserializer, Serialize}; | 19 | use serde::{de::Error as SerdeError, de::Visitor, Deserialize, Deserializer, Serialize}; |
| 20 | use tracing::Level; | ||
| 21 | use url::Url; | 20 | use url::Url; |
| 22 | 21 | ||
| 23 | use modalkit::{env::vim::VimMode, key::TerminalKey, keybindings::InputKey}; | 22 | use modalkit::{env::vim::VimMode, key::TerminalKey, keybindings::InputKey}; |
| @@ -224,47 +223,6 @@ impl<'de> Deserialize<'de> for VimModes { | |||
| 224 | } | 223 | } |
| 225 | } | 224 | } |
| 226 | 225 | ||
| 227 | #[derive(Clone, Debug, Eq, PartialEq)] | ||
| 228 | pub struct LogLevel(pub Level); | ||
| 229 | pub struct LogLevelVisitor; | ||
| 230 | |||
| 231 | impl From<LogLevel> for Level { | ||
| 232 | fn from(level: LogLevel) -> Level { | ||
| 233 | level.0 | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | impl Visitor<'_> for LogLevelVisitor { | ||
| 238 | type Value = LogLevel; | ||
| 239 | |||
| 240 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
| 241 | formatter.write_str("a valid log level (e.g. \"warn\" or \"debug\")") | ||
| 242 | } | ||
| 243 | |||
| 244 | fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> | ||
| 245 | where | ||
| 246 | E: SerdeError, | ||
| 247 | { | ||
| 248 | match value { | ||
| 249 | "info" => Ok(LogLevel(Level::INFO)), | ||
| 250 | "debug" => Ok(LogLevel(Level::DEBUG)), | ||
| 251 | "warn" => Ok(LogLevel(Level::WARN)), | ||
| 252 | "error" => Ok(LogLevel(Level::ERROR)), | ||
| 253 | "trace" => Ok(LogLevel(Level::TRACE)), | ||
| 254 | _ => Err(E::custom("Could not parse log level")), | ||
| 255 | } | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | impl<'de> Deserialize<'de> for LogLevel { | ||
| 260 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| 261 | where | ||
| 262 | D: Deserializer<'de>, | ||
| 263 | { | ||
| 264 | deserializer.deserialize_str(LogLevelVisitor) | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | #[derive(Clone, Debug, Eq, PartialEq)] | 226 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 269 | pub struct UserColor(pub Color); | 227 | pub struct UserColor(pub Color); |
| 270 | pub struct UserColorVisitor; | 228 | pub struct UserColorVisitor; |
| @@ -558,7 +516,8 @@ impl SortOverrides { | |||
| 558 | 516 | ||
| 559 | #[derive(Clone)] | 517 | #[derive(Clone)] |
| 560 | pub struct TunableValues { | 518 | pub struct TunableValues { |
| 561 | pub log_level: Level, | 519 | pub log_level: String, |
| 520 | pub max_log_files: usize, | ||
| 562 | pub message_shortcode_display: bool, | 521 | pub message_shortcode_display: bool, |
| 563 | pub normal_after_send: bool, | 522 | pub normal_after_send: bool, |
| 564 | pub reaction_display: bool, | 523 | pub reaction_display: bool, |
| @@ -585,7 +544,8 @@ pub struct TunableValues { | |||
| 585 | 544 | ||
| 586 | #[derive(Clone, Default, Deserialize)] | 545 | #[derive(Clone, Default, Deserialize)] |
| 587 | pub struct Tunables { | 546 | pub struct Tunables { |
| 588 | pub log_level: Option<LogLevel>, | 547 | pub log_level: Option<String>, |
| 548 | pub max_log_files: Option<usize>, | ||
| 589 | pub message_shortcode_display: Option<bool>, | 549 | pub message_shortcode_display: Option<bool>, |
| 590 | pub normal_after_send: Option<bool>, | 550 | pub normal_after_send: Option<bool>, |
| 591 | pub reaction_display: Option<bool>, | 551 | pub reaction_display: Option<bool>, |
| @@ -615,6 +575,7 @@ impl Tunables { | |||
| 615 | fn merge(self, other: Self) -> Self { | 575 | fn merge(self, other: Self) -> Self { |
| 616 | Tunables { | 576 | Tunables { |
| 617 | log_level: self.log_level.or(other.log_level), | 577 | log_level: self.log_level.or(other.log_level), |
| 578 | max_log_files: self.max_log_files.or(other.max_log_files), | ||
| 618 | message_shortcode_display: self | 579 | message_shortcode_display: self |
| 619 | .message_shortcode_display | 580 | .message_shortcode_display |
| 620 | .or(other.message_shortcode_display), | 581 | .or(other.message_shortcode_display), |
| @@ -648,7 +609,8 @@ impl Tunables { | |||
| 648 | 609 | ||
| 649 | fn values(self) -> TunableValues { | 610 | fn values(self) -> TunableValues { |
| 650 | TunableValues { | 611 | TunableValues { |
| 651 | log_level: self.log_level.map(Level::from).unwrap_or(Level::INFO), | 612 | log_level: self.log_level.unwrap_or_else(|| "warn".to_string()), |
| 613 | max_log_files: self.max_log_files.unwrap_or(7), | ||
| 652 | message_shortcode_display: self.message_shortcode_display.unwrap_or(false), | 614 | message_shortcode_display: self.message_shortcode_display.unwrap_or(false), |
| 653 | normal_after_send: self.normal_after_send.unwrap_or(false), | 615 | normal_after_send: self.normal_after_send.unwrap_or(false), |
| 654 | reaction_display: self.reaction_display.unwrap_or(true), | 616 | reaction_display: self.reaction_display.unwrap_or(true), |
src/main.rs +41 −13
| @@ -36,7 +36,8 @@ use rand::distr::Alphanumeric; | |||
| 36 | use rand::RngExt as _; | 36 | use rand::RngExt as _; |
| 37 | use temp_dir::TempDir; | 37 | use temp_dir::TempDir; |
| 38 | use tokio::sync::Mutex as AsyncMutex; | 38 | use tokio::sync::Mutex as AsyncMutex; |
| 39 | use tracing_subscriber::FmtSubscriber; | 39 | use tracing::Level; |
| 40 | use tracing_subscriber::{EnvFilter, FmtSubscriber}; | ||
| 40 | 41 | ||
| 41 | use modalkit::crossterm::{ | 42 | use modalkit::crossterm::{ |
| 42 | self, | 43 | self, |
| @@ -1079,6 +1080,44 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> { | |||
| 1079 | Ok(()) | 1080 | Ok(()) |
| 1080 | } | 1081 | } |
| 1081 | 1082 | ||
| 1083 | fn setup_logging(settings: &ApplicationSettings) -> tracing_appender::non_blocking::WorkerGuard { | ||
| 1084 | let log_prefix = format!("iamb-log-{}", settings.profile_name); | ||
| 1085 | let log_dir = settings.dirs.logs.as_path(); | ||
| 1086 | let max_log_files = settings.tunables.max_log_files; | ||
| 1087 | let log_level = &settings.tunables.log_level; | ||
| 1088 | |||
| 1089 | let appender = tracing_appender::rolling::Builder::new() | ||
| 1090 | .rotation(tracing_appender::rolling::Rotation::DAILY) | ||
| 1091 | .filename_prefix(log_prefix) | ||
| 1092 | .max_log_files(max_log_files) | ||
| 1093 | .build(log_dir) | ||
| 1094 | .expect("can build appending tracing logger"); | ||
| 1095 | let (appender, guard) = tracing_appender::non_blocking(appender); | ||
| 1096 | |||
| 1097 | let filter = if let Ok(dirs) = std::env::var(EnvFilter::DEFAULT_ENV) { | ||
| 1098 | EnvFilter::builder() | ||
| 1099 | .with_default_directive(Level::WARN.into()) | ||
| 1100 | .parse(dirs) | ||
| 1101 | .map_err(|err| format!("Unable to parse {}: {err}", EnvFilter::DEFAULT_ENV)) | ||
| 1102 | .unwrap_or_else(print_exit) | ||
| 1103 | } else { | ||
| 1104 | EnvFilter::builder() | ||
| 1105 | .with_default_directive(Level::WARN.into()) | ||
| 1106 | .parse(log_level) | ||
| 1107 | .map_err(|err| format!("Unable to parse `log_level`: {err}")) | ||
| 1108 | .unwrap_or_else(print_exit) | ||
| 1109 | }; | ||
| 1110 | |||
| 1111 | let subscriber = FmtSubscriber::builder() | ||
| 1112 | .with_writer(appender) | ||
| 1113 | .with_env_filter(filter) | ||
| 1114 | .finish(); | ||
| 1115 | |||
| 1116 | tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); | ||
| 1117 | |||
| 1118 | guard | ||
| 1119 | } | ||
| 1120 | |||
| 1082 | fn main() -> IambResult<()> { | 1121 | fn main() -> IambResult<()> { |
| 1083 | // Parse command-line flags. | 1122 | // Parse command-line flags. |
| 1084 | let iamb = Iamb::parse(); | 1123 | let iamb = Iamb::parse(); |
| @@ -1092,18 +1131,7 @@ fn main() -> IambResult<()> { | |||
| 1092 | libc::umask(0o077); | 1131 | libc::umask(0o077); |
| 1093 | }; | 1132 | }; |
| 1094 | 1133 | ||
| 1095 | // Set up the tracing subscriber so we can log client messages. | 1134 | let guard = setup_logging(&settings); |
| 1096 | let log_prefix = format!("iamb-log-{}", settings.profile_name); | ||
| 1097 | let log_dir = settings.dirs.logs.as_path(); | ||
| 1098 | |||
| 1099 | let appender = tracing_appender::rolling::daily(log_dir, log_prefix); | ||
| 1100 | let (appender, guard) = tracing_appender::non_blocking(appender); | ||
| 1101 | |||
| 1102 | let subscriber = FmtSubscriber::builder() | ||
| 1103 | .with_writer(appender) | ||
| 1104 | .with_max_level(settings.tunables.log_level) | ||
| 1105 | .finish(); | ||
| 1106 | tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); | ||
| 1107 | 1135 | ||
| 1108 | let rt = tokio::runtime::Builder::new_multi_thread() | 1136 | let rt = tokio::runtime::Builder::new_multi_thread() |
| 1109 | .enable_all() | 1137 | .enable_all() |
src/tests.rs +2 −2
| @@ -18,7 +18,6 @@ use lazy_static::lazy_static; | |||
| 18 | use ratatui::style::{Color, Style}; | 18 | use ratatui::style::{Color, Style}; |
| 19 | use serde_json::{Map, Value}; | 19 | use serde_json::{Map, Value}; |
| 20 | use tokio::sync::mpsc::unbounded_channel; | 20 | use tokio::sync::mpsc::unbounded_channel; |
| 21 | use tracing::Level; | ||
| 22 | use url::Url; | 21 | use url::Url; |
| 23 | 22 | ||
| 24 | use crate::{ | 23 | use crate::{ |
| @@ -172,7 +171,8 @@ pub fn mock_dirs() -> DirectoryValues { | |||
| 172 | pub fn mock_tunables() -> TunableValues { | 171 | pub fn mock_tunables() -> TunableValues { |
| 173 | TunableValues { | 172 | TunableValues { |
| 174 | default_room: None, | 173 | default_room: None, |
| 175 | log_level: Level::INFO, | 174 | log_level: "warn".into(), |
| 175 | max_log_files: 7, | ||
| 176 | message_shortcode_display: false, | 176 | message_shortcode_display: false, |
| 177 | normal_after_send: true, | 177 | normal_after_send: true, |
| 178 | reaction_display: true, | 178 | reaction_display: true, |