| 1 |
use std::sync::{Arc, Mutex}; |
| 2 |
|
| 3 |
use glib::*; |
| 4 |
|
| 5 |
#[derive(Default)] |
| 6 |
struct Counters { |
| 7 |
criticals: usize, |
| 8 |
warnings: usize, |
| 9 |
messages: usize, |
| 10 |
infos: usize, |
| 11 |
debugs: usize, |
| 12 |
} |
| 13 |
|
| 14 |
fn assert_counts( |
| 15 |
count: &Arc<Mutex<Counters>>, |
| 16 |
criticals: usize, |
| 17 |
warnings: usize, |
| 18 |
messages: usize, |
| 19 |
infos: usize, |
| 20 |
debugs: usize, |
| 21 |
) { |
| 22 |
let count = count.lock().expect("failed to lock 1"); |
| 23 |
assert_eq!(count.criticals, criticals); |
| 24 |
assert_eq!(count.warnings, warnings); |
| 25 |
assert_eq!(count.messages, messages); |
| 26 |
assert_eq!(count.infos, infos); |
| 27 |
assert_eq!(count.debugs, debugs); |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn check_log_handlers() { |
| 32 |
// We set the fatal level explicitly in case it's set outside of the test. |
| 33 |
log_set_fatal_mask(Some("domain"), LogLevels::LEVEL_ERROR); |
| 34 |
|
| 35 |
// |
| 36 |
// log_set_default_handler check part |
| 37 |
// |
| 38 |
let count = Arc::new(Mutex::new(Counters::default())); |
| 39 |
log_set_default_handler(clone!( |
| 40 |
#[weak] |
| 41 |
count, |
| 42 |
move |_, level, _| { |
| 43 |
match level { |
| 44 |
LogLevel::Critical => { |
| 45 |
count.lock().expect("failed to lock 3").criticals += 1; |
| 46 |
} |
| 47 |
LogLevel::Warning => { |
| 48 |
count.lock().expect("failed to lock 4").warnings += 1; |
| 49 |
} |
| 50 |
LogLevel::Message => { |
| 51 |
count.lock().expect("failed to lock 5").messages += 1; |
| 52 |
} |
| 53 |
LogLevel::Info => { |
| 54 |
count.lock().expect("failed to lock 6").infos += 1; |
| 55 |
} |
| 56 |
LogLevel::Debug => { |
| 57 |
count.lock().expect("failed to lock 7").debugs += 1; |
| 58 |
} |
| 59 |
_ => unreachable!(), |
| 60 |
} |
| 61 |
} |
| 62 |
)); |
| 63 |
assert_counts(&count, 0, 0, 0, 0, 0); |
| 64 |
g_warning!(Some("domain"), "hello"); |
| 65 |
assert_counts(&count, 0, 1, 0, 0, 0); |
| 66 |
g_warning!(Some("domain"), "hello"); |
| 67 |
g_critical!(Some("domain"), "hello"); |
| 68 |
g_warning!(Some("domain"), "hello"); |
| 69 |
g_message!(Some("domain"), "hello"); |
| 70 |
g_info!(Some("domain"), "hello"); |
| 71 |
g_debug!(Some("domain"), "hello"); |
| 72 |
g_info!(Some("domain"), "hello"); |
| 73 |
assert_counts(&count, 1, 3, 1, 2, 1); |
| 74 |
|
| 75 |
// We now unset our callback and check if it has really been unset. |
| 76 |
log_unset_default_handler(); |
| 77 |
g_info!(Some("domain"), "hello"); |
| 78 |
g_debug!(Some("domain"), "hello"); |
| 79 |
assert_counts(&count, 1, 3, 1, 2, 1); |
| 80 |
|
| 81 |
// |
| 82 |
// log_set_handler check part |
| 83 |
// |
| 84 |
let count = Arc::new(Mutex::new(Counters::default())); |
| 85 |
// We set the handler for both warning and debug. |
| 86 |
let handler_id = log_set_handler( |
| 87 |
Some("domain"), |
| 88 |
LogLevels::LEVEL_WARNING | LogLevels::LEVEL_DEBUG, |
| 89 |
false, |
| 90 |
false, |
| 91 |
clone!( |
| 92 |
#[weak] |
| 93 |
count, |
| 94 |
move |_, level, _| { |
| 95 |
match level { |
| 96 |
LogLevel::Warning => { |
| 97 |
count.lock().expect("failed to lock 4").warnings += 1; |
| 98 |
} |
| 99 |
LogLevel::Debug => { |
| 100 |
count.lock().expect("failed to lock 7").debugs += 1; |
| 101 |
} |
| 102 |
_ => unreachable!(), |
| 103 |
} |
| 104 |
} |
| 105 |
), |
| 106 |
); |
| 107 |
assert_counts(&count, 0, 0, 0, 0, 0); |
| 108 |
g_warning!(Some("domain"), "hello"); |
| 109 |
assert_counts(&count, 0, 1, 0, 0, 0); |
| 110 |
g_critical!(Some("domain"), "hello"); |
| 111 |
g_message!(Some("domain"), "hello"); |
| 112 |
g_info!(Some("domain"), "hello"); |
| 113 |
assert_counts(&count, 0, 1, 0, 0, 0); |
| 114 |
g_debug!(Some("domain"), "hello"); |
| 115 |
assert_counts(&count, 0, 1, 0, 0, 1); |
| 116 |
// We check that only "domain" messages are calling our callback. |
| 117 |
g_debug!(Some("not-domain"), "hello"); |
| 118 |
g_warning!(Some("not-domain"), "hello"); |
| 119 |
assert_counts(&count, 0, 1, 0, 0, 1); |
| 120 |
|
| 121 |
log_remove_handler(Some("domain"), handler_id); |
| 122 |
g_critical!(Some("domain"), "hello"); |
| 123 |
g_message!(Some("domain"), "hello"); |
| 124 |
g_info!(Some("domain"), "hello"); |
| 125 |
g_debug!(Some("domain"), "hello"); |
| 126 |
g_warning!(Some("domain"), "hello"); |
| 127 |
assert_counts(&count, 0, 1, 0, 0, 1); |
| 128 |
} |