| 1342 | } |
| 1343 | |
| 1344 | fn format_log_record<W: WriteColor>( |
| 1345 | out: &mut W, |
| 1346 | record: &LogRecord<'_>, |
| 1347 | prefix: Option<&str>, |
| 1348 | ) -> Result<(), std::io::Error> { |
| 1349 | // Write prefix if provided |
| 1350 | if let Some(prefix) = prefix { |
| 1351 | out.set_color(ColorSpec::new().set_fg(Some(Color::Cyan)).set_bold(true))?; |
| 1352 | write!(out, "[{}] ", prefix)?; |
| 1353 | out.reset()?; |
| 1354 | } |
| 1355 | |
| 1356 | if let Some(ts) = record.ts { |
| 1357 | out.set_color(ColorSpec::new().set_dimmed(true))?; |
| 1358 | write!(out, "{ts:?} ")?; |
| 1359 | } |
| 1360 | let mut color = ColorSpec::new(); |
| 1361 | let level = match record.level { |
| 1362 | LogLevel::Error => { |
| 1363 | color.set_fg(Some(Color::Red)); |
| 1364 | "ERROR" |
| 1365 | } |
| 1366 | LogLevel::Warn => { |
| 1367 | color.set_fg(Some(Color::Yellow)); |
| 1368 | "WARN" |
| 1369 | } |
| 1370 | LogLevel::Info => { |
| 1371 | color.set_fg(Some(Color::Blue)); |
| 1372 | "INFO" |
| 1373 | } |
| 1374 | LogLevel::Debug => { |
| 1375 | color.set_dimmed(true).set_bold(true); |
| 1376 | "DEBUG" |
| 1377 | } |
| 1378 | LogLevel::Trace => { |
| 1379 | color.set_dimmed(true); |
| 1380 | "TRACE" |
| 1381 | } |
| 1382 | LogLevel::Panic => { |
| 1383 | color.set_fg(Some(Color::Red)).set_bold(true).set_intense(true); |
| 1384 | "PANIC" |
| 1385 | } |
| 1386 | }; |
| 1387 | out.set_color(&color)?; |
| 1388 | write!(out, "{level:>5}: ")?; |
| 1389 | out.reset()?; |
| 1390 | let mut need_space_before_filename = false; |
| 1391 | let mut need_colon_sep = false; |
| 1392 | let dimmed = ColorSpec::new().set_dimmed(true).clone(); |
| 1393 | if let Some(function) = &record.function |
| 1394 | && function.as_ref() != SENTINEL |
| 1395 | { |
| 1396 | out.set_color(&dimmed)?; |
| 1397 | write!(out, "{function}")?; |
| 1398 | out.reset()?; |
| 1399 | need_space_before_filename = true; |
| 1400 | need_colon_sep = true; |
| 1401 | } |