log.rs 5.6 KB

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