Skip to main content

systemprompt_cli/commands/infrastructure/logs/
shared.rs

1use chrono::{DateTime, Utc};
2use systemprompt_logging::CliService;
3
4use super::LogEntryRow;
5
6pub fn truncate_id(id: &str, max_len: usize) -> String {
7    if id.len() > max_len {
8        format!("{}...", &id[..max_len])
9    } else {
10        id.to_string()
11    }
12}
13
14pub fn format_timestamp(dt: DateTime<Utc>) -> String {
15    dt.format("%Y-%m-%d %H:%M:%S").to_string()
16}
17
18pub fn cost_microdollars_to_dollars(microdollars: i64) -> f64 {
19    microdollars as f64 / 1_000_000.0
20}
21
22pub fn format_duration_ms(ms: Option<i64>) -> String {
23    ms.map_or_else(String::new, |d| format!(" ({}ms)", d))
24}
25
26pub fn display_log_row(log: &LogEntryRow) {
27    let time_part = if log.timestamp.len() >= 23 {
28        &log.timestamp[11..23]
29    } else {
30        &log.timestamp
31    };
32
33    let trace_short = truncate_id(&log.trace_id, 8);
34
35    let line = format!(
36        "{} {} [{}] {}  [{}]",
37        time_part, log.level, log.module, log.message, trace_short
38    );
39
40    match log.level.as_str() {
41        "ERROR" => CliService::error(&line),
42        "WARN" => CliService::warning(&line),
43        _ => CliService::info(&line),
44    }
45}