A backend for the p≡p Engine built on Sequoia.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.7 KiB

12 months ago
  1. use std::cell::RefCell;
  2. // Like eprintln!
  3. macro_rules! log {
  4. ($dst:expr $(,)?) => (
  5. eprintln!("{}", $dst)
  6. );
  7. ($dst:expr, $($arg:tt)*) => (
  8. eprintln!("{}", std::format!($dst, $($arg)*))
  9. );
  10. }
  11. // The indent level. It is increased with each call to tracer and
  12. // decremented when the tracker goes out of scope.
  13. thread_local! {
  14. pub static INDENT_LEVEL: RefCell<usize> = RefCell::new(0);
  15. }
  16. // Like eprintln!, but the first argument is a boolean, which
  17. // indicates if the string should actually be printed.
  18. macro_rules! trace {
  19. ( $TRACE:expr, $fmt:expr, $($pargs:expr),* ) => {
  20. if $TRACE {
  21. let indent_level = crate::log::INDENT_LEVEL.with(|i| {
  22. *i.borrow()
  23. });
  24. let ws = " ";
  25. log!("{}{}",
  26. &ws[0..std::cmp::min(ws.len(), cmp::max(1, indent_level) - 1)],
  27. format!($fmt, $($pargs),*));
  28. }
  29. };
  30. ( $TRACE:expr, $fmt:expr ) => {
  31. trace!($TRACE, $fmt, );
  32. };
  33. }
  34. macro_rules! tracer {
  35. ( $TRACE:expr, $func:expr ) => {
  36. // Currently, Rust doesn't support $( ... ) in a nested
  37. // macro's definition. See:
  38. // https://users.rust-lang.org/t/nested-macros-issue/8348/2
  39. macro_rules! t {
  40. ( $fmt:expr ) =>
  41. { trace!($TRACE, "{}: {}", $func, $fmt) };
  42. ( $fmt:expr, $a:expr ) =>
  43. { trace!($TRACE, "{}: {}", $func, format!($fmt, $a)) };
  44. ( $fmt:expr, $a:expr, $b:expr ) =>
  45. { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b)) };
  46. ( $fmt:expr, $a:expr, $b:expr, $c:expr ) =>
  47. { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c)) };
  48. ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr ) =>
  49. { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d)) };
  50. ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr ) =>
  51. { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e)) };
  52. ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr ) =>
  53. { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e, $f)) };
  54. ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr ) =>
  55. { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g)) };
  56. ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr ) =>
  57. { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h)) };
  58. ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr ) =>
  59. { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i)) };
  60. ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr ) =>
  61. { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j)) };
  62. ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr, $k:expr ) =>
  63. { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k)) };
  64. }
  65. struct Indent {};
  66. impl Indent {
  67. fn init() -> Self {
  68. crate::log::INDENT_LEVEL.with(|i| {
  69. i.replace_with(|i| *i + 1);
  70. });
  71. Indent {}
  72. }
  73. }
  74. impl Drop for Indent {
  75. fn drop(&mut self) {
  76. crate::log::INDENT_LEVEL.with(|i| {
  77. i.replace_with(|i| *i - 1);
  78. });
  79. }
  80. }
  81. let _indent = Indent::init();
  82. }
  83. }