Skip to content

Commit

Permalink
add max_line_length to LogStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed May 1, 2024
1 parent 2da7a5d commit b8556b2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
13 changes: 9 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- New method `LogStorage::set_max_line_length` to limit the logged line length when capturing
builds logs

## [0.16.0] - 2023-05-02

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

### Changed

* CI toolchains can now install additional targets and components.
- CI toolchains can now install additional targets and components.

## [0.15.1] - 2022-09-04

### Changed

* Updated `nix` dependency to 0.25.
* Replaced `winapi` dependency with `windows-sys`.
- Updated `nix` dependency to 0.25.
- Replaced `winapi` dependency with `windows-sys`.

## [0.15.0] - 2022-05-22

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

### Changed

* Updated tokio dependency to 1.0.
- Updated tokio dependency to 1.0.

## [0.11.0] - 2020-10-30

Expand Down
45 changes: 44 additions & 1 deletion src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub struct LogStorage {
min_level: LevelFilter,
max_size: Option<usize>,
max_lines: Option<usize>,
max_line_length: Option<usize>,
}

impl LogStorage {
Expand All @@ -127,6 +128,7 @@ impl LogStorage {
min_level,
max_size: None,
max_lines: None,
max_line_length: None,
}
}

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

/// Set the maximum amount of bytes per line before truncating the line.
pub fn set_max_line_length(&mut self, length: usize) {
self.max_line_length = Some(length);
}

/// Set the maximum amount of lines stored in this struct before truncating the output.
pub fn set_max_lines(&mut self, lines: usize) {
self.max_lines = Some(lines);
Expand All @@ -149,6 +156,7 @@ impl LogStorage {
min_level: self.min_level,
max_size: self.max_size,
max_lines: self.max_lines,
max_line_length: self.max_line_length,
}
}
}
Expand Down Expand Up @@ -176,7 +184,18 @@ impl SealedLog for LogStorage {
return;
}
}
let message = record.args().to_string();
let mut message = record.args().to_string();

if let Some(max_line_length) = self.max_line_length {
if message.len() > max_line_length {
let mut length = max_line_length - 3;
while !message.is_char_boundary(length) {
length -= 1;
}
message = format!("{}...", &message[..length]);
}
}

if let Some(max_size) = self.max_size {
if inner.size + message.len() >= max_size {
inner.records.push(StoredRecord {
Expand Down Expand Up @@ -352,4 +371,28 @@ mod tests {
.message
.contains("too many lines"));
}

#[test]
fn test_too_long_line() {
logging::init();

let mut storage = LogStorage::new(LevelFilter::Info);
storage.set_max_line_length(5);
logging::capture(&storage, || {
info!("short");
info!("this is too long");
info!("a🧗"); // 4 bytes
info!("ab🧗"); // 6 bytes
info!("end");
});

let inner = storage.inner.lock().unwrap();
assert!(!inner.truncated);
assert_eq!(inner.records.len(), 5);
assert_eq!(inner.records[0].message, "short");
assert_eq!(inner.records[1].message, "th...");
assert_eq!(inner.records[2].message, "a🧗");
assert_eq!(inner.records[3].message, "ab...");
assert_eq!(inner.records[4].message, "end");
}
}

0 comments on commit b8556b2

Please sign in to comment.