lib.rs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. //! A logging framework for eBPF programs.
  2. //!
  3. //! This is the user space side of the [Aya] logging framework. For the eBPF
  4. //! side, see the `aya-log-ebpf` crate.
  5. //!
  6. //! `aya-log` provides the [BpfLogger] type, which reads log records created by
  7. //! `aya-log-ebpf` and logs them using the [log] crate. Any logger that
  8. //! implements the [Log] trait can be used with this crate.
  9. //!
  10. //! # Example:
  11. //!
  12. //! This example uses the [env_logger] crate to log messages to the terminal.
  13. //!
  14. //! ```no_run
  15. //! # let mut bpf = aya::Bpf::load(&[]).unwrap();
  16. //! use aya_log::BpfLogger;
  17. //!
  18. //! // initialize env_logger as the default logger
  19. //! env_logger::init();
  20. //!
  21. //! // start reading aya-log records and log them using the default logger
  22. //! BpfLogger::init(&mut bpf).unwrap();
  23. //! ```
  24. //!
  25. //! With the following eBPF code:
  26. //!
  27. //! ```ignore
  28. //! # let ctx = ();
  29. //! use aya_log_ebpf::{debug, error, info, trace, warn};
  30. //!
  31. //! error!(&ctx, "this is an error message 🚨");
  32. //! warn!(&ctx, "this is a warning message ⚠️");
  33. //! info!(&ctx, "this is an info message ℹ️");
  34. //! debug!(&ctx, "this is a debug message ️🐝");
  35. //! trace!(&ctx, "this is a trace message 🔍");
  36. //! ```
  37. //! Outputs:
  38. //!
  39. //! ```text
  40. //! 21:58:55 [ERROR] xxx: [src/main.rs:35] this is an error message 🚨
  41. //! 21:58:55 [WARN] xxx: [src/main.rs:36] this is a warning message ⚠️
  42. //! 21:58:55 [INFO] xxx: [src/main.rs:37] this is an info message ℹ️
  43. //! 21:58:55 [DEBUG] (7) xxx: [src/main.rs:38] this is a debug message ️🐝
  44. //! 21:58:55 [TRACE] (7) xxx: [src/main.rs:39] this is a trace message 🔍
  45. //! ```
  46. //!
  47. //! [Aya]: https://docs.rs/aya
  48. //! [env_logger]: https://docs.rs/env_logger
  49. //! [Log]: https://docs.rs/log/0.4.14/log/trait.Log.html
  50. //! [log]: https://docs.rs/log
  51. //!
  52. use std::{
  53. fmt::{LowerHex, UpperHex},
  54. io, mem,
  55. net::{Ipv4Addr, Ipv6Addr},
  56. ptr, str,
  57. sync::Arc,
  58. };
  59. const MAP_NAME: &str = "AYA_LOGS";
  60. use aya_log_common::{
  61. Argument, DisplayHint, Level, LogValueLength, RecordField, LOG_BUF_CAPACITY, LOG_FIELDS,
  62. };
  63. use bytes::BytesMut;
  64. use log::{error, Log, Record};
  65. use thiserror::Error;
  66. use aya::{
  67. maps::{
  68. perf::{AsyncPerfEventArray, PerfBufferError},
  69. MapError,
  70. },
  71. util::online_cpus,
  72. Bpf, Pod,
  73. };
  74. #[derive(Copy, Clone)]
  75. #[repr(transparent)]
  76. struct RecordFieldWrapper(RecordField);
  77. #[derive(Copy, Clone)]
  78. #[repr(transparent)]
  79. struct ArgumentWrapper(Argument);
  80. #[derive(Copy, Clone)]
  81. #[repr(transparent)]
  82. struct DisplayHintWrapper(DisplayHint);
  83. unsafe impl aya::Pod for RecordFieldWrapper {}
  84. unsafe impl aya::Pod for ArgumentWrapper {}
  85. unsafe impl aya::Pod for DisplayHintWrapper {}
  86. /// Log messages generated by `aya_log_ebpf` using the [log] crate.
  87. ///
  88. /// For more details see the [module level documentation](crate).
  89. pub struct BpfLogger;
  90. impl BpfLogger {
  91. /// Starts reading log records created with `aya-log-ebpf` and logs them
  92. /// with the default logger. See [log::logger].
  93. pub fn init(bpf: &mut Bpf) -> Result<BpfLogger, Error> {
  94. BpfLogger::init_with_logger(bpf, DefaultLogger {})
  95. }
  96. /// Starts reading log records created with `aya-log-ebpf` and logs them
  97. /// with the given logger.
  98. pub fn init_with_logger<T: Log + 'static>(
  99. bpf: &mut Bpf,
  100. logger: T,
  101. ) -> Result<BpfLogger, Error> {
  102. let logger = Arc::new(logger);
  103. let mut logs: AsyncPerfEventArray<_> = bpf
  104. .take_map(MAP_NAME)
  105. .ok_or(Error::MapNotFound)?
  106. .try_into()?;
  107. for cpu_id in online_cpus().map_err(Error::InvalidOnlineCpu)? {
  108. let mut buf = logs.open(cpu_id, None)?;
  109. let log = logger.clone();
  110. tokio::spawn(async move {
  111. let mut buffers = vec![BytesMut::with_capacity(LOG_BUF_CAPACITY); 10];
  112. loop {
  113. let events = buf.read_events(&mut buffers).await.unwrap();
  114. #[allow(clippy::needless_range_loop)]
  115. for i in 0..events.read {
  116. let buf = &mut buffers[i];
  117. log_buf(buf, &*log).unwrap();
  118. }
  119. }
  120. });
  121. }
  122. Ok(BpfLogger {})
  123. }
  124. }
  125. pub trait Formatter<T> {
  126. fn format(v: T) -> String;
  127. }
  128. pub struct DefaultFormatter;
  129. impl<T> Formatter<T> for DefaultFormatter
  130. where
  131. T: ToString,
  132. {
  133. fn format(v: T) -> String {
  134. v.to_string()
  135. }
  136. }
  137. pub struct LowerHexFormatter;
  138. impl<T> Formatter<T> for LowerHexFormatter
  139. where
  140. T: LowerHex,
  141. {
  142. fn format(v: T) -> String {
  143. format!("{v:x}")
  144. }
  145. }
  146. pub struct LowerHexDebugFormatter;
  147. impl<T> Formatter<&[T]> for LowerHexDebugFormatter
  148. where
  149. T: LowerHex,
  150. {
  151. fn format(v: &[T]) -> String {
  152. let mut s = String::new();
  153. for v in v {
  154. let () = core::fmt::write(&mut s, format_args!("{v:x}")).unwrap();
  155. }
  156. s
  157. }
  158. }
  159. pub struct UpperHexFormatter;
  160. impl<T> Formatter<T> for UpperHexFormatter
  161. where
  162. T: UpperHex,
  163. {
  164. fn format(v: T) -> String {
  165. format!("{v:X}")
  166. }
  167. }
  168. pub struct UpperHexDebugFormatter;
  169. impl<T> Formatter<&[T]> for UpperHexDebugFormatter
  170. where
  171. T: UpperHex,
  172. {
  173. fn format(v: &[T]) -> String {
  174. let mut s = String::new();
  175. for v in v {
  176. let () = core::fmt::write(&mut s, format_args!("{v:X}")).unwrap();
  177. }
  178. s
  179. }
  180. }
  181. pub struct Ipv4Formatter;
  182. impl<T> Formatter<T> for Ipv4Formatter
  183. where
  184. T: Into<Ipv4Addr>,
  185. {
  186. fn format(v: T) -> String {
  187. v.into().to_string()
  188. }
  189. }
  190. pub struct Ipv6Formatter;
  191. impl<T> Formatter<T> for Ipv6Formatter
  192. where
  193. T: Into<Ipv6Addr>,
  194. {
  195. fn format(v: T) -> String {
  196. v.into().to_string()
  197. }
  198. }
  199. pub struct LowerMacFormatter;
  200. impl Formatter<[u8; 6]> for LowerMacFormatter {
  201. fn format(v: [u8; 6]) -> String {
  202. format!(
  203. "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
  204. v[0], v[1], v[2], v[3], v[4], v[5]
  205. )
  206. }
  207. }
  208. pub struct UpperMacFormatter;
  209. impl Formatter<[u8; 6]> for UpperMacFormatter {
  210. fn format(v: [u8; 6]) -> String {
  211. format!(
  212. "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
  213. v[0], v[1], v[2], v[3], v[4], v[5]
  214. )
  215. }
  216. }
  217. trait Format {
  218. fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()>;
  219. }
  220. impl Format for &[u8] {
  221. fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
  222. match last_hint.map(|DisplayHintWrapper(dh)| dh) {
  223. Some(DisplayHint::LowerHex) => Ok(LowerHexDebugFormatter::format(self)),
  224. Some(DisplayHint::UpperHex) => Ok(UpperHexDebugFormatter::format(self)),
  225. _ => Err(()),
  226. }
  227. }
  228. }
  229. impl Format for u32 {
  230. fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
  231. match last_hint.map(|DisplayHintWrapper(dh)| dh) {
  232. Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)),
  233. Some(DisplayHint::LowerHex) => Ok(LowerHexFormatter::format(self)),
  234. Some(DisplayHint::UpperHex) => Ok(UpperHexFormatter::format(self)),
  235. Some(DisplayHint::Ip) => Ok(Ipv4Formatter::format(*self)),
  236. Some(DisplayHint::LowerMac) => Err(()),
  237. Some(DisplayHint::UpperMac) => Err(()),
  238. _ => Ok(DefaultFormatter::format(self)),
  239. }
  240. }
  241. }
  242. impl Format for [u8; 6] {
  243. fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
  244. match last_hint.map(|DisplayHintWrapper(dh)| dh) {
  245. Some(DisplayHint::Default) => Err(()),
  246. Some(DisplayHint::LowerHex) => Err(()),
  247. Some(DisplayHint::UpperHex) => Err(()),
  248. Some(DisplayHint::Ip) => Err(()),
  249. Some(DisplayHint::LowerMac) => Ok(LowerMacFormatter::format(*self)),
  250. Some(DisplayHint::UpperMac) => Ok(UpperMacFormatter::format(*self)),
  251. _ => Err(()),
  252. }
  253. }
  254. }
  255. impl Format for [u8; 16] {
  256. fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
  257. match last_hint.map(|DisplayHintWrapper(dh)| dh) {
  258. Some(DisplayHint::Default) => Err(()),
  259. Some(DisplayHint::LowerHex) => Err(()),
  260. Some(DisplayHint::UpperHex) => Err(()),
  261. Some(DisplayHint::Ip) => Ok(Ipv6Formatter::format(*self)),
  262. Some(DisplayHint::LowerMac) => Err(()),
  263. Some(DisplayHint::UpperMac) => Err(()),
  264. _ => Err(()),
  265. }
  266. }
  267. }
  268. impl Format for [u16; 8] {
  269. fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
  270. match last_hint.map(|DisplayHintWrapper(dh)| dh) {
  271. Some(DisplayHint::Default) => Err(()),
  272. Some(DisplayHint::LowerHex) => Err(()),
  273. Some(DisplayHint::UpperHex) => Err(()),
  274. Some(DisplayHint::Ip) => Ok(Ipv6Formatter::format(*self)),
  275. Some(DisplayHint::LowerMac) => Err(()),
  276. Some(DisplayHint::UpperMac) => Err(()),
  277. _ => Err(()),
  278. }
  279. }
  280. }
  281. macro_rules! impl_format {
  282. ($type:ident) => {
  283. impl Format for $type {
  284. fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
  285. match last_hint.map(|DisplayHintWrapper(dh)| dh) {
  286. Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)),
  287. Some(DisplayHint::LowerHex) => Ok(LowerHexFormatter::format(self)),
  288. Some(DisplayHint::UpperHex) => Ok(UpperHexFormatter::format(self)),
  289. Some(DisplayHint::Ip) => Err(()),
  290. Some(DisplayHint::LowerMac) => Err(()),
  291. Some(DisplayHint::UpperMac) => Err(()),
  292. _ => Ok(DefaultFormatter::format(self)),
  293. }
  294. }
  295. }
  296. };
  297. }
  298. impl_format!(i8);
  299. impl_format!(i16);
  300. impl_format!(i32);
  301. impl_format!(i64);
  302. impl_format!(isize);
  303. impl_format!(u8);
  304. impl_format!(u16);
  305. impl_format!(u64);
  306. impl_format!(usize);
  307. macro_rules! impl_format_float {
  308. ($type:ident) => {
  309. impl Format for $type {
  310. fn format(&self, last_hint: Option<DisplayHintWrapper>) -> Result<String, ()> {
  311. match last_hint.map(|DisplayHintWrapper(dh)| dh) {
  312. Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)),
  313. Some(DisplayHint::LowerHex) => Err(()),
  314. Some(DisplayHint::UpperHex) => Err(()),
  315. Some(DisplayHint::Ip) => Err(()),
  316. Some(DisplayHint::LowerMac) => Err(()),
  317. Some(DisplayHint::UpperMac) => Err(()),
  318. _ => Ok(DefaultFormatter::format(self)),
  319. }
  320. }
  321. }
  322. };
  323. }
  324. impl_format_float!(f32);
  325. impl_format_float!(f64);
  326. #[derive(Copy, Clone, Debug)]
  327. struct DefaultLogger;
  328. impl Log for DefaultLogger {
  329. fn enabled(&self, metadata: &log::Metadata) -> bool {
  330. log::logger().enabled(metadata)
  331. }
  332. fn log(&self, record: &Record) {
  333. log::logger().log(record)
  334. }
  335. fn flush(&self) {
  336. log::logger().flush()
  337. }
  338. }
  339. #[derive(Error, Debug)]
  340. pub enum Error {
  341. #[error("log event array {} doesn't exist", MAP_NAME)]
  342. MapNotFound,
  343. #[error("error opening log event array")]
  344. MapError(#[from] MapError),
  345. #[error("error opening log buffer")]
  346. PerfBufferError(#[from] PerfBufferError),
  347. #[error("invalid /sys/devices/system/cpu/online format")]
  348. InvalidOnlineCpu(#[source] io::Error),
  349. }
  350. fn log_buf(mut buf: &[u8], logger: &dyn Log) -> Result<(), ()> {
  351. let mut target = None;
  352. let mut level = None;
  353. let mut module = None;
  354. let mut file = None;
  355. let mut line = None;
  356. let mut num_args = None;
  357. for _ in 0..LOG_FIELDS {
  358. let (RecordFieldWrapper(tag), value, rest) = try_read(buf)?;
  359. match tag {
  360. RecordField::Target => {
  361. target = Some(str::from_utf8(value).map_err(|_| ())?);
  362. }
  363. RecordField::Level => {
  364. level = Some({
  365. let level = unsafe { ptr::read_unaligned(value.as_ptr() as *const _) };
  366. match level {
  367. Level::Error => log::Level::Error,
  368. Level::Warn => log::Level::Warn,
  369. Level::Info => log::Level::Info,
  370. Level::Debug => log::Level::Debug,
  371. Level::Trace => log::Level::Trace,
  372. }
  373. })
  374. }
  375. RecordField::Module => {
  376. module = Some(str::from_utf8(value).map_err(|_| ())?);
  377. }
  378. RecordField::File => {
  379. file = Some(str::from_utf8(value).map_err(|_| ())?);
  380. }
  381. RecordField::Line => {
  382. line = Some(u32::from_ne_bytes(value.try_into().map_err(|_| ())?));
  383. }
  384. RecordField::NumArgs => {
  385. num_args = Some(usize::from_ne_bytes(value.try_into().map_err(|_| ())?));
  386. }
  387. }
  388. buf = rest;
  389. }
  390. let mut full_log_msg = String::new();
  391. let mut last_hint: Option<DisplayHintWrapper> = None;
  392. for _ in 0..num_args.ok_or(())? {
  393. let (ArgumentWrapper(tag), value, rest) = try_read(buf)?;
  394. match tag {
  395. Argument::DisplayHint => {
  396. last_hint = Some(unsafe { ptr::read_unaligned(value.as_ptr() as *const _) });
  397. }
  398. Argument::I8 => {
  399. full_log_msg.push_str(
  400. &i8::from_ne_bytes(value.try_into().map_err(|_| ())?)
  401. .format(last_hint.take())?,
  402. );
  403. }
  404. Argument::I16 => {
  405. full_log_msg.push_str(
  406. &i16::from_ne_bytes(value.try_into().map_err(|_| ())?)
  407. .format(last_hint.take())?,
  408. );
  409. }
  410. Argument::I32 => {
  411. full_log_msg.push_str(
  412. &i32::from_ne_bytes(value.try_into().map_err(|_| ())?)
  413. .format(last_hint.take())?,
  414. );
  415. }
  416. Argument::I64 => {
  417. full_log_msg.push_str(
  418. &i64::from_ne_bytes(value.try_into().map_err(|_| ())?)
  419. .format(last_hint.take())?,
  420. );
  421. }
  422. Argument::Isize => {
  423. full_log_msg.push_str(
  424. &isize::from_ne_bytes(value.try_into().map_err(|_| ())?)
  425. .format(last_hint.take())?,
  426. );
  427. }
  428. Argument::U8 => {
  429. full_log_msg.push_str(
  430. &u8::from_ne_bytes(value.try_into().map_err(|_| ())?)
  431. .format(last_hint.take())?,
  432. );
  433. }
  434. Argument::U16 => {
  435. full_log_msg.push_str(
  436. &u16::from_ne_bytes(value.try_into().map_err(|_| ())?)
  437. .format(last_hint.take())?,
  438. );
  439. }
  440. Argument::U32 => {
  441. full_log_msg.push_str(
  442. &u32::from_ne_bytes(value.try_into().map_err(|_| ())?)
  443. .format(last_hint.take())?,
  444. );
  445. }
  446. Argument::U64 => {
  447. full_log_msg.push_str(
  448. &u64::from_ne_bytes(value.try_into().map_err(|_| ())?)
  449. .format(last_hint.take())?,
  450. );
  451. }
  452. Argument::Usize => {
  453. full_log_msg.push_str(
  454. &usize::from_ne_bytes(value.try_into().map_err(|_| ())?)
  455. .format(last_hint.take())?,
  456. );
  457. }
  458. Argument::F32 => {
  459. full_log_msg.push_str(
  460. &f32::from_ne_bytes(value.try_into().map_err(|_| ())?)
  461. .format(last_hint.take())?,
  462. );
  463. }
  464. Argument::F64 => {
  465. full_log_msg.push_str(
  466. &f64::from_ne_bytes(value.try_into().map_err(|_| ())?)
  467. .format(last_hint.take())?,
  468. );
  469. }
  470. Argument::ArrU8Len6 => {
  471. let value: [u8; 6] = value.try_into().map_err(|_| ())?;
  472. full_log_msg.push_str(&value.format(last_hint.take())?);
  473. }
  474. Argument::ArrU8Len16 => {
  475. let value: [u8; 16] = value.try_into().map_err(|_| ())?;
  476. full_log_msg.push_str(&value.format(last_hint.take())?);
  477. }
  478. Argument::ArrU16Len8 => {
  479. let data: [u8; 16] = value.try_into().map_err(|_| ())?;
  480. let mut value: [u16; 8] = Default::default();
  481. for (i, s) in data.chunks_exact(2).enumerate() {
  482. value[i] = ((s[1] as u16) << 8) | s[0] as u16;
  483. }
  484. full_log_msg.push_str(&value.format(last_hint.take())?);
  485. }
  486. Argument::Bytes => {
  487. full_log_msg.push_str(&value.format(last_hint.take())?);
  488. }
  489. Argument::Str => match str::from_utf8(value) {
  490. Ok(v) => {
  491. full_log_msg.push_str(v);
  492. }
  493. Err(e) => error!("received invalid utf8 string: {}", e),
  494. },
  495. }
  496. buf = rest;
  497. }
  498. logger.log(
  499. &Record::builder()
  500. .args(format_args!("{full_log_msg}"))
  501. .target(target.ok_or(())?)
  502. .level(level.ok_or(())?)
  503. .module_path(module)
  504. .file(file)
  505. .line(line)
  506. .build(),
  507. );
  508. logger.flush();
  509. Ok(())
  510. }
  511. fn try_read<T: Pod>(mut buf: &[u8]) -> Result<(T, &[u8], &[u8]), ()> {
  512. if buf.len() < mem::size_of::<T>() + mem::size_of::<LogValueLength>() {
  513. return Err(());
  514. }
  515. let tag = unsafe { ptr::read_unaligned(buf.as_ptr() as *const T) };
  516. buf = &buf[mem::size_of::<T>()..];
  517. let len =
  518. LogValueLength::from_ne_bytes(buf[..mem::size_of::<LogValueLength>()].try_into().unwrap());
  519. buf = &buf[mem::size_of::<LogValueLength>()..];
  520. let len: usize = len.into();
  521. if buf.len() < len {
  522. return Err(());
  523. }
  524. let (value, rest) = buf.split_at(len);
  525. Ok((tag, value, rest))
  526. }
  527. #[cfg(test)]
  528. mod test {
  529. use super::*;
  530. use aya_log_common::{write_record_header, WriteToBuf};
  531. use log::{logger, Level};
  532. fn new_log(args: usize) -> Result<(usize, Vec<u8>), ()> {
  533. let mut buf = vec![0; 8192];
  534. let len = write_record_header(
  535. &mut buf,
  536. "test",
  537. aya_log_common::Level::Info,
  538. "test",
  539. "test.rs",
  540. 123,
  541. args,
  542. )?;
  543. Ok((len, buf))
  544. }
  545. #[test]
  546. fn test_str() {
  547. testing_logger::setup();
  548. let (mut len, mut input) = new_log(1).unwrap();
  549. len += "test".write(&mut input[len..]).unwrap();
  550. _ = len;
  551. let logger = logger();
  552. let () = log_buf(&input, logger).unwrap();
  553. testing_logger::validate(|captured_logs| {
  554. assert_eq!(captured_logs.len(), 1);
  555. assert_eq!(captured_logs[0].body, "test");
  556. assert_eq!(captured_logs[0].level, Level::Info);
  557. });
  558. }
  559. #[test]
  560. fn test_str_with_args() {
  561. testing_logger::setup();
  562. let (mut len, mut input) = new_log(2).unwrap();
  563. len += "hello ".write(&mut input[len..]).unwrap();
  564. len += "test".write(&mut input[len..]).unwrap();
  565. _ = len;
  566. let logger = logger();
  567. let () = log_buf(&input, logger).unwrap();
  568. testing_logger::validate(|captured_logs| {
  569. assert_eq!(captured_logs.len(), 1);
  570. assert_eq!(captured_logs[0].body, "hello test");
  571. assert_eq!(captured_logs[0].level, Level::Info);
  572. });
  573. }
  574. #[test]
  575. fn test_bytes() {
  576. testing_logger::setup();
  577. let (mut len, mut input) = new_log(2).unwrap();
  578. len += DisplayHint::LowerHex.write(&mut input[len..]).unwrap();
  579. len += [0xde, 0xad].write(&mut input[len..]).unwrap();
  580. _ = len;
  581. let logger = logger();
  582. let () = log_buf(&input, logger).unwrap();
  583. testing_logger::validate(|captured_logs| {
  584. assert_eq!(captured_logs.len(), 1);
  585. assert_eq!(captured_logs[0].body, "dead");
  586. assert_eq!(captured_logs[0].level, Level::Info);
  587. });
  588. }
  589. #[test]
  590. fn test_bytes_with_args() {
  591. testing_logger::setup();
  592. let (mut len, mut input) = new_log(5).unwrap();
  593. len += DisplayHint::LowerHex.write(&mut input[len..]).unwrap();
  594. len += [0xde, 0xad].write(&mut input[len..]).unwrap();
  595. len += " ".write(&mut input[len..]).unwrap();
  596. len += DisplayHint::UpperHex.write(&mut input[len..]).unwrap();
  597. len += [0xbe, 0xef].write(&mut input[len..]).unwrap();
  598. _ = len;
  599. let logger = logger();
  600. let () = log_buf(&input, logger).unwrap();
  601. testing_logger::validate(|captured_logs| {
  602. assert_eq!(captured_logs.len(), 1);
  603. assert_eq!(captured_logs[0].body, "dead BEEF");
  604. assert_eq!(captured_logs[0].level, Level::Info);
  605. });
  606. }
  607. #[test]
  608. fn test_display_hint_default() {
  609. testing_logger::setup();
  610. let (mut len, mut input) = new_log(3).unwrap();
  611. len += "default hint: ".write(&mut input[len..]).unwrap();
  612. len += DisplayHint::Default.write(&mut input[len..]).unwrap();
  613. len += 14.write(&mut input[len..]).unwrap();
  614. _ = len;
  615. let logger = logger();
  616. let () = log_buf(&input, logger).unwrap();
  617. testing_logger::validate(|captured_logs| {
  618. assert_eq!(captured_logs.len(), 1);
  619. assert_eq!(captured_logs[0].body, "default hint: 14");
  620. assert_eq!(captured_logs[0].level, Level::Info);
  621. });
  622. }
  623. #[test]
  624. fn test_display_hint_lower_hex() {
  625. testing_logger::setup();
  626. let (mut len, mut input) = new_log(3).unwrap();
  627. len += "lower hex: ".write(&mut input[len..]).unwrap();
  628. len += DisplayHint::LowerHex.write(&mut input[len..]).unwrap();
  629. len += 200.write(&mut input[len..]).unwrap();
  630. _ = len;
  631. let logger = logger();
  632. let () = log_buf(&input, logger).unwrap();
  633. testing_logger::validate(|captured_logs| {
  634. assert_eq!(captured_logs.len(), 1);
  635. assert_eq!(captured_logs[0].body, "lower hex: c8");
  636. assert_eq!(captured_logs[0].level, Level::Info);
  637. });
  638. }
  639. #[test]
  640. fn test_display_hint_upper_hex() {
  641. testing_logger::setup();
  642. let (mut len, mut input) = new_log(3).unwrap();
  643. len += "upper hex: ".write(&mut input[len..]).unwrap();
  644. len += DisplayHint::UpperHex.write(&mut input[len..]).unwrap();
  645. len += 200.write(&mut input[len..]).unwrap();
  646. _ = len;
  647. let logger = logger();
  648. let () = log_buf(&input, logger).unwrap();
  649. testing_logger::validate(|captured_logs| {
  650. assert_eq!(captured_logs.len(), 1);
  651. assert_eq!(captured_logs[0].body, "upper hex: C8");
  652. assert_eq!(captured_logs[0].level, Level::Info);
  653. });
  654. }
  655. #[test]
  656. fn test_display_hint_ipv4() {
  657. testing_logger::setup();
  658. let (mut len, mut input) = new_log(3).unwrap();
  659. len += "ipv4: ".write(&mut input[len..]).unwrap();
  660. len += DisplayHint::Ip.write(&mut input[len..]).unwrap();
  661. // 10.0.0.1 as u32
  662. len += 167772161u32.write(&mut input[len..]).unwrap();
  663. _ = len;
  664. let logger = logger();
  665. let () = log_buf(&input, logger).unwrap();
  666. testing_logger::validate(|captured_logs| {
  667. assert_eq!(captured_logs.len(), 1);
  668. assert_eq!(captured_logs[0].body, "ipv4: 10.0.0.1");
  669. assert_eq!(captured_logs[0].level, Level::Info);
  670. });
  671. }
  672. #[test]
  673. fn test_display_hint_ipv6_arr_u8_len_16() {
  674. testing_logger::setup();
  675. let (mut len, mut input) = new_log(3).unwrap();
  676. len += "ipv6: ".write(&mut input[len..]).unwrap();
  677. len += DisplayHint::Ip.write(&mut input[len..]).unwrap();
  678. // 2001:db8::1:1 as byte array
  679. let ipv6_arr: [u8; 16] = [
  680. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  681. 0x00, 0x01,
  682. ];
  683. len += ipv6_arr.write(&mut input[len..]).unwrap();
  684. _ = len;
  685. let logger = logger();
  686. let () = log_buf(&input, logger).unwrap();
  687. testing_logger::validate(|captured_logs| {
  688. assert_eq!(captured_logs.len(), 1);
  689. assert_eq!(captured_logs[0].body, "ipv6: 2001:db8::1:1");
  690. assert_eq!(captured_logs[0].level, Level::Info);
  691. });
  692. }
  693. #[test]
  694. fn test_display_hint_ipv6_arr_u16_len_8() {
  695. testing_logger::setup();
  696. let (mut len, mut input) = new_log(3).unwrap();
  697. len += "ipv6: ".write(&mut input[len..]).unwrap();
  698. len += DisplayHint::Ip.write(&mut input[len..]).unwrap();
  699. // 2001:db8::1:1 as u16 array
  700. let ipv6_arr: [u16; 8] = [
  701. 0x2001, 0x0db8, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0001,
  702. ];
  703. len += ipv6_arr.write(&mut input[len..]).unwrap();
  704. _ = len;
  705. let logger = logger();
  706. let () = log_buf(&input, logger).unwrap();
  707. testing_logger::validate(|captured_logs| {
  708. assert_eq!(captured_logs.len(), 1);
  709. assert_eq!(captured_logs[0].body, "ipv6: 2001:db8::1:1");
  710. assert_eq!(captured_logs[0].level, Level::Info);
  711. });
  712. }
  713. #[test]
  714. fn test_display_hint_lower_mac() {
  715. testing_logger::setup();
  716. let (mut len, mut input) = new_log(3).unwrap();
  717. len += "mac: ".write(&mut input[len..]).unwrap();
  718. len += DisplayHint::LowerMac.write(&mut input[len..]).unwrap();
  719. // 00:00:5e:00:53:af as byte array
  720. let mac_arr: [u8; 6] = [0x00, 0x00, 0x5e, 0x00, 0x53, 0xaf];
  721. len += mac_arr.write(&mut input[len..]).unwrap();
  722. _ = len;
  723. let logger = logger();
  724. let () = log_buf(&input, logger).unwrap();
  725. testing_logger::validate(|captured_logs| {
  726. assert_eq!(captured_logs.len(), 1);
  727. assert_eq!(captured_logs[0].body, "mac: 00:00:5e:00:53:af");
  728. assert_eq!(captured_logs[0].level, Level::Info);
  729. });
  730. }
  731. #[test]
  732. fn test_display_hint_upper_mac() {
  733. testing_logger::setup();
  734. let (mut len, mut input) = new_log(3).unwrap();
  735. len += "mac: ".write(&mut input[len..]).unwrap();
  736. len += DisplayHint::UpperMac.write(&mut input[len..]).unwrap();
  737. // 00:00:5E:00:53:AF as byte array
  738. let mac_arr: [u8; 6] = [0x00, 0x00, 0x5e, 0x00, 0x53, 0xaf];
  739. len += mac_arr.write(&mut input[len..]).unwrap();
  740. _ = len;
  741. let logger = logger();
  742. let () = log_buf(&input, logger).unwrap();
  743. testing_logger::validate(|captured_logs| {
  744. assert_eq!(captured_logs.len(), 1);
  745. assert_eq!(captured_logs[0].body, "mac: 00:00:5E:00:53:AF");
  746. assert_eq!(captured_logs[0].level, Level::Info);
  747. });
  748. }
  749. }