Skip to content

Commit 07cb9c2

Browse files
committed
feat: add colors for log levels
1 parent 89ee251 commit 07cb9c2

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/app.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl LogViewerApp {
9393
.resolve(ui.style())
9494
.size
9595
.max(ui.spacing().interact_size.y);
96+
let default_text_color = ui.visuals().text_color();
9697

9798
let mut table_builder = TableBuilder::new(ui)
9899
.striped(true)
@@ -189,7 +190,18 @@ impl LogViewerApp {
189190
if should_emphasize_field {
190191
ui.strong(field_value.display());
191192
} else {
192-
ui.label(field_value.display());
193+
let display_value = field_value.display();
194+
if let Some(coloring_rules) =
195+
self.data_display_options.colored_fields.get(field_name)
196+
{
197+
let color = coloring_rules
198+
.value_color_map
199+
.get(&display_value)
200+
.unwrap_or(&default_text_color);
201+
ui.colored_label(*color, display_value);
202+
} else {
203+
ui.label(display_value);
204+
}
193205
}
194206
});
195207
}

src/app/data_display_options.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::collections::{BTreeMap, BTreeSet};
22

3+
use egui::Color32;
4+
35
#[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq, Eq)]
46
#[serde(default)] // if we add new fields, give them default values when deserializing old state
57
pub struct DataDisplayOptions {
@@ -13,6 +15,9 @@ pub struct DataDisplayOptions {
1315
/// WARNING: This must be a valid index into the list as this is assumed in method implementations
1416
emphasize_if_matching_field_idx: Option<usize>,
1517

18+
/// Fields that should be colored based on their value. Key is field name
19+
pub colored_fields: BTreeMap<String, FieldColoringRules>,
20+
1621
/// When set adds a field with this name and populates it with the row numbers (Skips record if field name already exists)
1722
pub row_idx_field_name: Option<String>,
1823

@@ -23,6 +28,12 @@ pub struct DataDisplayOptions {
2328
pub level_conversion: Option<LevelConversion>,
2429
}
2530

31+
#[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq, Eq)]
32+
pub struct FieldColoringRules {
33+
/// Matches a field value to color
34+
pub value_color_map: BTreeMap<String, Color32>,
35+
}
36+
2637
#[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq, Eq)]
2738
pub enum RowParseErrorHandling {
2839
AbortOnAnyErrors,
@@ -99,6 +110,23 @@ impl Default for DataDisplayOptions {
99110
row_idx_field_name: Some("row#".to_string()),
100111
row_parse_error_handling: Default::default(),
101112
level_conversion: Some(Default::default()),
113+
colored_fields: [(
114+
"level_str".to_string(),
115+
FieldColoringRules {
116+
value_color_map: [
117+
("Trace".to_string(), Color32::from_rgb(150, 100, 200)),
118+
("Debug".to_string(), Color32::from_rgb(80, 140, 205)),
119+
("Info".to_string(), Color32::from_rgb(15, 175, 85)),
120+
("Warn".to_string(), Color32::from_rgb(210, 210, 20)),
121+
("Error".to_string(), Color32::from_rgb(220, 105, 105)),
122+
("Fatal".to_string(), Color32::from_rgb(255, 20, 20)),
123+
]
124+
.into_iter()
125+
.collect(),
126+
},
127+
)]
128+
.into_iter()
129+
.collect(),
102130
}
103131
}
104132
}

0 commit comments

Comments
 (0)