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