PageRenderTime 141ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/crates/core/messages.rs

https://github.com/BurntSushi/ripgrep
Rust | 74 lines | 45 code | 10 blank | 19 comment | 3 complexity | ed97fe7fb26b08c4cb13fe14b6b23cd2 MD5 | raw file
Possible License(s): MIT, Unlicense
  1. use std::sync::atomic::{AtomicBool, Ordering};
  2. static MESSAGES: AtomicBool = AtomicBool::new(false);
  3. static IGNORE_MESSAGES: AtomicBool = AtomicBool::new(false);
  4. static ERRORED: AtomicBool = AtomicBool::new(false);
  5. /// Emit a non-fatal error message, unless messages were disabled.
  6. #[macro_export]
  7. macro_rules! message {
  8. ($($tt:tt)*) => {
  9. if crate::messages::messages() {
  10. eprintln!($($tt)*);
  11. }
  12. }
  13. }
  14. /// Like message, but sets ripgrep's "errored" flag, which controls the exit
  15. /// status.
  16. #[macro_export]
  17. macro_rules! err_message {
  18. ($($tt:tt)*) => {
  19. crate::messages::set_errored();
  20. message!($($tt)*);
  21. }
  22. }
  23. /// Emit a non-fatal ignore-related error message (like a parse error), unless
  24. /// ignore-messages were disabled.
  25. #[macro_export]
  26. macro_rules! ignore_message {
  27. ($($tt:tt)*) => {
  28. if crate::messages::messages() && crate::messages::ignore_messages() {
  29. eprintln!($($tt)*);
  30. }
  31. }
  32. }
  33. /// Returns true if and only if messages should be shown.
  34. pub fn messages() -> bool {
  35. MESSAGES.load(Ordering::SeqCst)
  36. }
  37. /// Set whether messages should be shown or not.
  38. ///
  39. /// By default, they are not shown.
  40. pub fn set_messages(yes: bool) {
  41. MESSAGES.store(yes, Ordering::SeqCst)
  42. }
  43. /// Returns true if and only if "ignore" related messages should be shown.
  44. pub fn ignore_messages() -> bool {
  45. IGNORE_MESSAGES.load(Ordering::SeqCst)
  46. }
  47. /// Set whether "ignore" related messages should be shown or not.
  48. ///
  49. /// By default, they are not shown.
  50. ///
  51. /// Note that this is overridden if `messages` is disabled. Namely, if
  52. /// `messages` is disabled, then "ignore" messages are never shown, regardless
  53. /// of this setting.
  54. pub fn set_ignore_messages(yes: bool) {
  55. IGNORE_MESSAGES.store(yes, Ordering::SeqCst)
  56. }
  57. /// Returns true if and only if ripgrep came across a non-fatal error.
  58. pub fn errored() -> bool {
  59. ERRORED.load(Ordering::SeqCst)
  60. }
  61. /// Indicate that ripgrep has come across a non-fatal error.
  62. pub fn set_errored() {
  63. ERRORED.store(true, Ordering::SeqCst);
  64. }