log.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //! Allocator logging.
  2. //!
  3. //! This allows for detailed logging for `ralloc`.
  4. /// Log to the appropriate source.
  5. ///
  6. /// The first argument this takes is of the form `pool;cursor`, which is used to print the
  7. /// block pools state. `cursor` is what the operation "revolves around" to give a sense of
  8. /// position.
  9. ///
  10. /// If the `;cursor` part is left out, no cursor will be printed.
  11. ///
  12. /// The rest of the arguments are just normal formatters.
  13. #[macro_export]
  14. macro_rules! log {
  15. ($pool:expr, $( $arg:expr ),*) => {
  16. log!($pool;(), $( $arg ),*);
  17. };
  18. ($bk:expr;$cur:expr, $( $arg:expr ),*) => {
  19. #[cfg(feature = "log")]
  20. {
  21. use core::fmt::Write;
  22. use {write, log};
  23. use log::internal::IntoCursor;
  24. // Print the pool state.
  25. let mut log = write::LogWriter::new();
  26. let _ = write!(log, "({:2}) {:10?} : ", $bk.id, log::internal::BlockLogger {
  27. cur: $cur.clone().into_cursor(),
  28. blocks: &$bk.pool,
  29. });
  30. // Print the log message.
  31. let _ = write!(log, $( $arg ),*);
  32. let _ = writeln!(log, " (at {}:{})", file!(), line!());
  33. }
  34. };
  35. }
  36. /// Top secret place-holding module.
  37. #[macro_use]
  38. #[cfg(feature = "log")]
  39. pub mod internal {
  40. use prelude::*;
  41. use core::fmt;
  42. use core::cell::Cell;
  43. use core::ops::Range;
  44. /// A "cursor".
  45. ///
  46. /// Cursors represents a block or an interval in the log output. This trait is implemented for
  47. /// various types that can represent a cursor.
  48. pub trait Cursor {
  49. /// Iteration at n.
  50. ///
  51. /// This is called in the logging loop. The cursor should then write, what it needs, to the
  52. /// formatter if the underlying condition is true.
  53. ///
  54. /// For example, a plain position cursor will write `"|"` when `n == self.pos`.
  55. // TODO: Use an iterator instead.
  56. fn at(&self, f: &mut fmt::Formatter, n: usize) -> fmt::Result;
  57. /// The after hook.
  58. ///
  59. /// This is runned when the loop is over. The aim is to e.g. catch up if the cursor wasn't
  60. /// printed (i.e. is out of range).
  61. fn after(&self, f: &mut fmt::Formatter) -> fmt::Result;
  62. }
  63. /// Types that can be converted into a cursor.
  64. pub trait IntoCursor {
  65. /// The end result.
  66. type Cursor: Cursor;
  67. /// Convert this value into its equivalent cursor.
  68. fn into_cursor(self) -> Self::Cursor;
  69. }
  70. /// A single-point cursor.
  71. pub struct UniCursor {
  72. /// The position where this cursor will be placed.
  73. pos: usize,
  74. /// Is this cursor printed?
  75. ///
  76. /// This is used for the after hook.
  77. is_printed: Cell<bool>,
  78. }
  79. impl Cursor for UniCursor {
  80. fn at(&self, f: &mut fmt::Formatter, n: usize) -> fmt::Result {
  81. if self.pos == n {
  82. self.is_printed.set(true);
  83. write!(f, "|")?;
  84. }
  85. Ok(())
  86. }
  87. fn after(&self, f: &mut fmt::Formatter) -> fmt::Result {
  88. if !self.is_printed.get() {
  89. write!(f, "…|")?;
  90. }
  91. Ok(())
  92. }
  93. }
  94. impl IntoCursor for usize {
  95. type Cursor = UniCursor;
  96. fn into_cursor(self) -> UniCursor {
  97. UniCursor {
  98. pos: self,
  99. is_printed: Cell::new(false),
  100. }
  101. }
  102. }
  103. impl Cursor for () {
  104. fn at(&self, _: &mut fmt::Formatter, _: usize) -> fmt::Result { Ok(()) }
  105. fn after(&self, _: &mut fmt::Formatter) -> fmt::Result { Ok(()) }
  106. }
  107. impl IntoCursor for () {
  108. type Cursor = ();
  109. fn into_cursor(self) -> () {
  110. ()
  111. }
  112. }
  113. /// A interval/range cursor.
  114. ///
  115. /// The start of the range is marked by `[` and the end by `]`.
  116. pub struct RangeCursor {
  117. /// The range of this cursor.
  118. range: Range<usize>,
  119. }
  120. impl Cursor for RangeCursor {
  121. fn at(&self, f: &mut fmt::Formatter, n: usize) -> fmt::Result {
  122. if self.range.start == n {
  123. write!(f, "[")?;
  124. } else if self.range.end == n {
  125. write!(f, "]")?;
  126. }
  127. Ok(())
  128. }
  129. fn after(&self, _: &mut fmt::Formatter) -> fmt::Result { Ok(()) }
  130. }
  131. impl IntoCursor for Range<usize> {
  132. type Cursor = RangeCursor;
  133. fn into_cursor(self) -> RangeCursor {
  134. RangeCursor {
  135. range: self,
  136. }
  137. }
  138. }
  139. /// A "block logger".
  140. ///
  141. /// This intend to show the structure of a block pool. The syntax used is like:
  142. ///
  143. /// ```
  144. /// xxx__|xx_
  145. /// ```
  146. ///
  147. /// where `x` denotes an non-empty block. `_` denotes an empty block, with `|` representing the
  148. /// cursor.
  149. pub struct BlockLogger<'a, T> {
  150. /// The cursor.
  151. ///
  152. /// This is where the `|` will be printed.
  153. pub cur: T,
  154. /// The blocks.
  155. pub blocks: &'a [Block],
  156. }
  157. impl<'a, T: Cursor> fmt::Debug for BlockLogger<'a, T> {
  158. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  159. // TODO: Handle alignment etc.
  160. for (n, i) in self.blocks.iter().enumerate() {
  161. self.cur.at(f, n)?;
  162. if i.is_empty() {
  163. // Empty block.
  164. write!(f, "_")?;
  165. } else {
  166. // Non-empty block.
  167. write!(f, "x")?;
  168. }
  169. }
  170. self.cur.after(f)?;
  171. Ok(())
  172. }
  173. }
  174. }