Skip to content

Commit c840986

Browse files
committed
misc updates - working on memory safe logging
1 parent b06eda6 commit c840986

File tree

3 files changed

+184
-7
lines changed

3 files changed

+184
-7
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ impl log::Log for TelemetryCollectorLogBridge {
1515

1616
fn log(&self, record: &Record) {
1717
if unsafe { CONSOLE_ENABLED } && self.enabled(record.metadata()) {
18-
let now = chrono::Utc::now();
18+
let utc_now = chrono::Utc::now();
19+
let nanos = utc_now.timestamp_subsec_nanos();
1920

2021
println!(
21-
"[{} {}] {} <{}> - {}: {}",
22-
now.date_naive().to_string(),
23-
now.time().format("%H:%M:%S"),
22+
"[{} {}.{}] {} <{}> - {}: {}",
23+
utc_now.date_naive().to_string(),
24+
utc_now.time().format("%H:%M:%S"),
25+
nanos,
2426
record.target(),
2527
record.module_path().unwrap(),
2628
record.level(),
@@ -31,16 +33,16 @@ impl log::Log for TelemetryCollectorLogBridge {
3133
if unsafe { COLLECTOR_ENABLED }
3234
// Default
3335
&& record.target() != "oneds_telemetry"
34-
// Manually Set
35-
&& record.target() != "oneds_telemetry_internal"
36+
// Items from deeper in the `oneds_telemetry` crate
37+
&& !record.target().starts_with("oneds_telemetry::")
3638
{
3739
log_manager_provider().trace(format!("{}", record.args()).as_str());
3840
}
3941
}
4042

4143
fn flush(&self) {
4244
if unsafe { COLLECTOR_ENABLED } {
43-
log_manager_provider().flush();
45+
log_manager_provider().flush(true);
4446
}
4547
}
4648
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::*;
2+
use std::ffi::{CStr, CString};
3+
use std::mem;
4+
use std::os::raw::c_char;
5+
6+
/**
7+
* This will leak memory
8+
*/
9+
pub fn to_leaky_c_str(value: &str) -> *const i8 {
10+
let c_str = Box::new(CString::new(value.clone()).unwrap());
11+
let ptr = c_str.as_ptr() as *const i8;
12+
mem::forget(c_str);
13+
return ptr;
14+
}
15+
16+
pub fn to_c_str(value: &str) -> *const c_char {
17+
let null_terminated_str = format!("{}\0", value);
18+
let c_str = CStr::from_bytes_with_nul(null_terminated_str.as_bytes())
19+
.expect("to_c_str: CString conversion failed");
20+
c_str.as_ptr()
21+
}
22+
23+
pub fn string_prop(name: &str, value: &str, is_pii: bool) -> evt_prop {
24+
let mut pii_kind = 0;
25+
26+
if is_pii {
27+
pii_kind = 1;
28+
}
29+
30+
evt_prop {
31+
name: to_leaky_c_str(name),
32+
type_: evt_prop_t_TYPE_STRING,
33+
value: evt_prop_v {
34+
as_string: to_leaky_c_str(value),
35+
},
36+
piiKind: pii_kind,
37+
}
38+
}
39+
40+
pub fn int_prop(name: &str, value: i64) -> evt_prop {
41+
evt_prop {
42+
name: to_c_str(name),
43+
type_: evt_prop_t_TYPE_INT64,
44+
value: evt_prop_v {
45+
as_int64: value.clone(),
46+
},
47+
piiKind: 0,
48+
}
49+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
use std::{
2+
collections::HashMap,
3+
ffi::{CStr, CString},
4+
os::raw::c_char,
5+
};
6+
7+
use chrono::{DateTime, Utc};
8+
use log::debug;
9+
use once_cell::sync::Lazy;
10+
11+
use crate::{evt_prop, evt_prop_t_TYPE_STRING, evt_prop_t_TYPE_TIME, evt_prop_v};
12+
13+
#[derive(Clone, Debug)]
14+
pub struct StringProperty {
15+
value: CString,
16+
}
17+
18+
impl StringProperty {
19+
pub fn new(value: &str) -> Self {
20+
StringProperty {
21+
value: CString::new(value).expect("Unable to convert value to CString"),
22+
}
23+
}
24+
25+
pub fn as_ptr(&self) -> *const c_char {
26+
self.value.as_ptr()
27+
}
28+
29+
fn to_str(&self) -> Result<&str, std::str::Utf8Error> {
30+
unsafe { CStr::from_ptr(self.as_ptr()).to_str() }
31+
}
32+
}
33+
34+
static NAME_PROPERTY: Lazy<StringProperty> = Lazy::new(|| StringProperty::new("name"));
35+
static TIME_PROPERTY: Lazy<StringProperty> = Lazy::new(|| StringProperty::new("time"));
36+
37+
#[derive(Clone, Debug)]
38+
pub enum TelemetryProperty {
39+
String(StringProperty),
40+
Int(i64),
41+
}
42+
43+
pub type TelemetryPropertyBag = HashMap<&'static str, TelemetryProperty>;
44+
45+
#[derive(Clone, Debug)]
46+
pub struct TelemetryItem {
47+
name: StringProperty,
48+
data: TelemetryPropertyBag,
49+
time: Option<DateTime<Utc>>,
50+
}
51+
52+
impl TelemetryItem {
53+
pub fn new(name: &'static str) -> TelemetryItem {
54+
TelemetryItem {
55+
name: StringProperty::new(name),
56+
data: TelemetryPropertyBag::new(),
57+
time: Option::None,
58+
}
59+
}
60+
61+
pub fn new_with_time(name: &'static str, time: DateTime<Utc>) -> TelemetryItem {
62+
TelemetryItem {
63+
name: StringProperty::new(name),
64+
data: TelemetryPropertyBag::new(),
65+
time: Some(time),
66+
}
67+
}
68+
69+
pub fn add_property(&mut self, name: &'static str, value: TelemetryProperty) {
70+
self.data.insert(name, value);
71+
}
72+
73+
pub fn to_evt(&self) -> Vec<evt_prop> {
74+
let mut properties = Vec::new();
75+
76+
properties.push(evt_prop {
77+
name: NAME_PROPERTY.as_ptr(),
78+
type_: evt_prop_t_TYPE_STRING,
79+
value: evt_prop_v {
80+
as_string: self.name.as_ptr(),
81+
},
82+
piiKind: 0,
83+
});
84+
85+
if self.time != Option::None {
86+
// Convert UTC time to a Unix timestamp as i64
87+
let timestamp_i64 = self.time.unwrap().timestamp();
88+
89+
// Convert the i64 timestamp to u64
90+
let millis = timestamp_i64 as u64;
91+
92+
properties.push(evt_prop {
93+
name: TIME_PROPERTY.as_ptr(),
94+
type_: evt_prop_t_TYPE_TIME,
95+
value: evt_prop_v { as_time: millis },
96+
piiKind: 0,
97+
});
98+
}
99+
100+
for prop in self.data.clone().into_iter() {}
101+
102+
debug!("{}", self.name.to_str().unwrap());
103+
104+
properties
105+
}
106+
}
107+
108+
#[cfg(test)]
109+
mod tests {
110+
use crate::types::StringProperty;
111+
112+
#[test]
113+
fn validate_c_str_arrays() {
114+
let mut iteration = 0;
115+
116+
while iteration < 5 {
117+
iteration = iteration + 1;
118+
119+
let value1 = StringProperty::new("TEST_VALUE1");
120+
let value2 = StringProperty::new("TEST_VALUE2");
121+
122+
assert_eq!("TEST_VALUE1", value1.to_str().unwrap());
123+
assert_eq!("TEST_VALUE2", value2.to_str().unwrap());
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)