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 //! utils
16 #![allow(missing_docs)]
17
18 use crate::config::{SHELL_PROG, WIN_CMD_PROG};
19
20 use std::future::Future;
21 use std::io::{self, Error, ErrorKind};
22 use std::process::Command;
23 use std::time::{SystemTime, UNIX_EPOCH};
24
25 #[cfg(feature = "host")]
26 extern crate ylong_runtime_static as ylong_runtime;
27 use ylong_runtime::io::AsyncWriteExt;
28
get_pseudo_random_u32null29 pub fn get_pseudo_random_u32() -> u32 {
30 (SystemTime::now()
31 .duration_since(UNIX_EPOCH)
32 .unwrap()
33 .as_nanos()
34 & 0xffffffff) as u32
35 }
36
get_current_timenull37 pub fn get_current_time() -> u64 {
38 SystemTime::now()
39 .duration_since(UNIX_EPOCH)
40 .unwrap()
41 .as_millis() as u64
42 }
43
44 pub async fn print_msg(buf: Vec<u8>) -> io::Result<()> {
45 let mut stdout = ylong_runtime::io::stdout();
46 let _ = stdout.write(&buf).await;
47 let _ = stdout.flush().await;
48 Ok(())
49 }
50
execute_cmdnull51 pub fn execute_cmd(cmd: String) -> Result<std::process::Output, Error> {
52 let arg_sign = if cfg!(target_os = "windows") {
53 "/c"
54 } else {
55 "-c"
56 };
57
58 let programe = if cfg!(target_os = "windows") {
59 WIN_CMD_PROG
60 } else {
61 SHELL_PROG
62 };
63
64 Command::new(programe).args([arg_sign, &cmd]).output()
65 }
66
error_othernull67 pub fn error_other(msg: String) -> Error {
68 Error::new(ErrorKind::Other, msg)
69 }
70
bytes_to_stringnull71 pub fn bytes_to_string(message: Vec<u8>) -> String {
72 let msg = String::from_utf8(message);
73 match msg {
74 Ok(str) => str,
75 Err(_e) => "".to_string(),
76 }
77 }
78
79 #[allow(missing_docs)]
spawnnull80 pub fn spawn<T>(fun : T) -> ylong_runtime::task::JoinHandle<()>
81 where
82 T : Future<Output = ()>,
83 T : Send + 'static,
84 T : Sync
85 {
86 ylong_runtime::spawn(Box::into_pin(Box::new(fun) as Box<dyn Future<Output = ()> + Send + Sync>))
87 }
88
89 pub mod hdc_log {
90 #[cfg(not(feature = "host"))]
91 pub use hilog_rust::{hilog, HiLogLabel, LogType};
92 #[cfg(not(feature = "host"))]
93 pub use std::ffi::{c_char, CString};
94
95 #[cfg(not(feature = "host"))]
96 pub const LOG_LABEL: HiLogLabel = HiLogLabel {
97 log_type: LogType::LogCore,
98 domain: 0xD002D13,
99 tag: "HDC_LOG",
100 };
101
102 #[cfg(not(feature = "host"))]
103 #[inline(never)]
get_file_namenull104 pub fn get_file_name(path: &str) -> &str {
105 path.split('/').last().unwrap_or(path)
106 }
107
108 #[cfg(not(feature = "host"))]
109 #[macro_export]
110 macro_rules! trace {
111 ($($arg:tt)+) => {
112 hilog_rust::debug!(LOG_LABEL, "{}:{} {}", @public(get_file_name(file!())), @public(line!()), @public(format!($($arg)+)));
113 log::trace!($($arg)+);
114 };
115 }
116
117 #[cfg(feature = "host")]
118 #[macro_export]
119 macro_rules! trace {
120 ($($arg:tt)+) => {
121 log::trace!($($arg)+);
122 };
123 }
124
125 #[cfg(not(feature = "host"))]
126 #[macro_export]
127 macro_rules! debug {
128 ($($arg:tt)+) => {
129 hilog_rust::debug!(LOG_LABEL, "{}:{} {}", @public(get_file_name(file!())), @public(line!()), @public(format!($($arg)+)));
130 log::debug!($($arg)+);
131 };
132 }
133
134 #[cfg(feature = "host")]
135 #[macro_export]
136 macro_rules! debug {
137 ($($arg:tt)+) => {
138 log::debug!($($arg)+);
139 };
140 }
141
142 #[cfg(not(feature = "host"))]
143 #[macro_export]
144 macro_rules! info {
145 ($($arg:tt)+) => {
146 hilog_rust::info!(LOG_LABEL, "{}:{} {}", @public(get_file_name(file!())), @public(line!()), @public(format!($($arg)+)));
147 log::info!($($arg)+);
148 };
149 }
150
151 #[cfg(feature = "host")]
152 #[macro_export]
153 macro_rules! info {
154 ($($arg:tt)+) => {
155 log::info!($($arg)+);
156 };
157 }
158
159 #[cfg(not(feature = "host"))]
160 #[macro_export]
161 macro_rules! warn {
162 ($($arg:tt)+) => {
163 hilog_rust::warn!(LOG_LABEL, "{}:{} {}", @public(get_file_name(file!())), @public(line!()), @public(format!($($arg)+)));
164 log::warn!($($arg)+);
165 };
166 }
167
168 #[cfg(feature = "host")]
169 #[macro_export]
170 macro_rules! warn {
171 ($($arg:tt)+) => {
172 log::warn!($($arg)+);
173 };
174 }
175
176 #[cfg(not(feature = "host"))]
177 #[macro_export]
178 macro_rules! error {
179 ($($arg:tt)+) => {
180 hilog_rust::error!(LOG_LABEL, "{}:{} {}", @public(get_file_name(file!())), @public(line!()), @public(format!($($arg)+)));
181 log::error!($($arg)+);
182 };
183 }
184
185 #[cfg(feature = "host")]
186 #[macro_export]
187 macro_rules! error {
188 ($($arg:tt)+) => {
189 log::error!($($arg)+);
190 };
191 }
192
193 #[cfg(not(feature = "host"))]
194 #[macro_export]
195 macro_rules! fatal {
196 ($($arg:tt)+) => {
197 hilog_rust::fatal!(LOG_LABEL, "{}:{} {}", @public(get_file_name(file!())), @public(line!()), @public(format!($($arg)+)));
198 log::fatal!($($arg)+);
199 };
200 }
201
202 #[cfg(feature = "host")]
203 #[macro_export]
204 macro_rules! fatal {
205 ($($arg:tt)+) => {
206 log::fatal!($($arg)+);
207 };
208 }
209
210 // pub use crate::{trace, debug, info, warn, error, fatal};
211 }
212