Skip to content

Commit

Permalink
Truncate log files in tests and ignore old logs
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Oct 23, 2023
1 parent f7c770a commit b83b070
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions test/test-runner/src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
use log::{Level, LevelFilter, Metadata, Record, SetLoggerError};
use once_cell::sync::Lazy;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use test_rpc::logging::Error;
use test_rpc::logging::{LogFile, LogOutput, Output};
use tokio::{
fs::read_to_string,
fs::File,
io::{self, AsyncBufReadExt, BufReader},
sync::{
broadcast::{channel, Receiver, Sender},
Mutex,
},
};

const MAX_OUTPUT_BUFFER: usize = 10_000;
/// Only consider files that end with ".log"
const INCLUDE_LOG_FILE_EXT: &str = "log";
/// Ignore log files that contain ".old"
const EXCLUDE_LOG_FILE_CONTAIN: &str = ".old";
/// Maximum number of lines that each log file may contain
const TRUNCATE_LOG_FILE_LINES: usize = 100;

pub static LOGGER: Lazy<StdOutBuffer> = Lazy::new(|| {
let (sender, listener) = channel(MAX_OUTPUT_BUFFER);
Expand Down Expand Up @@ -69,7 +77,7 @@ async fn read_settings_file() -> Result<String, Error> {
let mut settings_path = mullvad_paths::get_default_settings_dir()
.map_err(|error| Error::Logs(format!("{}", error)))?;
settings_path.push("settings.json");
read_to_string(&settings_path)
read_truncated(&settings_path)
.await
.map_err(|error| Error::Logs(format!("{}: {}", settings_path.display(), error)))
}
Expand All @@ -82,7 +90,7 @@ async fn read_log_files() -> Result<Vec<Result<LogFile, Error>>, Error> {
.map_err(|error| Error::Logs(format!("{}", error)))?;
let mut log_files = Vec::new();
for path in paths {
let log_file = read_to_string(&path)
let log_file = read_truncated(&path)
.await
.map_err(|error| Error::Logs(format!("{}: {}", path.display(), error)))
.map(|content| LogFile {
Expand All @@ -98,14 +106,33 @@ async fn list_logs<T: AsRef<Path>>(log_dir: T) -> Result<Vec<PathBuf>, Error> {
let mut dir_entries = tokio::fs::read_dir(&log_dir)
.await
.map_err(|e| Error::Logs(format!("{}: {}", log_dir.as_ref().display(), e)))?;
let log_extension = Some(std::ffi::OsStr::new("log"));

let mut paths = Vec::new();
while let Ok(Some(entry)) = dir_entries.next_entry().await {
let path = entry.path();
if path.extension() == log_extension {
if let Some(u8_path) = path.to_str() {
if u8_path.contains(EXCLUDE_LOG_FILE_CONTAIN) {
continue;
}
}
if path.extension() == Some(OsStr::new(INCLUDE_LOG_FILE_EXT)) {
paths.push(path);
}
}
Ok(paths)
}

async fn read_truncated<T: AsRef<Path>>(path: T) -> io::Result<String> {
let mut output = vec![];
let reader = BufReader::new(File::open(path).await?);
let mut lines = reader.lines();
while let Some(line) = lines.next_line().await? {
output.push(line);
}
if output.len() > TRUNCATE_LOG_FILE_LINES {
let drop_count = output.len() - TRUNCATE_LOG_FILE_LINES;
// not the most efficient
output.drain(0..drop_count);
}
Ok(output.join("\n"))
}

0 comments on commit b83b070

Please sign in to comment.