2
2
#![ allow( non_camel_case_types) ]
3
3
#![ allow( non_snake_case) ]
4
4
5
- use chrono;
6
- use internals:: evt_api_call_t;
7
5
use log:: debug;
8
6
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 } ;
13
9
14
10
pub mod appender;
15
11
mod client_library;
16
12
mod helpers;
17
13
mod internals;
14
+ pub mod types;
18
15
19
16
include ! ( concat!( env!( "OUT_DIR" ) , "/bindings.rs" ) ) ;
20
17
21
18
#[ derive( Clone ) ]
22
19
pub struct LogManager {
23
- call_api : evt_api_call_t ,
24
20
is_started : bool ,
25
- configuration : Option < String > ,
26
- log_handle : Option < Box < i64 > > ,
21
+ configuration : StringProperty ,
22
+ log_handle : Option < i64 > ,
27
23
}
28
24
29
25
/// The global `Tracer` provider singleton.
@@ -52,18 +48,22 @@ pub fn flush() {
52
48
. write ( )
53
49
. expect ( "GLOBAL_LOG_MANAGER RwLock poisoned" ) ;
54
50
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 ) ;
56
60
}
57
61
58
62
impl LogManager {
59
63
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
-
63
64
LogManager {
64
- call_api : call_api,
65
65
is_started : false ,
66
- configuration : Option :: None ,
66
+ configuration : StringProperty :: new ( "{}" ) ,
67
67
log_handle : Option :: None ,
68
68
}
69
69
}
@@ -72,147 +72,86 @@ impl LogManager {
72
72
* Validates that a configuration has been set and
73
73
*/
74
74
pub fn start ( & mut self ) -> bool {
75
- if self . configuration . is_none ( ) {
76
- return false ;
77
- }
78
-
79
75
if self . is_started {
80
76
return true ;
81
77
}
82
78
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) ;
95
81
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 ( ) {
101
83
debug ! ( "LogManager.start: failed" ) ;
102
84
return false ;
103
85
}
104
86
105
- context = unsafe { Box :: from_raw ( raw_pointer) } ;
106
- self . log_handle = Some ( Box :: new ( context. handle ) ) ;
87
+ self . log_handle = handle;
107
88
self . is_started = true ;
108
89
109
90
debug ! ( "LogManager.start: success" ) ;
110
91
111
92
return true ;
112
93
}
113
94
114
- pub fn flush ( & mut self ) {
95
+ pub fn flush ( & mut self , upload : bool ) {
115
96
if self . is_started == false {
116
97
return ;
117
98
}
118
99
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 ;
122
100
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) ;
135
102
debug ! ( "LogManager.flush(EVT_OP_FLUSH)" ) ;
136
103
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
+ }
149
108
}
150
109
151
110
pub fn stop ( & mut self ) {
152
111
if self . is_started == false {
153
112
return ;
154
113
}
155
114
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 ) ;
168
116
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) ;
184
119
}
185
120
186
121
pub fn configure ( & mut self , config : & str ) {
187
- self . configuration = Some ( String :: from ( config. clone ( ) ) ) ;
122
+ self . configuration = StringProperty :: new ( config) ;
188
123
debug ! ( "LogManager.configure:\n {}" , config) ;
189
124
}
190
125
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" ) ;
193
128
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
+ ) ;
198
133
199
- self . log ( & mut event_props ) ;
134
+ self . track_item ( & item ) ;
200
135
}
201
136
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 ( ) ;
204
139
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
+ ) ;
210
144
211
- self . log ( & mut event_props) ;
145
+ let handle = self . log_handle . clone ( ) . unwrap ( ) ;
146
+ internals:: evt_log_vec ( & handle, & mut event_props) ;
212
147
}
213
148
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
+
216
155
let handle = self . log_handle . clone ( ) . unwrap ( ) ;
217
156
internals:: evt_log ( & handle, & mut event_props) ;
218
157
}
0 commit comments