Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

logging.rs 2.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  1. use structopt::StructOpt;
  2. use indicatif::ProgressBar;
  3. use log::*;
  4. use std::sync::atomic::{AtomicPtr, Ordering};
  5. use std::ptr;
  6. use crate::error::Result;
  7. static mut LOG_LEVEL: LevelFilter = LevelFilter::Info;
  8. static LOG_PB: AtomicPtr<ProgressBar> = AtomicPtr::new(ptr::null_mut());
  9. static LOGGER: LogEnv = LogEnv {};
  10. struct LogEnv {}
  11. impl Log for LogEnv {
  12. fn enabled(&self, metadata: &Metadata) -> bool {
  13. unsafe {
  14. metadata.level() <= LOG_LEVEL
  15. }
  16. }
  17. fn log(&self, record: &Record) {
  18. let pass = unsafe {
  19. record.level() <= LOG_LEVEL
  20. };
  21. if pass {
  22. let pb_ptr = LOG_PB.load(Ordering::Relaxed);
  23. if pb_ptr.is_null() {
  24. eprintln!("[{:>5}] {}", record.level(), record.args());
  25. } else {
  26. let msg = format!("[{:>5}] {}", record.level(), record.args());
  27. unsafe {
  28. let pb = &*pb_ptr;
  29. pb.println(msg)
  30. }
  31. }
  32. }
  33. }
  34. fn flush(&self) {}
  35. }
  36. fn verbosify(f: LevelFilter) -> LevelFilter {
  37. match f {
  38. LevelFilter::Error => LevelFilter::Warn,
  39. LevelFilter::Warn => LevelFilter::Info,
  40. LevelFilter::Info => LevelFilter::Debug,
  41. LevelFilter::Debug => LevelFilter::Trace,
  42. x => x
  43. }
  44. }
  45. #[derive(StructOpt, Debug)]
  46. pub struct LogOpts {
  47. /// Verbose mode (-v, -vv, -vvv, etc.)
  48. #[structopt(short="v", long="verbose", parse(from_occurrences))]
  49. verbose: usize,
  50. /// Silence output
  51. #[structopt(short="q", long="quiet")]
  52. quiet: bool
  53. }
  54. impl LogOpts {
  55. /// Initialize logging
  56. pub fn init(&self) -> Result<()> {
  57. unsafe {
  58. if self.quiet {
  59. LOG_LEVEL = LevelFilter::Off;
  60. }
  61. for _i in 0..self.verbose {
  62. LOG_LEVEL = verbosify(LOG_LEVEL);
  63. }
  64. }
  65. set_logger(&LOGGER)?;
  66. unsafe {
  67. set_max_level(LOG_LEVEL);
  68. }
  69. Ok(())
  70. }
  71. }
  72. pub fn set_progress(pb: &ProgressBar) {
  73. let pbb = Box::new(pb.clone());
  74. LOG_PB.store(Box::leak(pbb), Ordering::Relaxed);
  75. }
  76. pub fn clear_progress() {
  77. LOG_PB.store(ptr::null_mut(), Ordering::Relaxed);
  78. }
  79. #[test]
  80. fn test_default_logenv() {
  81. unsafe {
  82. assert!(Level::Info <= LOG_LEVEL);
  83. assert!(Level::Warn <= LOG_LEVEL);
  84. assert!(Level::Debug > LOG_LEVEL);
  85. }
  86. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...