##// END OF EJS Templates
rust: blanket implementation of Graph for Graph references...
rust: blanket implementation of Graph for Graph references The need comes from the fact that `AncestorsIterator` and many Graph-related algorithms take ownership of the `Graph` they work with. This, in turn is due to them needing to accept the `Index` instances that are provided by the Python layers (that neither rhg nor `RHGitaly` use, of course): the fact that nowadays the Python layer holds an object that is itself implemented in Rust does not change the core problem that they cannot be tracked by the borrow checker. Even though it looks like cloning `Changelog` would be cheap, it seems hard to guarantee that on the long run. The object is already too rich for us to be comfortable with it, when using references is the most natural and guaranteed way of proceeding. The added test seems a bit superfleous, but it will act as a reminder that this feature is really useful until something in the Mercurial code base actually uses it.

File last commit:

r51659:58aa5ee9 default
r52512:b08c5fbe stable
Show More
blackbox.rs
177 lines | 5.5 KiB | application/rls-services+xml | RustLexer
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 //! Logging for repository events, including commands run in the repository.
use crate::CliInvocation;
use format_bytes::format_bytes;
use hg::errors::HgError;
use hg::repo::Repo;
use hg::utils::{files::get_bytes_from_os_str, shell_quote};
Arseniy Alekseyev
rhg: refactor to pass argv down, instead of caling args_os()...
r49961 use std::ffi::OsString;
Simon Sapin
rhg: Add support for the blackbox extension...
r47343
// Python does not support %.3f, only %f
Valentin Gatien-Baron
blackbox: change year in logs to ISO 8601 format...
r49421 const DEFAULT_DATE_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.3f";
Simon Sapin
rhg: Add support for the blackbox extension...
r47343
type DateTime = chrono::DateTime<chrono::Local>;
pub struct ProcessStartTime {
/// For measuring duration
monotonic_clock: std::time::Instant,
/// For formatting with year, month, day, etc.
calendar_based: DateTime,
}
impl ProcessStartTime {
pub fn now() -> Self {
Self {
monotonic_clock: std::time::Instant::now(),
calendar_based: chrono::Local::now(),
}
}
}
pub struct Blackbox<'a> {
process_start_time: &'a ProcessStartTime,
/// Do nothing if this is `None`
configured: Option<ConfiguredBlackbox<'a>>,
}
struct ConfiguredBlackbox<'a> {
repo: &'a Repo,
max_size: u64,
max_files: u32,
date_format: &'a str,
}
impl<'a> Blackbox<'a> {
pub fn new(
invocation: &'a CliInvocation<'a>,
process_start_time: &'a ProcessStartTime,
) -> Result<Self, HgError> {
let configured = if let Ok(repo) = invocation.repo {
Raphaël Gomès
rust-blackbox: use `is_extension_enabled` config helper...
r51659 if !invocation.config.is_extension_enabled(b"blackbox") {
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 None
} else {
Some(ConfiguredBlackbox {
repo,
Simon Sapin
rhg: Make configuration available as early as possible in main()...
r47423 max_size: invocation
.config
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 .get_byte_size(b"blackbox", b"maxsize")?
Raphaël Gomès
configitems: move blackbox's config items to the new configitems.toml...
r51658 .expect(
"blackbox.maxsize should have a default value",
),
Simon Sapin
rhg: Make configuration available as early as possible in main()...
r47423 max_files: invocation
.config
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 .get_u32(b"blackbox", b"maxfiles")?
Raphaël Gomès
configitems: move blackbox's config items to the new configitems.toml...
r51658 .expect(
"blackbox.maxfiles should have a default value",
),
Simon Sapin
rhg: Make configuration available as early as possible in main()...
r47423 date_format: invocation
.config
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 .get_str(b"blackbox", b"date-format")?
Raphaël Gomès
configitems: move blackbox's config items to the new configitems.toml...
r51658 .map(|f| {
if f.is_empty() {
DEFAULT_DATE_FORMAT
} else {
f
}
})
.expect(
"blackbox.date-format should have a default value",
),
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 })
}
} else {
// Without a local repository there’s no `.hg/blackbox.log` to
// write to.
None
};
Ok(Self {
process_start_time,
configured,
})
}
Arseniy Alekseyev
rhg: refactor to pass argv down, instead of caling args_os()...
r49961 pub fn log_command_start<'arg>(
&self,
argv: impl Iterator<Item = &'arg OsString>,
) {
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 if let Some(configured) = &self.configured {
Arseniy Alekseyev
rhg: refactor to pass argv down, instead of caling args_os()...
r49961 let message = format_bytes!(b"(rust) {}", format_cli_args(argv));
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 configured.log(&self.process_start_time.calendar_based, &message);
}
}
Arseniy Alekseyev
rhg: refactor to pass argv down, instead of caling args_os()...
r49961 pub fn log_command_end<'arg>(
&self,
argv: impl Iterator<Item = &'arg OsString>,
exit_code: i32,
) {
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 if let Some(configured) = &self.configured {
let now = chrono::Local::now();
let duration = self
.process_start_time
.monotonic_clock
.elapsed()
.as_secs_f64();
let message = format_bytes!(
b"(rust) {} exited {} after {} seconds",
Arseniy Alekseyev
rhg: refactor to pass argv down, instead of caling args_os()...
r49961 format_cli_args(argv),
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 exit_code,
format_bytes::Utf8(format_args!("{:.03}", duration))
);
configured.log(&now, &message);
}
}
}
impl ConfiguredBlackbox<'_> {
fn log(&self, date_time: &DateTime, message: &[u8]) {
let date = format_bytes::Utf8(date_time.format(self.date_format));
Raphaël Gomès
rust-dependencies: switch from `users` to `whoami`...
r51576 let user = get_bytes_from_os_str(whoami::username_os());
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 let rev = format_bytes::Utf8(match self.repo.dirstate_parents() {
Ok(parents) if parents.p2 == hg::revlog::node::NULL_NODE => {
format!("{:x}", parents.p1)
}
Ok(parents) => format!("{:x}+{:x}", parents.p1, parents.p2),
Err(_dirstate_corruption_error) => {
// TODO: log a non-fatal warning to stderr
"???".to_owned()
}
});
let pid = std::process::id();
let line = format_bytes!(
b"{} {} @{} ({})> {}\n",
date,
user,
rev,
pid,
message
);
let result =
hg::logging::LogFile::new(self.repo.hg_vfs(), "blackbox.log")
.max_size(Some(self.max_size))
.max_files(self.max_files)
.write(&line);
match result {
Ok(()) => {}
Err(_io_error) => {
// TODO: log a non-fatal warning to stderr
}
}
}
}
Arseniy Alekseyev
rhg: refactor to pass argv down, instead of caling args_os()...
r49961 fn format_cli_args<'a>(
mut args: impl Iterator<Item = &'a OsString>,
) -> Vec<u8> {
Simon Sapin
rhg: Add support for the blackbox extension...
r47343 let _ = args.next(); // Skip the first (or zeroth) arg, the name of the `rhg` executable
let mut args = args.map(|arg| shell_quote(&get_bytes_from_os_str(arg)));
let mut formatted = Vec::new();
if let Some(arg) = args.next() {
formatted.extend(arg)
}
for arg in args {
formatted.push(b' ');
formatted.extend(arg)
}
formatted
}