1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /// macro wrapper for Updater::UpdaterLogger in updater
17 ///
18 /// # intro
19 /// support 5 level DEBUG, INFO, WARNING, ERROR, FATAL
20 ///
21 /// # usage:
22 /// ```
23 /// let hello = "hello".to_string();
24 /// updaterlog!(INFO, "this is a info log, {:?}", hello);
25 /// ```
26 ///
27 /// # note:
28 /// InitLogger / SetLevel is done in C++ code. if you need to
29 /// change log threshold, please add ffi interface in ffi.rs.
30 ///
31 #[macro_export]
32 macro_rules! updaterlog {
33     ($level:tt, $($arg:tt)* ) => (
34         let log_str = format!($($arg)*);
35         let file_name_str = match std::path::Path::new(file!()).file_name() {
36             Some(name_os_str) => { name_os_str.to_str() },
37             None => { None }
38         };
39         let file_name = file_name_str.unwrap_or("unknown");
40         let c_file_name = std::ffi::CString::new(file_name).expect("unknown");
41         let c_log_str = std::ffi::CString::new(log_str).expect("default log");
42         // can use CString::new(...).expect(...) because file_name and log_str can't have internal 0 byte
43         unsafe {
44             $crate::ffi::Logger(
45                 $crate::ffi::LogLevel::$level as i32,
46                 c_file_name.as_ptr() as *const std::ffi::c_char,
47                 line!() as i32,
48                 c_log_str.as_ptr() as *const std::ffi::c_char
49             )
50         };
51     )
52 }