1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use crate::record::LogRecord;
use std::env;
#[derive(Debug, Clone)]
pub struct FormatOptions {
pub date: bool,
pub colors: bool,
pub compact: bool,
pub columns: Option<usize>,
pub error_level: usize,
pub unicode: bool,
pub show_tag: bool,
pub show_type: bool,
pub show_repetition: bool,
pub show_stack: bool,
pub show_additional: bool,
pub show_meta: bool,
pub force_simple_width: bool,
}
impl Default for FormatOptions {
fn default() -> Self {
Self {
date: true,
colors: true,
compact: false,
columns: None,
error_level: 16,
unicode: true,
show_tag: true,
show_type: true,
show_repetition: true,
show_stack: false,
show_additional: true,
show_meta: true,
force_simple_width: false,
}
}
}
impl FormatOptions {
pub fn adaptive() -> Self {
let mut o = Self::default();
// Env overrides
if env::var("NO_COLOR").is_ok() {
o.colors = false;
}
if let Ok(force) = env::var("FORCE_COLOR") {
if !force.is_empty() && force != "0" {
o.colors = true;
}
}
if let Ok(compact) = env::var("CONSOLA_COMPACT") {
if compact == "1" {
o.compact = true;
}
}
// Terminal width detect
o.columns = detect_terminal_width();
o
}
}
#[derive(Debug, Clone)]
pub struct Segment {
pub text: String,
pub style: Option<SegmentStyle>,
}
#[derive(Debug, Clone)]
pub struct SegmentStyle {
pub fg_color: Option<String>,
pub bg_color: Option<String>,
pub bold: bool,
pub dim: bool,
pub italic: bool,
pub underline: bool,
}
pub fn build_basic_segments(record: &LogRecord, opts: &FormatOptions) -> Vec<Segment> {
let mut v = Vec::new();
if opts.date {
let ts = {
let z = jiff::Zoned::now();
z.to_string()
};
v.push(Segment {
text: ts,
style: Some(SegmentStyle {
fg_color: Some("gray".into()),
bg_color: None,
bold: false,
dim: true,
italic: false,
underline: false,
}),
});
}
if opts.show_type {
v.push(Segment {
text: format!("[{}]", record.type_name),
style: Some(SegmentStyle {
fg_color: Some("cyan".into()),
bg_color: None,
bold: true,
dim: false,
italic: false,
underline: false,
}),
});
}
if opts.show_tag {
if let Some(tag) = &record.tag {
v.push(Segment {
text: format!("[{tag}]"),
style: Some(SegmentStyle {
fg_color: Some("magenta".into()),
bg_color: None,
bold: false,
dim: false,
italic: true,
underline: false,
}),
});
}
}
if let Some(msg) = &record.message {
v.push(Segment {
text: msg.clone(),
style: None,
});
}
if opts.show_repetition && record.repetition_count > 1 {
v.push(Segment {
text: format!(" (x{})", record.repetition_count),
style: Some(SegmentStyle {
fg_color: Some("gray".into()),
bg_color: None,
bold: false,
dim: true,
italic: false,
underline: false,
}),
});
}
// Additional args (if any) appended as JSON-ish list (placeholder formatting)
if opts.show_additional {
if let Some(additional) = &record.additional {
if !additional.is_empty() {
// Stack or error chain
if opts.show_stack {
if let Some(stack) = &record.stack {
if !stack.is_empty() {
for (i, line) in stack.iter().enumerate() {
let prefix = if i == 0 { "\n" } else { "" };
// Enhanced stack line coloring: gray "at", cyan path
let styled_line = if line.trim().starts_with("at ") {
let parts: Vec<&str> = line.splitn(2, "at ").collect();
if parts.len() == 2 {
format!("{}at {}", parts[0], parts[1])
} else {
line.clone()
}
} else {
line.clone()
};
v.push(Segment {
text: format!("{prefix}{styled_line}"),
style: Some(SegmentStyle {
fg_color: Some("gray".into()),
bg_color: None,
bold: false,
dim: true,
italic: false,
underline: false,
}),
});
}
}
} else if let Some(chain) = &record.error_chain {
let limited: Vec<String> =
chain.iter().take(opts.error_level).cloned().collect();
for (i, line) in limited.iter().enumerate() {
let prefix = if i == 0 { "\n" } else { "" };
// Multi-line message normalization with indentation
let text = if i == 0 {
normalize_multiline_message(line, "")
} else {
let caused_by_msg = format!("Caused by: {line}");
normalize_multiline_message(&caused_by_msg, " ") // Indent continuation lines
};
v.push(Segment {
text: format!("{prefix}{text}"),
style: Some(SegmentStyle {
fg_color: Some("red".into()),
bg_color: None,
bold: false,
dim: false,
italic: false,
underline: false,
}),
});
}
if chain.len() > opts.error_level {
v.push(Segment {
text: format!(
"\n(+{} more causes)",
chain.len() - opts.error_level
),
style: Some(SegmentStyle {
fg_color: Some("gray".into()),
bg_color: None,
bold: false,
dim: true,
italic: false,
underline: false,
}),
});
}
}
}
let mut out = String::new();
out.push(' ');
out.push('[');
for (i, a) in additional.iter().enumerate() {
if i > 0 {
out.push_str(", ");
}
out.push_str(&a.to_string());
}
out.push(']');
v.push(Segment {
text: out,
style: Some(SegmentStyle {
fg_color: Some("cyan".into()),
bg_color: None,
bold: false,
dim: true,
italic: false,
underline: false,
}),
});
}
}
}
// Meta key=value pairs
if opts.show_meta {
if let Some(meta) = &record.meta {
if !meta.is_empty() {
let mut out = String::new();
out.push(' ');
out.push('{');
for (i, (k, vval)) in meta.iter().enumerate() {
if i > 0 {
out.push_str(", ");
}
out.push_str(k);
out.push('=');
out.push_str(&vval.to_string());
}
out.push('}');
v.push(Segment {
text: out,
style: Some(SegmentStyle {
fg_color: Some("yellow".into()),
bg_color: None,
bold: false,
dim: true,
italic: false,
underline: false,
}),
});
}
}
}
v
}
/// Attempt to detect terminal column width.
/// Tries in order:
/// 1. COLUMNS environment variable
/// 2. Terminal size detection (Unix only)
/// 3. Falls back to None (let caller decide default)
pub fn detect_terminal_width() -> Option<usize> {
// Try COLUMNS env var first
if let Ok(cols) = env::var("COLUMNS") {
if let Ok(n) = cols.parse::<usize>() {
if n > 0 {
return Some(n);
}
}
}
// Try terminal size detection on Unix
#[cfg(unix)]
{
if let Some(size) = get_terminal_size_unix() {
return Some(size);
}
}
// Windows terminal detection could be added here with winapi
#[cfg(windows)]
{
// TODO: Windows console API for terminal width
}
None
}
#[cfg(unix)]
fn get_terminal_size_unix() -> Option<usize> {
use std::io::{self, IsTerminal};
// Check if stdout is a terminal
if !io::stdout().is_terminal() {
return None;
}
// Use ioctl to get terminal size
// SAFETY: This is safe because we're just querying terminal size
// and not modifying anything
unsafe {
let mut ws: libc::winsize = std::mem::zeroed();
if libc::ioctl(libc::STDOUT_FILENO, libc::TIOCGWINSZ, &mut ws) == 0 && ws.ws_col > 0 {
return Some(ws.ws_col as usize);
}
}
None
}
/// Normalize multi-line error messages with proper indentation
fn normalize_multiline_message(message: &str, indent: &str) -> String {
let lines: Vec<&str> = message.lines().collect();
if lines.len() <= 1 {
return message.to_string();
}
let mut result = String::new();
for (i, line) in lines.iter().enumerate() {
if i > 0 {
result.push('\n');
result.push_str(indent);
}
result.push_str(line);
}
result
}
/// Compute printable width of concatenated segments (simplistic; excludes ANSI codes)
pub fn compute_line_width(segments: &[Segment], opts: &FormatOptions) -> usize {
segments
.iter()
.map(|s| display_width(&s.text, opts.force_simple_width))
.sum()
}
fn display_width(s: &str, force_simple: bool) -> usize {
if force_simple {
return s.chars().count();
}
#[cfg(feature = "fancy")]
{
use unicode_width::UnicodeWidthStr;
UnicodeWidthStr::width(s)
}
#[cfg(not(feature = "fancy"))]
{
s.chars().count()
}
}