Skip to content

Commit

Permalink
[π˜€π—½π—Ώ] initial version
Browse files Browse the repository at this point in the history
Created using spr 1.3.6-beta.1
  • Loading branch information
sunshowers committed Mar 30, 2024
2 parents 17510a6 + 801c212 commit ea67266
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ sha3 = "0.10.8"
shell-words = "1.1.0"
signal-hook = "0.3"
signal-hook-tokio = { version = "0.3", features = [ "futures-v0_3" ] }
sigpipe = "0.1.3"
similar-asserts = "1.5.0"
sled = "0.34"
sled-agent-client = { path = "clients/sled-agent-client" }
Expand Down
1 change: 1 addition & 0 deletions dev-tools/oxlog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ anyhow.workspace = true
camino.workspace = true
chrono.workspace = true
clap.workspace = true
sigpipe.workspace = true
uuid.workspace = true
omicron-workspace-hack.workspace = true

Expand Down
2 changes: 2 additions & 0 deletions dev-tools/oxlog/src/bin/oxlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ struct FilterArgs {
}

fn main() -> Result<(), anyhow::Error> {
sigpipe::reset();

let cli = Cli::parse();

match cli.command {
Expand Down
85 changes: 85 additions & 0 deletions dev-tools/oxlog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ impl LogFile {
}
}
}

pub fn file_name_cmp(&self, other: &Self) -> std::cmp::Ordering {
self.path.file_name().cmp(&other.path.file_name())
}
}

impl PartialEq for LogFile {
Expand Down Expand Up @@ -142,6 +146,22 @@ pub struct SvcLogs {
pub extra: Vec<LogFile>,
}

impl SvcLogs {
/// Sort the archived and extra log files by filename.
///
/// readdir traverses over directories in indeterminate order, so sort by
/// filename (which is enough to sort by service name and timestamp in most
/// cases).
///
/// Generally we don't want to sort by full path, because log files may be
/// scattered across several different directories -- and we care more
/// about filename than which directory they are in.
pub fn sort_by_file_name(&mut self) {
self.archived.sort_unstable_by(LogFile::file_name_cmp);
self.extra.sort_unstable_by(LogFile::file_name_cmp);
}
}

// These probably don't warrant newtypes. They are just to make the
// keys in maps a bit easier to read.
type ZoneName = String;
Expand Down Expand Up @@ -284,10 +304,19 @@ impl Zones {
load_extra_logs(dir, svc_name, &mut output, filter.show_empty);
}
}

sort_logs(&mut output);

output
}
}

fn sort_logs(output: &mut BTreeMap<String, SvcLogs>) {
for svc_logs in output.values_mut() {
svc_logs.sort_by_file_name();
}
}

const OX_SMF_PREFIXES: [&str; 2] = ["oxide-", "system-illumos-"];

/// Return true if the provided file name appears to be a valid log file for an
Expand Down Expand Up @@ -464,4 +493,60 @@ mod tests {
)
.is_none());
}

#[test]
fn test_sort_logs() {
use super::{LogFile, SvcLogs};
use std::collections::BTreeMap;

let mut logs = BTreeMap::new();
logs.insert(
"blah".to_string(),
SvcLogs {
current: None,
archived: vec![
// "foo" comes after "bar", but the sorted order should
// have 1600000000 before 1700000000.
LogFile {
path: "/bar/blah:default.log.1700000000".into(),
size: None,
modified: None,
},
LogFile {
path: "/foo/blah:default.log.1600000000".into(),
size: None,
modified: None,
},
],
extra: vec![
// "foo" comes after "bar", but the sorted order should
// have log1 before log2.
LogFile {
path: "/foo/blah/sub.default.log1".into(),
size: None,
modified: None,
},
LogFile {
path: "/bar/blah/sub.default.log2".into(),
size: None,
modified: None,
},
],
},
);

super::sort_logs(&mut logs);

let svc_logs = logs.get("blah").unwrap();
assert_eq!(
svc_logs.archived[0].path,
"/foo/blah:default.log.1600000000"
);
assert_eq!(
svc_logs.archived[1].path,
"/bar/blah:default.log.1700000000"
);
assert_eq!(svc_logs.extra[0].path, "/foo/blah/sub.default.log1");
assert_eq!(svc_logs.extra[1].path, "/bar/blah/sub.default.log2");
}
}

0 comments on commit ea67266

Please sign in to comment.