Skip to content

Commit b8556b2

Browse files
committed
add max_line_length to LogStorage
1 parent 2da7a5d commit b8556b2

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

CHANGELOG.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## Unreleased
77

8+
### Added
9+
10+
- New method `LogStorage::set_max_line_length` to limit the logged line length when capturing
11+
builds logs
12+
813
## [0.16.0] - 2023-05-02
914

1015
### Added
@@ -17,14 +22,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1722

1823
### Changed
1924

20-
* CI toolchains can now install additional targets and components.
25+
- CI toolchains can now install additional targets and components.
2126

2227
## [0.15.1] - 2022-09-04
2328

2429
### Changed
2530

26-
* Updated `nix` dependency to 0.25.
27-
* Replaced `winapi` dependency with `windows-sys`.
31+
- Updated `nix` dependency to 0.25.
32+
- Replaced `winapi` dependency with `windows-sys`.
2833

2934
## [0.15.0] - 2022-05-22
3035

@@ -85,7 +90,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8590

8691
### Changed
8792

88-
* Updated tokio dependency to 1.0.
93+
- Updated tokio dependency to 1.0.
8994

9095
## [0.11.0] - 2020-10-30
9196

src/logging.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ pub struct LogStorage {
113113
min_level: LevelFilter,
114114
max_size: Option<usize>,
115115
max_lines: Option<usize>,
116+
max_line_length: Option<usize>,
116117
}
117118

118119
impl LogStorage {
@@ -127,6 +128,7 @@ impl LogStorage {
127128
min_level,
128129
max_size: None,
129130
max_lines: None,
131+
max_line_length: None,
130132
}
131133
}
132134

@@ -135,6 +137,11 @@ impl LogStorage {
135137
self.max_size = Some(size);
136138
}
137139

140+
/// Set the maximum amount of bytes per line before truncating the line.
141+
pub fn set_max_line_length(&mut self, length: usize) {
142+
self.max_line_length = Some(length);
143+
}
144+
138145
/// Set the maximum amount of lines stored in this struct before truncating the output.
139146
pub fn set_max_lines(&mut self, lines: usize) {
140147
self.max_lines = Some(lines);
@@ -149,6 +156,7 @@ impl LogStorage {
149156
min_level: self.min_level,
150157
max_size: self.max_size,
151158
max_lines: self.max_lines,
159+
max_line_length: self.max_line_length,
152160
}
153161
}
154162
}
@@ -176,7 +184,18 @@ impl SealedLog for LogStorage {
176184
return;
177185
}
178186
}
179-
let message = record.args().to_string();
187+
let mut message = record.args().to_string();
188+
189+
if let Some(max_line_length) = self.max_line_length {
190+
if message.len() > max_line_length {
191+
let mut length = max_line_length - 3;
192+
while !message.is_char_boundary(length) {
193+
length -= 1;
194+
}
195+
message = format!("{}...", &message[..length]);
196+
}
197+
}
198+
180199
if let Some(max_size) = self.max_size {
181200
if inner.size + message.len() >= max_size {
182201
inner.records.push(StoredRecord {
@@ -352,4 +371,28 @@ mod tests {
352371
.message
353372
.contains("too many lines"));
354373
}
374+
375+
#[test]
376+
fn test_too_long_line() {
377+
logging::init();
378+
379+
let mut storage = LogStorage::new(LevelFilter::Info);
380+
storage.set_max_line_length(5);
381+
logging::capture(&storage, || {
382+
info!("short");
383+
info!("this is too long");
384+
info!("a🧗"); // 4 bytes
385+
info!("ab🧗"); // 6 bytes
386+
info!("end");
387+
});
388+
389+
let inner = storage.inner.lock().unwrap();
390+
assert!(!inner.truncated);
391+
assert_eq!(inner.records.len(), 5);
392+
assert_eq!(inner.records[0].message, "short");
393+
assert_eq!(inner.records[1].message, "th...");
394+
assert_eq!(inner.records[2].message, "a🧗");
395+
assert_eq!(inner.records[3].message, "ab...");
396+
assert_eq!(inner.records[4].message, "end");
397+
}
355398
}

0 commit comments

Comments
 (0)