use std::collections::BTreeMap;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::sync::Mutex;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
pub struct Logger {
file: Option<Mutex<File>>,
echo: bool,
tz_offset_s: i64,
}
impl Logger {
pub fn new(path: Option<&Path>, echo: bool) -> std::io::Result<Self> {
let file = match path {
Some(p) => Some(Mutex::new(File::create(p)?)),
None => None,
};
Ok(Self { file, echo, tz_offset_s: local_tz_offset() })
}
pub fn line(&self, s: &str) {
if let Some(f) = &self.file {
if let Ok(mut f) = f.lock() {
let _ = writeln!(f, "{s}");
let _ = f.flush();
}
}
if self.echo {
println!("{s}");
}
}
pub fn stamped(&self, s: &str) {
self.line(&format!("[{}] {s}", self.clock()));
}
pub fn stamped_at(&self, at: SystemTime, s: &str) {
self.line(&format!("[{}] {s}", self.clock_at(at)));
}
pub fn clock(&self) -> String {
self.clock_at(SystemTime::now())
}
pub fn clock_at(&self, at: SystemTime) -> String {
let t = at.duration_since(UNIX_EPOCH).unwrap_or_default().as_secs() as i64 + self.tz_offset_s;
let d = t.rem_euclid(86400);
format!("{:02}:{:02}:{:02}", d / 3600, (d / 60) % 60, d % 60)
}
}
fn local_tz_offset() -> i64 {
let out = std::process::Command::new("date").arg("+%z").output();
let Ok(out) = out else { return 0 };
let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
if s.len() != 5 {
return 0;
}
let sign = if s.starts_with('-') { -1 } else { 1 };
let h: i64 = s[1..3].parse().unwrap_or(0);
let m: i64 = s[3..5].parse().unwrap_or(0);
sign * (h * 3600 + m * 60)
}
pub struct Timeline {
file: Mutex<File>,
events: Mutex<Vec<(f64, String)>>,
}
impl Timeline {
pub fn new(path: &Path) -> std::io::Result<Self> {
Ok(Self { file: Mutex::new(File::create(path)?), events: Mutex::new(Vec::new()) })
}
pub fn ev(&self, log: &Logger, s: &str) {
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default();
let line = format!("{}.{:09} {s}", now.as_secs(), now.subsec_nanos());
if let Ok(mut f) = self.file.lock() {
let _ = writeln!(f, "{line}");
let _ = f.flush();
}
if let Ok(mut e) = self.events.lock() {
e.push((now.as_secs_f64(), s.to_string()));
}
log.stamped(s);
}
pub fn relative(&self) -> Vec<(f64, String)> {
let e = self.events.lock().map(|e| e.clone()).unwrap_or_default();
let t0 = e.first().map(|x| x.0).unwrap_or(0.0);
e.into_iter().map(|(t, s)| (t - t0, s)).collect()
}
}
#[derive(Default)]
pub struct StageTimes {
m: Mutex<BTreeMap<String, (Duration, usize)>>,
}
impl StageTimes {
pub fn add(&self, stage: &str, d: Duration) {
if let Ok(mut m) = self.m.lock() {
let e = m.entry(stage.to_string()).or_default();
e.0 += d;
e.1 += 1;
}
}
pub fn snapshot(&self) -> BTreeMap<String, (Duration, usize)> {
self.m.lock().map(|m| m.clone()).unwrap_or_default()
}
}
pub fn timed<T>(log: &Logger, times: &StageTimes, stage: &str, what: &str, f: impl FnOnce() -> T) -> T {
let t = Instant::now();
let r = f();
let d = t.elapsed();
times.add(stage, d);
log.stamped(&format!("[time] {stage} {what}: {:.3}s", d.as_secs_f64()));
r
}
pub fn py_float(x: f64, digits: i32) -> String {
let p = 10f64.powi(digits);
let r = (x * p).round() / p;
let r = if r == 0.0 { 0.0 } else { r };
if !r.is_finite() {
return if r.is_nan() { "NaN".into() } else if r > 0.0 { "Infinity".into() } else { "-Infinity".into() };
}
let s = format!("{r}");
if s.contains('.') || s.contains('e') {
s
} else {
format!("{s}.0")
}
}
#[derive(Clone, Debug)]
pub enum Json {
Null,
Bool(bool),
Int(i64),
Num(String),
Str(String),
Arr(Vec<Json>),
Obj(Vec<(String, Json)>),
}
impl Json {
pub fn obj(items: Vec<(&str, Json)>) -> Json {
Json::Obj(items.into_iter().map(|(k, v)| (k.to_string(), v)).collect())
}
pub fn f(x: f64, digits: i32) -> Json {
Json::Num(py_float(x, digits))
}
pub fn of(x: Option<f64>, digits: i32) -> Json {
x.map_or(Json::Null, |v| Json::f(v, digits))
}
pub fn ints(v: &[usize]) -> Json {
Json::Arr(v.iter().map(|x| Json::Int(*x as i64)).collect())
}
pub fn dump(&self) -> String {
let mut s = String::new();
self.write(&mut s, 0);
s
}
fn write(&self, s: &mut String, ind: usize) {
match self {
Json::Null => s.push_str("null"),
Json::Bool(b) => s.push_str(if *b { "true" } else { "false" }),
Json::Int(i) => s.push_str(&i.to_string()),
Json::Num(n) => s.push_str(n),
Json::Str(t) => push_str_lit(s, t),
Json::Arr(v) => {
if v.is_empty() {
s.push_str("[]");
return;
}
s.push_str("[\n");
for (i, x) in v.iter().enumerate() {
s.push_str(&" ".repeat(ind + 1));
x.write(s, ind + 1);
if i + 1 < v.len() {
s.push(',');
}
s.push('\n');
}
s.push_str(&" ".repeat(ind));
s.push(']');
}
Json::Obj(v) => {
if v.is_empty() {
s.push_str("{}");
return;
}
s.push_str("{\n");
for (i, (k, x)) in v.iter().enumerate() {
s.push_str(&" ".repeat(ind + 1));
push_str_lit(s, k);
s.push_str(": ");
x.write(s, ind + 1);
if i + 1 < v.len() {
s.push(',');
}
s.push('\n');
}
s.push_str(&" ".repeat(ind));
s.push('}');
}
}
}
pub fn py_repr(&self) -> String {
match self {
Json::Null => "None".into(),
Json::Bool(b) => (if *b { "True" } else { "False" }).into(),
Json::Int(i) => i.to_string(),
Json::Num(n) => n.clone(),
Json::Str(t) => format!("'{}'", t.replace('\\', "\\\\").replace('\'', "\\'")),
Json::Arr(v) => format!("[{}]", v.iter().map(|x| x.py_repr()).collect::<Vec<_>>().join(", ")),
Json::Obj(v) => {
format!("{{{}}}", v.iter().map(|(k, x)| format!("'{k}': {}", x.py_repr())).collect::<Vec<_>>().join(", "))
}
}
}
pub fn get(&self, key: &str) -> Option<&Json> {
match self {
Json::Obj(v) => v.iter().find(|(k, _)| k == key).map(|(_, x)| x),
_ => None,
}
}
}
fn push_str_lit(s: &mut String, t: &str) {
s.push('"');
for c in t.chars() {
match c {
'"' => s.push_str("\\\""),
'\\' => s.push_str("\\\\"),
'\n' => s.push_str("\\n"),
'\t' => s.push_str("\\t"),
c if (c as u32) < 0x20 => s.push_str(&format!("\\u{:04x}", c as u32)),
c => s.push(c),
}
}
s.push('"');
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn py_float_matches_python_repr() {
assert_eq!(py_float(1.0, 3), "1.0");
assert_eq!(py_float(12.345, 1), "12.3");
assert_eq!(py_float(-0.0001, 2), "0.0");
assert_eq!(py_float(0.987654, 3), "0.988");
}
#[test]
fn json_dump_indent1() {
let j = Json::obj(vec![("a", Json::Int(1)), ("b", Json::Arr(vec![])), ("c", Json::Arr(vec![Json::Null]))]);
assert_eq!(j.dump(), "{\n \"a\": 1,\n \"b\": [],\n \"c\": [\n null\n ]\n}");
assert_eq!(j.py_repr(), "{'a': 1, 'b': [], 'c': [None]}");
}
}