1 // Copyright (C) 2024 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 use std::ffi::c_char;
15
16 pub use ffi::{LogLevel, LogType};
17
18 #[cxx::bridge]
19 mod ffi {
20 #[repr(i32)]
21 enum LogType {
22 // min log type
23 LOG_TYPE_MIN = 0,
24 // Used by app log.
25 LOG_APP = 0,
26 // Log to kmsg, only used by init phase.
27 LOG_INIT = 1,
28 // Used by core service, framework.
29 LOG_CORE = 3,
30 // Used by kmsg log.
31 LOG_KMSG = 4,
32 // Not print in release version.
33 LOG_ONLY_PRERELEASE = 5,
34 // max log type
35 LOG_TYPE_MAX,
36 }
37
38 // Log level
39 #[repr(i32)]
40 enum LogLevel {
41 // min log level
42 LOG_LEVEL_MIN = 0,
43 // Designates lower priority log.
44 LOG_DEBUG = 3,
45 // Designates useful information.
46 LOG_INFO = 4,
47 // Designates hazardous situations.
48 LOG_WARN = 5,
49 // Designates very serious errors.
50 LOG_ERROR = 6,
51 // Designates major fatal anomaly.
52 LOG_FATAL = 7,
53 // max log level
54 LOG_LEVEL_MAX,
55 }
56
57 unsafe extern "C++" {
58 include!("hilog/log.h");
59
60 type LogType;
61 type LogLevel;
62 }
63 }
64
hilog_print(level: LogLevel, domain: u32, tag: &str, mut fmt: String)65 pub fn hilog_print(level: LogLevel, domain: u32, tag: &str, mut fmt: String) {
66 let tag = tag.as_ptr() as *const c_char;
67 fmt.push('\0');
68 unsafe {
69 HiLogPrint(
70 LogType::LOG_CORE,
71 level,
72 domain,
73 tag,
74 fmt.as_ptr() as *const c_char,
75 );
76 }
77 }
78
79 extern "C" {
HiLogPrint( log_type: ffi::LogType, level: ffi::LogLevel, domain: u32, tag: *const c_char, fmt: *const c_char, ... ) -> i3280 fn HiLogPrint(
81 log_type: ffi::LogType,
82 level: ffi::LogLevel,
83 domain: u32,
84 tag: *const c_char,
85 fmt: *const c_char,
86 ...
87 ) -> i32;
88 }
89