/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Gecko-esque logger implementation for the [`log`] crate. //! //! The [`log`] crate provides a single logging API that abstracts over the //! actual logging implementation. This module uses the logging API //! to provide a log implementation that shares many aesthetical traits with //! [Log.sys.mjs] from Gecko. //! //! Using the [`error!`], [`warn!`], [`info!`], [`debug!`], and //! [`trace!`] macros from `log` will output a timestamp field, followed by the //! log level, and then the message. The fields are separated by a tab //! character, making the output suitable for further text processing with //! `awk(1)`. //! //! This module shares the same API as `log`, except it provides additional //! entry functions [`init`] and [`init_with_level`] and additional log levels //! `Level::Fatal` and `Level::Config`. Converting these into the //! [`log::Level`] is lossy so that `Level::Fatal` becomes `log::Level::Error` //! and `Level::Config` becomes `log::Level::Debug`. //! //! [`log`]: https://docs.rs/log/newest/log/ //! [Log.sys.mjs]: https://searchfox.org/mozilla-central/source/toolkit/modules/Log.sys.mjs //! [`error!`]: https://docs.rs/log/newest/log/macro.error.html //! [`warn!`]: https://docs.rs/log/newest/log/macro.warn.html //! [`info!`]: https://docs.rs/log/newest/log/macro.info.html //! [`debug!`]: https://docs.rs/log/newest/log/macro.debug.html //! [`trace!`]: https://docs.rs/log/newest/log/macro.trace.html //! [`init`]: fn.init.html //! [`init_with_level`]: fn.init_with_level.html
use icu_segmenter::GraphemeClusterSegmenter; use std::fmt; use std::io; use std::io::Write; use std::str; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
/// Produces a 13-digit Unix Epoch timestamp similar to Gecko. fn format_ts(ts: chrono::DateTime<chrono::Local>) -> String {
format!("{}{:03}", ts.timestamp(), ts.timestamp_subsec_millis())
}
/// Truncate a log message if it's too long fn truncate_message(args: &fmt::Arguments) -> Option<(String, String)> { // Don't truncate the message if requested. if !truncate() { return None;
}
let message = format!("{}", args); if message.is_empty() || message.len() < MAX_STRING_LENGTH { return None;
} let chars = GraphemeClusterSegmenter::new()
.segment_str(&message)
.collect::<Vec<_>>()
.windows(2)
.map(|i| &message[i[0]..i[1]])
.collect::<Vec<&str>>();
if chars.len() > MAX_STRING_LENGTH { let middle: usize = MAX_STRING_LENGTH / 2; let s1 = chars[0..middle].concat(); let s2 = chars[chars.len() - middle..].concat();
Some((s1, s2))
} else {
None
}
}
#[test] fn test_level_repr() {
assert_eq!(Level::Fatal as usize, 70);
assert_eq!(Level::Error as usize, 60);
assert_eq!(Level::Warn as usize, 50);
assert_eq!(Level::Info as usize, 40);
assert_eq!(Level::Config as usize, 30);
assert_eq!(Level::Debug as usize, 20);
assert_eq!(Level::Trace as usize, 10);
}
#[test] fn test_format_ts() { let ts = chrono::Local::now(); let s = format_ts(ts);
assert_eq!(s.len(), 13);
}
#[test] fn test_truncate() { let short_message = (0..MAX_STRING_LENGTH).map(|_| "x").collect::<String>(); // A message up to MAX_STRING_LENGTH is not truncated
assert_eq!(truncate_message(&format_args!("{}", short_message)), None);
let long_message = (0..MAX_STRING_LENGTH + 1).map(|_| "x").collect::<String>(); let part = (0..MAX_STRING_LENGTH / 2).map(|_| "x").collect::<String>();
// A message longer than MAX_STRING_LENGTH is not truncated if requested
set_truncate(false);
assert_eq!(truncate_message(&format_args!("{}", long_message)), None);
// A message longer than MAX_STRING_LENGTH is truncated if requested
set_truncate(true);
assert_eq!(
truncate_message(&format_args!("{}", long_message)),
Some((part.to_owned(), part))
);
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.