-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtlg.rs
123 lines (98 loc) · 3.64 KB
/
tlg.rs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use tracelogging as tlg;
use ferrisetw::parser::Parser;
use ferrisetw::provider::Provider;
use ferrisetw::schema_locator::SchemaLocator;
use ferrisetw::trace::TraceTrait;
use ferrisetw::trace::UserTrace;
use ferrisetw::EventRecord;
mod utils;
use utils::{Status, TestKind};
const EVENT1_COUNT: u32 = 1;
const EVENT2_COUNT: u32 = 5;
const TEST_STRING_VALUE: &'static str = "TestString";
const PROVIDER_NAME: &'static str = "ferrisETW.TraceLoggingTest";
tlg::define_provider!(FERRIS_PROVIDER, "ferrisETW.TraceLoggingTest");
#[ignore]
#[test]
fn tlg_tests() {
unsafe {
FERRIS_PROVIDER.register();
}
let binding = tlg::Guid::from_name(PROVIDER_NAME).to_utf8_bytes();
let guid = std::str::from_utf8(&binding).unwrap();
tlg_multiple_events(guid);
FERRIS_PROVIDER.unregister();
}
fn generate_tlg_events() {
for _i in 0..EVENT1_COUNT {
tlg::write_event!(
FERRIS_PROVIDER,
"Event1",
level(Warning),
keyword(0x13),
str8("String", TEST_STRING_VALUE),
);
}
for i in 0..EVENT2_COUNT {
tlg::write_event!(
FERRIS_PROVIDER,
"Event2",
level(Informational),
keyword(0x6),
u32("Integer", &i),
);
}
}
fn tlg_multiple_events(provider_guid: &str) {
let passed = Status::new(TestKind::ExpectSuccess);
let notifier = passed.notifier();
let mut event1_count = 0;
let mut event2_count = 0;
let tlg_provider = Provider::by_guid(provider_guid)
.add_callback(
move |record: &EventRecord, schema_locator: &SchemaLocator| {
let schema = schema_locator.event_schema(record).unwrap();
let parser = Parser::create(record, &schema);
// Test event_name function is working as expected & we can handle multiple
// different events.
if record.event_name() == "Event1" {
println!(
"Received Event1({}) from ferrisETW.TraceLoggingTest",
event1_count
);
assert_eq!(record.level(), tlg::Level::Warning.as_int());
assert_eq!(record.keyword(), 0x13);
// Tracelogging crate sets OutTypeUtf8 for str8 which we don't handle at the
// moment.
let _data = parser.try_parse::<String>("String");
// assert!(data.is_ok());
// assert_eq!(data, TEST_STRING_VALUE);
event1_count = event1_count + 1;
} else if record.event_name() == "Event2" {
println!(
"Received Event2({}) from ferrisETW.TraceLoggingTest",
event2_count
);
assert_eq!(record.level(), tlg::Level::Informational.as_int());
assert_eq!(record.keyword(), 0x6);
let data = parser.try_parse::<u32>("Integer");
assert!(data.is_ok());
assert_eq!(data.unwrap(), event2_count);
event2_count = event2_count + 1;
}
if event1_count == EVENT1_COUNT && event2_count == EVENT2_COUNT {
notifier.notify_success();
}
},
)
.build();
let tlg_trace = UserTrace::new()
.enable(tlg_provider)
.start_and_process()
.unwrap();
generate_tlg_events();
passed.assert_passed();
assert!(tlg_trace.events_handled() > 0);
tlg_trace.stop().unwrap();
println!("tlg_multiple_events passed");
}