Skip to main content

cvlr_log/
core.rs

1pub mod rt_decls {
2    #[allow(improper_ctypes)]
3    extern "C" {
4        pub fn CVT_calltrace_print_tag(tag: &str);
5
6        pub fn CVT_calltrace_print_u64_1(tag: &str, x: u64);
7        pub fn CVT_calltrace_print_u64_2(tag: &str, x: u64, y: u64);
8        pub fn CVT_calltrace_print_u64_3(tag: &str, x: u64, y: u64, z: u64);
9        pub fn CVT_calltrace_print_u128(tag: &str, x: u128);
10
11        pub fn CVT_calltrace_print_i64_1(tag: &str, x: i64);
12        pub fn CVT_calltrace_print_i64_2(tag: &str, x: i64, y: i64);
13        pub fn CVT_calltrace_print_i64_3(tag: &str, x: i64, y: i64, z: i64);
14        pub fn CVT_calltrace_print_i128(tag: &str, x: i128);
15
16        pub fn CVT_calltrace_print_string(tag: &str, v: &str);
17
18        pub fn CVT_calltrace_print_u64_as_fixed(tag: &str, x: u64, y: u64);
19        pub fn CVT_calltrace_print_u64_as_decimal(tag: &str, x: u64, y: u64);
20
21        pub fn CVT_calltrace_print_location(file: &str, line: u64);
22        pub fn CVT_calltrace_attach_location(file: &str, line: u64);
23
24        pub fn CVT_rule_location(file: &str, line: u64);
25
26        pub fn CVT_calltrace_scope_start(name: &str);
27        pub fn CVT_calltrace_scope_end(name: &str);
28    }
29}
30
31#[cfg(feature = "rt")]
32#[allow(improper_ctypes_definitions)]
33mod rt_impls {
34    #[no_mangle]
35    pub extern "C" fn CVT_calltrace_print_tag(_tag: &str) {}
36    #[no_mangle]
37    pub extern "C" fn CVT_calltrace_print_u64_1(_tag: &str, _x: u64) {}
38    #[no_mangle]
39    pub extern "C" fn CVT_calltrace_print_u64_2(_tag: &str, _x: u64, _y: u64) {}
40    #[no_mangle]
41    pub extern "C" fn CVT_calltrace_print_u64_3(_tag: &str, _x: u64, _y: u64, _z: u64) {}
42    #[no_mangle]
43    pub extern "C" fn CVT_calltrace_print_u128(_tag: &str, _x: u128) {}
44    #[no_mangle]
45    pub extern "C" fn CVT_calltrace_print_i64_1(_tag: &str, _x: i64) {}
46    #[no_mangle]
47    pub extern "C" fn CVT_calltrace_print_i64_2(_tag: &str, _x: i64, _y: i64) {}
48    #[no_mangle]
49    pub extern "C" fn CVT_calltrace_print_i64_3(_tag: &str, _x: i64, _y: i64, _z: i64) {}
50    #[no_mangle]
51    pub extern "C" fn CVT_calltrace_print_i128(_tag: &str, _x: i128) {}
52    #[no_mangle]
53    pub extern "C" fn CVT_calltrace_print_u64_as_fixed(_tag: &str, _x: u64, _y: u64) {}
54    #[no_mangle]
55    pub extern "C" fn CVT_calltrace_print_u64_as_decimal(_tag: &str, _x: u64, _y: u64) {}
56    #[no_mangle]
57    pub extern "C" fn CVT_calltrace_print_string(_tag: &str, _v: &str) {}
58    #[no_mangle]
59    pub extern "C" fn CVT_calltrace_print_location(_file: &str, _line: u64) {}
60    #[no_mangle]
61    pub extern "C" fn CVT_calltrace_attach_location(_file: &str, _line: u64) {}
62    #[no_mangle]
63    pub extern "C" fn CVT_rule_location(_file: &str, _line: u64) {}
64    #[no_mangle]
65    pub extern "C" fn CVT_calltrace_scope_start(_name: &str) {}
66    #[no_mangle]
67    pub extern "C" fn CVT_calltrace_scope_end(_name: &str) {}
68}
69pub use rt_decls::*;
70
71#[derive(Default)]
72pub struct CvlrLogger;
73
74impl CvlrLogger {
75    #[inline(always)]
76    pub fn new() -> Self {
77        Self {}
78    }
79    #[inline(always)]
80    pub fn log(&mut self, v: &str) {
81        unsafe {
82            CVT_calltrace_print_tag(v);
83        }
84    }
85
86    #[inline(always)]
87    pub fn log_str(&mut self, t: &str, v: &str) {
88        unsafe {
89            CVT_calltrace_print_string(t, v);
90        }
91    }
92
93    #[inline(always)]
94    pub fn log_u64(&mut self, t: &str, v: u64) {
95        unsafe {
96            CVT_calltrace_print_u64_1(t, v);
97        }
98    }
99
100    #[inline(always)]
101    pub fn log_u64_2(&mut self, t: &str, v0: u64, v1: u64) {
102        unsafe {
103            CVT_calltrace_print_u64_2(t, v0, v1);
104        }
105    }
106
107    #[inline(always)]
108    pub fn log_u64_3(&mut self, t: &str, v0: u64, v1: u64, v2: u64) {
109        unsafe {
110            CVT_calltrace_print_u64_3(t, v0, v1, v2);
111        }
112    }
113
114    #[inline(always)]
115    pub fn log_u128(&mut self, t: &str, v: u128) {
116        unsafe {
117            CVT_calltrace_print_u128(t, v);
118        }
119    }
120
121    #[inline(always)]
122    pub fn log_i64(&mut self, t: &str, v: i64) {
123        unsafe {
124            CVT_calltrace_print_i64_1(t, v);
125        }
126    }
127
128    #[inline(always)]
129    pub fn log_i128(&mut self, t: &str, v: i128) {
130        unsafe {
131            CVT_calltrace_print_i128(t, v);
132        }
133    }
134
135    #[inline(always)]
136    pub fn log_u64_as_fp(&mut self, t: &str, v: u64, b: u64) {
137        unsafe {
138            CVT_calltrace_print_u64_as_fixed(t, v, b);
139        }
140    }
141
142    #[inline(always)]
143    pub fn log_u64_as_dec(&mut self, t: &str, v: u64, d: u64) {
144        unsafe {
145            CVT_calltrace_print_u64_as_decimal(t, v, d);
146        }
147    }
148
149    #[inline(always)]
150    pub fn log_loc(&mut self, file: &str, line: u32) {
151        unsafe {
152            CVT_calltrace_print_location(file, line as u64);
153        }
154    }
155
156    #[inline(always)]
157    pub fn add_loc(&mut self, file: &str, line: u32) {
158        unsafe {
159            CVT_calltrace_attach_location(file, line as u64);
160        }
161    }
162
163    #[inline(always)]
164    pub fn log_rule_location(&mut self, file: &str, line: u64) {
165        unsafe {
166            crate::CVT_rule_location(file, line);
167        }
168    }
169
170    #[inline(always)]
171    pub fn log_scope_start(&mut self, scope: &str) {
172        unsafe {
173            CVT_calltrace_scope_start(scope);
174        }
175    }
176
177    #[inline(always)]
178    pub fn log_scope_end(&mut self, scope: &str) {
179        unsafe {
180            CVT_calltrace_scope_end(scope);
181        }
182    }
183}
184
185#[inline(always)]
186pub fn log(v: &str) {
187    let mut logger = CvlrLogger::new();
188    logger.log(v);
189}
190
191#[inline(always)]
192pub fn log_u64_as_fp(t: &str, v: u64, b: u64) {
193    let mut logger = CvlrLogger::new();
194    logger.log_u64_as_fp(t, v, b);
195}
196
197#[inline(always)]
198pub fn log_u64_as_dec(t: &str, v: u64, d: u64) {
199    let mut logger = CvlrLogger::new();
200    logger.log_u64_as_dec(t, v, d);
201}
202
203#[inline(always)]
204pub fn log_rule_location(file: &str, line: u64) {
205    let mut logger = CvlrLogger::new();
206    logger.log_rule_location(file, line);
207}
208
209#[inline(always)]
210pub fn log_scope_start(scope: &str) {
211    let mut logger = CvlrLogger::new();
212    logger.log_scope_start(scope);
213}
214
215#[inline(always)]
216pub fn log_scope_end(scope: &str) {
217    let mut logger = CvlrLogger::new();
218    logger.log_scope_end(scope);
219}
220
221macro_rules! expose_log_fn {
222    ($name: ident, $ty: ty) => {
223        #[inline(always)]
224        pub fn $name(t: &str, v: $ty) {
225            let mut logger = CvlrLogger::new();
226            logger.$name(t, v)
227        }
228    };
229}
230
231expose_log_fn! {log_str, &str}
232expose_log_fn! {log_u64, u64}
233expose_log_fn! {log_i64, i64}
234expose_log_fn! {log_u128, u128}
235expose_log_fn! {log_i128, i128}
236expose_log_fn! {log_loc, u32}
237expose_log_fn! {add_loc, u32}
238
239#[macro_export]
240macro_rules! cvlr_rule_location {
241    () => {{
242        $crate::log_rule_location(std::file!(), std::line!() as u64)
243    }};
244}