• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::{GetCacheDir, LogLevel, LogType, SHA256};
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         include!("request_utils_wrapper.h");
60         include!("application_context.h");
61 
62         #[namespace = "OHOS::Request"]
GetCacheDir() -> String63         fn GetCacheDir() -> String;
64 
65         #[namespace = "OHOS::Request"]
SHA256(input: &str) -> String66         fn SHA256(input: &str) -> String;
67 
68         type LogType;
69         type LogLevel;
70     }
71 }
72 
hilog_print(level: LogLevel, domain: u32, tag: &str, mut fmt: String)73 pub fn hilog_print(level: LogLevel, domain: u32, tag: &str, mut fmt: String) {
74     let tag = tag.as_ptr() as *const c_char;
75     fmt.push('\0');
76     unsafe {
77         HiLogPrint(
78             LogType::LOG_CORE,
79             level,
80             domain,
81             tag,
82             fmt.as_ptr() as *const c_char,
83         );
84     }
85 }
86 
87 extern "C" {
HiLogPrint( log_type: ffi::LogType, level: ffi::LogLevel, domain: u32, tag: *const c_char, fmt: *const c_char, ... ) -> i3288     fn HiLogPrint(
89         log_type: ffi::LogType,
90         level: ffi::LogLevel,
91         domain: u32,
92         tag: *const c_char,
93         fmt: *const c_char,
94         ...
95     ) -> i32;
96 }
97