//! Fetch a miner's log for the "View log (API)" viewer (round 12 #12, reworked //! round 19 #11-16). //! //! The log is fetched through asic-rs's per-backend `read_logs()` API, which //! knows each firmware's real transport (Antminer/Z15 + BixBit stock web, //! Braiins gRPC/journal, VNish and ePIC HTTP, ...). This replaced the GUI's //! narrow three-way curl fetch, whose stock `/cgi-bin/log.cgi` `else` branch //! failed on every firmware that does not serve that CGI (Braiins, LuxOS, Z15, //! BixBit). //! //! WhatsMiner is the exception: its btminer log is not exposed over the API, and //! asic-rs has no `read_logs` for it, so the viewer falls back to the active //! fault codes (get_error_code on :4028, unauthenticated). LuxOS likewise has no //! asic-rs log API and reports the log as unavailable. /// Cap on the log text we keep for display (bytes); a miner's freshest lines are /// at the tail, so we keep the tail. const MAX_LOG_BYTES: usize = 256 * 1024; /// A fetched log for the viewer. pub struct LogScan { /// The (possibly tail-trimmed) log text. pub text: String, } /// Fetch one miner's log. `brand` is the make ("Antminer"/"Whatsminer"/...); /// `timeout_secs` is the per-miner identification timeout. Returns `Err` only /// when no log could be read. pub fn scan_log(ip: &str, brand: &str, timeout_secs: u64) -> Result { // WhatsMiner's real log is not exposed over its API and asic-rs has no // read_logs for it, so show the active fault codes instead of failing. if brand.eq_ignore_ascii_case("whatsminer") { let resp = crate::access_probe::whatsminer_get_error_code(ip) .ok_or_else(|| "WhatsMiner did not answer get_error_code on :4028".to_string())?; return Ok(LogScan { text: tail(&resp, MAX_LOG_BYTES) }); } let text = crate::scan::fetch_log(ip, timeout_secs)?; if text.trim().is_empty() { return Err("the miner returned an empty log".to_string()); } Ok(LogScan { text: tail(&text, MAX_LOG_BYTES) }) } /// Keep the last `max` bytes of `s` (a log's tail is the freshest), on a line /// boundary, with a marker when trimmed. fn tail(s: &str, max: usize) -> String { if s.len() <= max { return s.to_string(); } // Nudge the cut forward to a UTF-8 char boundary first: a raw byte offset can // land inside a multi-byte character and slicing there would panic (round 18 // #12; miner logs are not guaranteed pure ASCII). let mut start = s.len() - max; while start < s.len() && !s.is_char_boundary(start) { start += 1; } // then advance to the next line boundary so we don't cut a line in half let start = s[start..].find('\n').map(|i| start + i + 1).unwrap_or(start); format!("[... earlier log trimmed ...]\n{}", &s[start..]) } #[cfg(test)] mod tests { use super::*; #[test] fn tail_does_not_panic_mid_multibyte_char() { // A byte-offset cut can land inside a multi-byte character; tail must nudge // to a char boundary rather than panic (round 18 #12). let s = "\u{00e9}".repeat(200); // 400 bytes of 2-byte chars, no newlines let out = tail(&s, 101); // cut at an odd offset -> inside a char assert!(out.starts_with("[... earlier log trimmed ...]")); assert!(out.len() < s.len()); } #[test] fn tail_trims_on_line_boundary() { let log = "aaaa\nbbbb\ncccc\n"; let t = tail(log, 6); assert!(t.starts_with("[... earlier log trimmed ...]"), "{t}"); assert!(t.ends_with("cccc\n")); } }