xref: /aosp_15_r20/external/pigweed/pw_rust/examples/basic_executable/bin/hello_pw_log/main.rs (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #![no_main]
16 #![cfg_attr(not(feature = "std"), no_std)]
17 #![warn(clippy::all)]
18 
19 #[path = "../../other.rs"]
20 mod other;
21 
22 extern "C" {
23     // This external function links to the pw_log C backend specified by the
24     // target. It will be replaced by pw_log rust crate once we have third party
25     // support for GN.
pw_Log( level: i32, flags: u32, module_name: *const i8, file_name: *const i8, line_number: i32, function_name: *const i8, message: *const i8, ... )26     pub fn pw_Log(
27         level: i32,
28         flags: u32,
29         module_name: *const i8,
30         file_name: *const i8,
31         line_number: i32,
32         function_name: *const i8,
33         message: *const i8,
34         ...
35     );
36 }
37 
38 const PW_LOG_LEVEL_INFO: i32 = 2;
39 
40 macro_rules! pw_log_info {
41   ($($args:expr),*) => (
42       unsafe {
43         pw_Log(
44           PW_LOG_LEVEL_INFO,
45           0,
46           "\0".as_ptr() as *const i8,
47           "\0".as_ptr() as *const i8,
48           i32::try_from(line!()).unwrap(),
49           "main\0".as_ptr() as *const i8,
50           $($args),*);
51       }
52   );
53 }
54 
55 #[no_mangle]
main() -> isize56 pub extern "C" fn main() -> isize {
57     pw_log_info!("Hello, Pigweed!\0".as_ptr() as *const i8);
58 
59     // ensure we can run code from other modules in the main crate
60     pw_log_info!("%d\0".as_ptr() as *const i8, other::foo());
61 
62     // ensure we can run code from dependent libraries
63     pw_log_info!(
64         "%d\0".as_ptr() as *const i8,
65         a::RequiredA::default().required_b.value
66     );
67     pw_log_info!("%d\0".as_ptr() as *const i8, c::value());
68 
69     pw_log_info!(
70       "%d\0".as_ptr() as *const i8,
71       proc_macro::fn_like_proc_macro!(123)
72     );
73     return 0;
74 }
75 
76 #[cfg(not(feature = "std"))]
77 #[panic_handler]
panic(_info: &core::panic::PanicInfo) -> !78 fn panic(_info: &core::panic::PanicInfo) -> ! {
79     // Abort only
80     loop {}
81 }
82