| 1 |
//! Logging for repository events, including commands run in the repository. |
| 2 |
|
| 3 |
use std::ffi::OsString; |
| 4 |
|
| 5 |
use format_bytes::format_bytes; |
| 6 |
use hg::errors::HgError; |
| 7 |
use hg::repo::Repo; |
| 8 |
use hg::utils::files::get_bytes_from_os_str; |
| 9 |
use hg::utils::strings::shell_quote; |
| 10 |
|
| 11 |
use crate::CliInvocation; |
| 12 |
|
| 13 |
// Python does not support %.3f, only %f |
| 14 |
const DEFAULT_DATE_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.3f"; |
| 15 |
|
| 16 |
type DateTime = chrono::DateTime<chrono::Local>; |
| 17 |
|
| 18 |
pub struct ProcessStartTime { |
| 19 |
/// For measuring duration |
| 20 |
monotonic_clock: std::time::Instant, |
| 21 |
/// For formatting with year, month, day, etc. |
| 22 |
calendar_based: DateTime, |
| 23 |
} |
| 24 |
|
| 25 |
impl ProcessStartTime { |
| 26 |
pub fn now() -> Self { |
| 27 |
Self { |
| 28 |
monotonic_clock: std::time::Instant::now(), |
| 29 |
calendar_based: chrono::Local::now(), |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
pub struct Blackbox<'a> { |
| 35 |
process_start_time: &'a ProcessStartTime, |
| 36 |
/// Do nothing if this is `None` |
| 37 |
configured: Option<ConfiguredBlackbox<'a>>, |
| 38 |
} |
| 39 |
|
| 40 |
struct ConfiguredBlackbox<'a> { |
| 41 |
repo: &'a Repo, |
| 42 |
max_size: u64, |
| 43 |
max_files: u32, |
| 44 |
date_format: &'a str, |
| 45 |
} |
| 46 |
|
| 47 |
impl<'a> Blackbox<'a> { |
| 48 |
pub fn new( |
| 49 |
invocation: &'a CliInvocation<'a>, |
| 50 |
process_start_time: &'a ProcessStartTime, |
| 51 |
) -> Result<Self, HgError> { |
| 52 |
let configured = if let Ok(repo) = invocation.repo { |
| 53 |
if !invocation.config.is_extension_enabled(b"blackbox") { |
| 54 |
None |
| 55 |
} else { |
| 56 |
Some(ConfiguredBlackbox { |
| 57 |
repo, |
| 58 |
max_size: invocation |
| 59 |
.config |
| 60 |
.get_byte_size(b"blackbox", b"maxsize")? |
| 61 |
.expect("blackbox.maxsize should have a default value"), |
| 62 |
max_files: invocation |
| 63 |
.config |
| 64 |
.get_u32(b"blackbox", b"maxfiles")? |
| 65 |
.expect( |
| 66 |
"blackbox.maxfiles should have a default value", |
| 67 |
), |
| 68 |
date_format: invocation |
| 69 |
.config |
| 70 |
.get_str(b"blackbox", b"date-format")? |
| 71 |
.map( |
| 72 |
|f| { |
| 73 |
if f.is_empty() { |
| 74 |
DEFAULT_DATE_FORMAT |
| 75 |
} else { |
| 76 |
f |
| 77 |
} |
| 78 |
}, |
| 79 |
) |
| 80 |
.expect( |
| 81 |
"blackbox.date-format should have a default value", |
| 82 |
), |
| 83 |
}) |
| 84 |
} |
| 85 |
} else { |
| 86 |
// Without a local repository there’s no `.hg/blackbox.log` to |
| 87 |
// write to. |
| 88 |
None |
| 89 |
}; |
| 90 |
Ok(Self { process_start_time, configured }) |
| 91 |
} |
| 92 |
|
| 93 |
pub fn log_command_start<'arg>( |
| 94 |
&self, |
| 95 |
argv: impl Iterator<Item = &'arg OsString>, |
| 96 |
) { |
| 97 |
if let Some(configured) = &self.configured { |
| 98 |
let message = format_bytes!(b"(rust) {}", format_cli_args(argv)); |
| 99 |
configured.log(&self.process_start_time.calendar_based, &message); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
pub fn log_command_end<'arg>( |
| 104 |
&self, |
| 105 |
argv: impl Iterator<Item = &'arg OsString>, |
| 106 |
exit_code: i32, |
| 107 |
) { |
| 108 |
if let Some(configured) = &self.configured { |
| 109 |
let now = chrono::Local::now(); |
| 110 |
let duration = |
| 111 |
self.process_start_time.monotonic_clock.elapsed().as_secs_f64(); |
| 112 |
let message = format_bytes!( |
| 113 |
b"(rust) {} exited {} after {} seconds", |
| 114 |
format_cli_args(argv), |
| 115 |
exit_code, |
| 116 |
format_bytes::Utf8(format_args!("{:.03}", duration)) |
| 117 |
); |
| 118 |
configured.log(&now, &message); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
impl ConfiguredBlackbox<'_> { |
| 124 |
fn log(&self, date_time: &DateTime, message: &[u8]) { |
| 125 |
let date = format_bytes::Utf8(date_time.format(self.date_format)); |
| 126 |
let user = get_bytes_from_os_str(whoami::username_os()); |
| 127 |
let rev = format_bytes::Utf8(match self.repo.dirstate_parents() { |
| 128 |
Ok(parents) if parents.p2 == hg::revlog::node::NULL_NODE => { |
| 129 |
format!("{:x}", parents.p1) |
| 130 |
} |
| 131 |
Ok(parents) => format!("{:x}+{:x}", parents.p1, parents.p2), |
| 132 |
Err(_dirstate_corruption_error) => { |
| 133 |
// TODO: log a non-fatal warning to stderr |
| 134 |
"???".to_owned() |
| 135 |
} |
| 136 |
}); |
| 137 |
let pid = std::process::id(); |
| 138 |
let line = format_bytes!( |
| 139 |
b"{} {} @{} ({})> {}\n", |
| 140 |
date, |
| 141 |
user, |
| 142 |
rev, |
| 143 |
pid, |
| 144 |
message |
| 145 |
); |
| 146 |
let result = |
| 147 |
hg::logging::LogFile::new(self.repo.hg_vfs(), "blackbox.log") |
| 148 |
.max_size(Some(self.max_size)) |
| 149 |
.max_files(self.max_files) |
| 150 |
.write(&line); |
| 151 |
match result { |
| 152 |
Ok(()) => {} |
| 153 |
Err(_io_error) => { |
| 154 |
// TODO: log a non-fatal warning to stderr |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
fn format_cli_args<'a>( |
| 161 |
mut args: impl Iterator<Item = &'a OsString>, |
| 162 |
) -> Vec<u8> { |
| 163 |
let _ = args.next(); // Skip the first (or zeroth) arg, the name of the `rhg` executable |
| 164 |
let mut args = args.map(|arg| shell_quote(&get_bytes_from_os_str(arg))); |
| 165 |
let mut formatted = Vec::new(); |
| 166 |
if let Some(arg) = args.next() { |
| 167 |
formatted.extend(arg) |
| 168 |
} |
| 169 |
for arg in args { |
| 170 |
formatted.push(b' '); |
| 171 |
formatted.extend(arg) |
| 172 |
} |
| 173 |
formatted |
| 174 |
} |