From 1db0bc6ebe7c7bb810bf76e837144f4194b308e3 Mon Sep 17 00:00:00 2001 From: Endidx <3027225237@qq.com> Date: Tue, 1 Dec 2026 15:46:10 +0800 Subject: [PATCH] fix(log): truncate error and warning examples by character count --- src/cmds/system/log_cmd.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/cmds/system/log_cmd.rs b/src/cmds/system/log_cmd.rs index cd2ca717e0..e4d1d2bb78 100644 --- a/src/cmds/system/log_cmd.rs +++ b/src/cmds/system/log_cmd.rs @@ -170,12 +170,7 @@ fn analyze_logs(content: &str) -> String { .map(|s| s.as_str()) .unwrap_or(normalized); - let truncated = if original.len() > 100 { - let t: String = original.chars().take(97).collect(); - format!("{}...", t) - } else { - original.to_string() - }; + let truncated = truncate(original, 100); if **count > 1 { result.push(format!(" [×{}] {}", count, truncated)); @@ -212,12 +207,7 @@ fn analyze_logs(content: &str) -> String { .map(|s| s.as_str()) .unwrap_or(normalized); - let truncated = if original.len() > 100 { - let t: String = original.chars().take(97).collect(); - format!("{}...", t) - } else { - original.to_string() - }; + let truncated = truncate(original, 100); if **count > 1 { result.push(format!(" [×{}] {}", count, truncated)); @@ -285,6 +275,20 @@ mod tests { assert!(result.ends_with(&expected), "{result}"); } + #[test] + fn severity_examples_truncate_by_character_count() { + for severity in ["ERROR", "WARN"] { + let prefix = format!("{severity} "); + let at_limit = format!("{prefix}{}", "界".repeat(100 - prefix.len())); + let over_limit = format!("{at_limit}界"); + let expected = format!("{prefix}{}...", "界".repeat(97 - prefix.len())); + for (line, shown) in [(&at_limit, &at_limit), (&over_limit, &expected)] { + let result = run_stdin_str(line); + assert!(result.lines().any(|row| row.trim() == shown), "{result}"); + } + } + } + #[test] fn unclassified_lines_in_mixed_logs_exclude_blank_lines() { let result = run_stdin_str("INFO ready\nWARN retry\nERROR timeout\n\n \nworker stopped\n");