Skip to content

Commit 736409a

Browse files
committed
* Adding back internals
* Making quality of life improvements to the types * Adding testing to types
1 parent 96ff42e commit 736409a

File tree

5 files changed

+231
-128
lines changed

5 files changed

+231
-128
lines changed

wrappers/rust/oneds-telemetry/src/appender.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl log::Log for TelemetryCollectorLogBridge {
4242

4343
fn flush(&self) {
4444
if unsafe { COLLECTOR_ENABLED } {
45-
log_manager_provider().flush();
45+
log_manager_provider().flush(false);
4646
}
4747
}
4848
}

wrappers/rust/oneds-telemetry/src/internals.rs

Lines changed: 126 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,130 @@
1-
use std::os::raw::c_void;
1+
use std::{ffi::CStr, mem, os::raw::c_void};
22

3-
use crate::*;
3+
use crate::{client_library::Library, *};
44

5-
pub type evt_api_call_t = extern "stdcall" fn(context: *mut evt_context_t) -> evt_status_t;
5+
type evt_api_call_t = extern "stdcall" fn(context: *mut evt_context_t) -> evt_status_t;
6+
7+
pub fn evt_open(config: *mut c_void) -> Option<evt_handle_t> {
8+
let mut context: Box<evt_context_t> = Box::new(evt_context_t {
9+
call: evt_call_t_EVT_OP_OPEN,
10+
handle: 0,
11+
data: config,
12+
result: 0,
13+
size: 0,
14+
});
15+
16+
let raw_pointer = Box::into_raw(context);
17+
18+
let ct_lib = Library::new("ClientTelemetry.dll").unwrap();
19+
let evt_api_call_default: evt_api_call_t =
20+
unsafe { ct_lib.get_proc("evt_api_call_default").unwrap() };
21+
22+
let result = evt_api_call_default(raw_pointer);
23+
24+
if result == -1 {
25+
return Option::None;
26+
}
27+
28+
context = unsafe { Box::from_raw(raw_pointer) };
29+
30+
return Some(context.handle.clone());
31+
}
32+
33+
pub fn evt_close(handle: &evt_handle_t) -> evt_status_t {
34+
let context: Box<evt_context_t> = Box::new(evt_context_t {
35+
call: evt_call_t_EVT_OP_CLOSE,
36+
handle: *handle,
37+
data: std::ptr::null_mut(),
38+
result: 0,
39+
size: 0,
40+
});
41+
42+
let raw_pointer = Box::into_raw(context);
43+
44+
let ct_lib = Library::new("ClientTelemetry.dll").unwrap();
45+
let evt_api_call_default: evt_api_call_t =
46+
unsafe { ct_lib.get_proc("evt_api_call_default").unwrap() };
47+
48+
return evt_api_call_default(raw_pointer);
49+
}
50+
51+
pub fn evt_upload(handle: &evt_handle_t) -> evt_status_t {
52+
let context: Box<evt_context_t> = Box::new(evt_context_t {
53+
call: evt_call_t_EVT_OP_UPLOAD,
54+
handle: *handle,
55+
data: std::ptr::null_mut(),
56+
result: 0,
57+
size: 0,
58+
});
59+
60+
let raw_pointer = Box::into_raw(context);
61+
62+
let ct_lib = Library::new("ClientTelemetry.dll").unwrap();
63+
let evt_api_call_default: evt_api_call_t =
64+
unsafe { ct_lib.get_proc("evt_api_call_default").unwrap() };
65+
66+
let status = evt_api_call_default(raw_pointer);
67+
68+
println!("UPLOAD STATUS >> {}", status);
69+
70+
status
71+
}
72+
73+
pub fn evt_flush(handle: &evt_handle_t) -> evt_status_t {
74+
let context: Box<evt_context_t> = Box::new(evt_context_t {
75+
call: evt_call_t_EVT_OP_FLUSH,
76+
handle: *handle,
77+
data: std::ptr::null_mut(),
78+
result: 0,
79+
size: 0,
80+
});
81+
82+
let raw_pointer = Box::into_raw(context);
83+
84+
let ct_lib = Library::new("ClientTelemetry.dll").unwrap();
85+
let evt_api_call_default: evt_api_call_t =
86+
unsafe { ct_lib.get_proc("evt_api_call_default").unwrap() };
87+
88+
let status = evt_api_call_default(raw_pointer);
89+
90+
println!("FLUSH STATUS >> {}", status);
91+
92+
status
93+
}
94+
95+
pub fn evt_log_vec(handle: &evt_handle_t, data_box: &mut Box<Vec<evt_prop>>) -> evt_status_t {
96+
// Extract the inner vector from the Box
97+
let data = &mut **data_box;
98+
99+
let val = unsafe { CStr::from_ptr(data[0].name) };
100+
let val2 = unsafe { CStr::from_ptr(data[0].value.as_string) };
101+
println!(
102+
"MAGIC >> [{}]:{}",
103+
val.to_str().unwrap(),
104+
val2.to_str().unwrap()
105+
);
106+
107+
let data_pointer = data.as_mut_ptr() as *mut c_void;
108+
let data_len = data.len() as u32;
109+
110+
let context: Box<evt_context_t> = Box::new(evt_context_t {
111+
call: evt_call_t_EVT_OP_LOG,
112+
handle: *handle,
113+
data: data_pointer,
114+
result: 0,
115+
size: data_len,
116+
});
117+
118+
let raw_pointer = Box::into_raw(context);
119+
120+
let ct_lib = Library::new("ClientTelemetry.dll").unwrap();
121+
let evt_api_call_default: evt_api_call_t =
122+
unsafe { ct_lib.get_proc("evt_api_call_default").unwrap() };
123+
124+
let status = evt_api_call_default(raw_pointer);
125+
126+
status
127+
}
6128

7129
pub fn evt_log(handle: &evt_handle_t, data: &mut [evt_prop]) -> evt_status_t {
8130
let data_len = data.len() as u32;
@@ -18,7 +140,7 @@ pub fn evt_log(handle: &evt_handle_t, data: &mut [evt_prop]) -> evt_status_t {
18140

19141
let raw_pointer = Box::into_raw(context);
20142

21-
let ct_lib = client_library::Library::new("ClientTelemetry.dll").unwrap();
143+
let ct_lib = Library::new("ClientTelemetry.dll").unwrap();
22144
let evt_api_call_default: evt_api_call_t =
23145
unsafe { ct_lib.get_proc("evt_api_call_default").unwrap() };
24146

wrappers/rust/oneds-telemetry/src/lib.rs

Lines changed: 50 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,24 @@
22
#![allow(non_camel_case_types)]
33
#![allow(non_snake_case)]
44

5-
use chrono;
6-
use internals::evt_api_call_t;
75
use log::debug;
86
use once_cell::sync::Lazy;
9-
use std::{
10-
ffi::{c_void, CString},
11-
sync::RwLock,
12-
};
7+
use std::{ffi::c_void, sync::RwLock};
8+
use types::{StringProperty, TelemetryItem, TelemetryProperty};
139

1410
pub mod appender;
1511
mod client_library;
1612
mod helpers;
1713
mod internals;
14+
pub mod types;
1815

1916
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
2017

2118
#[derive(Clone)]
2219
pub struct LogManager {
23-
call_api: evt_api_call_t,
2420
is_started: bool,
25-
configuration: Option<String>,
26-
log_handle: Option<Box<i64>>,
21+
configuration: StringProperty,
22+
log_handle: Option<i64>,
2723
}
2824

2925
/// The global `Tracer` provider singleton.
@@ -52,18 +48,22 @@ pub fn flush() {
5248
.write()
5349
.expect("GLOBAL_LOG_MANAGER RwLock poisoned");
5450

55-
log_manager.flush();
51+
log_manager.flush(true);
52+
}
53+
54+
pub fn offline_flush() {
55+
let mut log_manager = GLOBAL_LOG_MANAGER
56+
.write()
57+
.expect("GLOBAL_LOG_MANAGER RwLock poisoned");
58+
59+
log_manager.flush(false);
5660
}
5761

5862
impl LogManager {
5963
fn new() -> Self {
60-
let ct_lib = client_library::Library::new("ClientTelemetry.dll").unwrap();
61-
let call_api: evt_api_call_t = unsafe { ct_lib.get_proc("evt_api_call_default").unwrap() };
62-
6364
LogManager {
64-
call_api: call_api,
6565
is_started: false,
66-
configuration: Option::None,
66+
configuration: StringProperty::new("{}"),
6767
log_handle: Option::None,
6868
}
6969
}
@@ -72,147 +72,86 @@ impl LogManager {
7272
* Validates that a configuration has been set and
7373
*/
7474
pub fn start(&mut self) -> bool {
75-
if self.configuration.is_none() {
76-
return false;
77-
}
78-
7975
if self.is_started {
8076
return true;
8177
}
8278

83-
// We don't need to leak this value
84-
let config = self.configuration.clone().unwrap();
85-
let c_str = Box::new(CString::new(config.as_str()).unwrap());
86-
let config_data = c_str.as_ptr() as *mut c_void;
87-
88-
let mut context: Box<evt_context_t> = Box::new(evt_context_t {
89-
call: evt_call_t_EVT_OP_OPEN,
90-
handle: 0,
91-
data: config_data,
92-
result: 0,
93-
size: 0,
94-
});
79+
let config_ptr = self.configuration.as_ptr() as *mut c_void;
80+
let handle = internals::evt_open(config_ptr);
9581

96-
let raw_pointer = Box::into_raw(context);
97-
98-
let result = (self.call_api)(raw_pointer);
99-
100-
if result == -1 {
82+
if handle.is_none() {
10183
debug!("LogManager.start: failed");
10284
return false;
10385
}
10486

105-
context = unsafe { Box::from_raw(raw_pointer) };
106-
self.log_handle = Some(Box::new(context.handle));
87+
self.log_handle = handle;
10788
self.is_started = true;
10889

10990
debug!("LogManager.start: success");
11091

11192
return true;
11293
}
11394

114-
pub fn flush(&mut self) {
95+
pub fn flush(&mut self, upload: bool) {
11596
if self.is_started == false {
11697
return;
11798
}
11899

119-
let mut c_chars: Vec<i8> = Vec::new();
120-
c_chars.push(0); // null
121-
let null_config_data = c_chars.as_mut_ptr() as *mut c_void;
122100
let handle = self.log_handle.as_ref().unwrap();
123-
124-
let flush_ctx: Box<evt_context_t> = Box::new(evt_context_t {
125-
call: evt_call_t_EVT_OP_FLUSH,
126-
handle: *handle.as_ref(),
127-
data: null_config_data,
128-
result: 0,
129-
size: 0,
130-
});
131-
132-
let flush_ctx_ptr = Box::into_raw(flush_ctx);
133-
134-
let _ = (self.call_api)(flush_ctx_ptr);
101+
internals::evt_flush(handle);
135102
debug!("LogManager.flush(EVT_OP_FLUSH)");
136103

137-
let upload_ctx: Box<evt_context_t> = Box::new(evt_context_t {
138-
call: evt_call_t_EVT_OP_UPLOAD,
139-
handle: *handle.as_ref(),
140-
data: null_config_data,
141-
result: 0,
142-
size: 0,
143-
});
144-
145-
let upload_ctx_ptr = Box::into_raw(upload_ctx);
146-
147-
let _ = (self.call_api)(upload_ctx_ptr);
148-
debug!("LogManager.flush(EVT_OP_UPLOAD)");
104+
if upload {
105+
internals::evt_upload(handle);
106+
debug!("LogManager.flush(EVT_OP_UPLOAD)");
107+
}
149108
}
150109

151110
pub fn stop(&mut self) {
152111
if self.is_started == false {
153112
return;
154113
}
155114

156-
let mut c_chars: Vec<i8> = Vec::new();
157-
c_chars.push(0); // null
158-
let null_config_data = c_chars.as_mut_ptr() as *mut c_void;
159-
let handle = self.log_handle.as_ref().unwrap();
160-
161-
let flush_ctx: Box<evt_context_t> = Box::new(evt_context_t {
162-
call: evt_call_t_EVT_OP_FLUSH,
163-
handle: *handle.as_ref(),
164-
data: null_config_data,
165-
result: 0,
166-
size: 0,
167-
});
115+
self.flush(false);
168116

169-
let flush_ctx_ptr = Box::into_raw(flush_ctx);
170-
171-
let _ = (self.call_api)(flush_ctx_ptr);
172-
173-
let close_ctx: Box<evt_context_t> = Box::new(evt_context_t {
174-
call: evt_call_t_EVT_OP_CLOSE,
175-
handle: *handle.as_ref(),
176-
data: null_config_data,
177-
result: 0,
178-
size: 0,
179-
});
180-
181-
let close_ctx_ptr = Box::into_raw(close_ctx);
182-
183-
let _ = (self.call_api)(close_ctx_ptr);
117+
let handle = self.log_handle.as_ref().unwrap();
118+
internals::evt_close(handle);
184119
}
185120

186121
pub fn configure(&mut self, config: &str) {
187-
self.configuration = Some(String::from(config.clone()));
122+
self.configuration = StringProperty::new(config);
188123
debug!("LogManager.configure:\n{}", config);
189124
}
190125

191-
pub fn log_event(&self, name: &str) {
192-
let evt_time = chrono::Utc::now();
126+
pub fn trace(&mut self, message: &str) {
127+
let mut item = TelemetryItem::new("trace");
193128

194-
let mut event_props: [evt_prop; 2] = [
195-
helpers::string_prop("name", name, false),
196-
helpers::int_prop("time", evt_time.timestamp_millis()),
197-
];
129+
item.add_property(
130+
"message",
131+
TelemetryProperty::String(StringProperty::new(message)),
132+
);
198133

199-
self.log(&mut event_props);
134+
self.track_item(&item);
200135
}
201136

202-
pub fn trace(&self, message: &str) {
203-
let evt_time = chrono::Utc::now();
137+
pub fn track_item(&mut self, item: &TelemetryItem) {
138+
let mut event_props = item.to_evt();
204139

205-
let mut event_props: [evt_prop; 3] = [
206-
helpers::string_prop("name", "trace", false),
207-
helpers::int_prop("time", evt_time.timestamp_millis()),
208-
helpers::string_prop("message", message, false),
209-
];
140+
debug!(
141+
"LogManager.track_item(event_props_len:{})",
142+
event_props.len()
143+
);
210144

211-
self.log(&mut event_props);
145+
let handle = self.log_handle.clone().unwrap();
146+
internals::evt_log_vec(&handle, &mut event_props);
212147
}
213148

214-
pub fn log(&self, mut event_props: &mut [evt_prop]) {
215-
debug!("LogManager.log(event_props_len:{})", event_props.len());
149+
pub fn track_evt(&self, mut event_props: &mut [evt_prop]) {
150+
debug!(
151+
"LogManager.track_evt(event_props_len:{})",
152+
event_props.len()
153+
);
154+
216155
let handle = self.log_handle.clone().unwrap();
217156
internals::evt_log(&handle, &mut event_props);
218157
}

0 commit comments

Comments
 (0)