From 35a0b0a4d69f0722ac9099cc0c7e965f7e97e03d Mon Sep 17 00:00:00 2001 From: chenchong_666 Date: Wed, 8 Feb 2023 09:53:23 +0800 Subject: [PATCH] Modify ipc log printing Signed-off-by: chenchong_666 --- interfaces/innerkits/rust/BUILD.gn | 12 +- interfaces/innerkits/rust/src/ashmem.rs | 11 +- interfaces/innerkits/rust/src/ipc/macros.rs | 2 - .../src/ipc/remote_obj/death_recipient.rs | 10 +- .../innerkits/rust/src/ipc/remote_stub.rs | 11 +- .../rust/src/parcel/types/file_desc.rs | 12 +- .../rust/src/parcel/types/interface_token.rs | 12 +- .../rust/src/parcel/types/string16.rs | 12 +- .../rust/src/parcel/types/strings.rs | 17 ++- interfaces/innerkits/rust/src/process.rs | 12 +- ipc/native/src/c_wrapper/source/c_ashmem.cpp | 15 ++- ipc/native/src/c_wrapper/source/c_parcel.cpp | 119 +++++++++--------- ipc/native/src/c_wrapper/source/c_process.cpp | 5 +- .../src/c_wrapper/source/c_remote_object.cpp | 33 ++--- 14 files changed, 178 insertions(+), 105 deletions(-) diff --git a/interfaces/innerkits/rust/BUILD.gn b/interfaces/innerkits/rust/BUILD.gn index 7227ba3a..e2f2b1d3 100644 --- a/interfaces/innerkits/rust/BUILD.gn +++ b/interfaces/innerkits/rust/BUILD.gn @@ -9,7 +9,7 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and -# limitations under the License. +# limitations under the License. import("//build/ohos.gni") @@ -30,7 +30,10 @@ ohos_rust_shared_library("ipc_rust") { "src/parcel/types.rs", ] - deps = [ ":ipc_c" ] + deps = [ + ":ipc_c", + "//base/hiviewdfx/hilog/interfaces/rust:hilog_rust", + ] crate_name = "ipc_rust" crate_type = "dylib" @@ -46,7 +49,10 @@ config("libipc_c_private_config") { } ohos_shared_library("ipc_c") { - include_dirs = [ "$IPC_CORE_ROOT/src/c_wrapper/include" ] + include_dirs = [ + "$IPC_CORE_ROOT/src/c_wrapper/include", + "$SUBSYSTEM_DIR/utils/include", + ] sources = [ "$IPC_CORE_ROOT/src/c_wrapper/source/c_ashmem.cpp", "$IPC_CORE_ROOT/src/c_wrapper/source/c_parcel.cpp", diff --git a/interfaces/innerkits/rust/src/ashmem.rs b/interfaces/innerkits/rust/src/ashmem.rs index b6d831ae..9742a51b 100644 --- a/interfaces/innerkits/rust/src/ashmem.rs +++ b/interfaces/innerkits/rust/src/ashmem.rs @@ -18,8 +18,15 @@ use crate::{ AsRawPtr }; use crate::ipc_binding::CAshmem; -use std::ffi::CString; +use std::ffi::{CString, c_char}; use crate::parcel::parcelable::{Serialize, Deserialize}; +use hilog_rust::{error, hilog, HiLogLabel, LogType}; + +const LOG_LABEL: HiLogLabel = HiLogLabel { + log_type: LogType::LogCore, + domain: 0xd001510, + tag: "RustAshmem" +}; /// Ashmem packed the native CAshmem #[repr(C)] @@ -138,7 +145,7 @@ impl Ashmem { pub fn write(&self, data: &[u8], offset: i32) -> bool { let len = data.len() as i32; if offset < 0 || offset >= len { - println!("invalid offset: {}, len: {}", offset, len); + error!(LOG_LABEL, "invalid offset: {}, len: {}", offset, len); return false; } unsafe { diff --git a/interfaces/innerkits/rust/src/ipc/macros.rs b/interfaces/innerkits/rust/src/ipc/macros.rs index 095a10ce..e950b5da 100644 --- a/interfaces/innerkits/rust/src/ipc/macros.rs +++ b/interfaces/innerkits/rust/src/ipc/macros.rs @@ -92,8 +92,6 @@ macro_rules! define_remote_object { match result { Ok(_) => 0, Err(error) => { - println!("stub: {} deal fail: {} for code: {}", $descriptor, - error, code); error } } diff --git a/interfaces/innerkits/rust/src/ipc/remote_obj/death_recipient.rs b/interfaces/innerkits/rust/src/ipc/remote_obj/death_recipient.rs index 52362e88..afbb3078 100644 --- a/interfaces/innerkits/rust/src/ipc/remote_obj/death_recipient.rs +++ b/interfaces/innerkits/rust/src/ipc/remote_obj/death_recipient.rs @@ -14,6 +14,14 @@ */ use super::*; +use std::ffi::{CString, c_char}; +use hilog_rust::{info, hilog, HiLogLabel, LogType}; + +const LOG_LABEL: HiLogLabel = HiLogLabel { + log_type: LogType::LogCore, + domain: 0xd001510, + tag: "RustDeathRecipient" +}; /// This type represent a rust DeathRecipient which like C++ DethRecipient. #[repr(C)] @@ -70,7 +78,7 @@ impl DeathRecipient { F: Fn() + Send + Sync + 'static, { if !callback.is_null() { - println!("death recipient on destroy"); + info!(LOG_LABEL, "death recipient on destroy"); drop(Box::from_raw(callback as *mut F)); } } diff --git a/interfaces/innerkits/rust/src/ipc/remote_stub.rs b/interfaces/innerkits/rust/src/ipc/remote_stub.rs index 9cc21215..96a1587a 100644 --- a/interfaces/innerkits/rust/src/ipc/remote_stub.rs +++ b/interfaces/innerkits/rust/src/ipc/remote_stub.rs @@ -15,8 +15,15 @@ use crate::{ipc_binding, IRemoteStub, IRemoteBroker, RemoteObj, BorrowedMsgParcel, }; use crate::ipc_binding::{CRemoteObject, CParcel}; -use std::ffi::{c_void, CString}; +use std::ffi::{c_void, CString, c_char}; use std::ops::{Deref}; +use hilog_rust::{info, hilog, HiLogLabel, LogType}; + +const LOG_LABEL: HiLogLabel = HiLogLabel { + log_type: LogType::LogCore, + domain: 0xd001510, + tag: "RustRemoteStub" +}; /// RemoteStub packed the native CRemoteObject and the rust stub object T /// which must implement IRemoteStub trait. @@ -94,7 +101,7 @@ impl RemoteStub { } unsafe extern "C" fn on_destroy(user_data: *mut c_void) { - println!("RemoteStub on_destroy in Rust"); + info!(LOG_LABEL, "RemoteStub on_destroy in Rust"); // T will be freed by Box after this function end. drop(Box::from_raw(user_data as *mut T)); } diff --git a/interfaces/innerkits/rust/src/parcel/types/file_desc.rs b/interfaces/innerkits/rust/src/parcel/types/file_desc.rs index c497fc9d..49e3c977 100644 --- a/interfaces/innerkits/rust/src/parcel/types/file_desc.rs +++ b/interfaces/innerkits/rust/src/parcel/types/file_desc.rs @@ -18,6 +18,14 @@ use crate::{ipc_binding, BorrowedMsgParcel, AsRawPtr, result_status, Result}; use std::fs::File; use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; +use std::ffi::{CString}; +use hilog_rust::{error, hilog, HiLogLabel, LogType}; + +const LOG_LABEL: HiLogLabel = HiLogLabel { + log_type: LogType::LogCore, + domain: 0xd001510, + tag: "RustFileDesc" +}; /// Rust version of the Java class android.os.ParcelFileDescriptor #[derive(Debug)] @@ -108,7 +116,7 @@ impl DeOption for FileDesc { }; if ok_status{ if fd < 0 { - println!("file descriptor is invalid from native"); + error!(LOG_LABEL, "file descriptor is invalid from native"); Err(-1) } else { let file = unsafe { @@ -120,7 +128,7 @@ impl DeOption for FileDesc { Ok(Some(FileDesc::new(file))) } } else { - println!("read file descriptor failed from native"); + error!(LOG_LABEL, "read file descriptor failed from native"); Err(-1) } } diff --git a/interfaces/innerkits/rust/src/parcel/types/interface_token.rs b/interfaces/innerkits/rust/src/parcel/types/interface_token.rs index 50694c0b..b44c1ba4 100644 --- a/interfaces/innerkits/rust/src/parcel/types/interface_token.rs +++ b/interfaces/innerkits/rust/src/parcel/types/interface_token.rs @@ -16,6 +16,14 @@ use super::*; use crate::{ipc_binding, BorrowedMsgParcel, Result, AsRawPtr, result_status}; use std::convert::TryInto; +use std::ffi::{CString, c_char}; +use hilog_rust::{error, hilog, HiLogLabel, LogType}; + +const LOG_LABEL: HiLogLabel = HiLogLabel { + log_type: LogType::LogCore, + domain: 0xd001510, + tag: "RustInterfaceToken" +}; /// InterfaceToken packed a String type which transfered with C++ std::u16string. pub struct InterfaceToken(String); @@ -68,11 +76,11 @@ impl Deserialize for InterfaceToken { if let Some(val) = result { Ok(Self(val)) } else { - println!("convert interface token to String fail"); + error!(LOG_LABEL, "convert interface token to String fail"); Err(-1) } }else{ - println!("read interface token from native fail"); + error!(LOG_LABEL, "read interface token from native fail"); Err(-1) } } diff --git a/interfaces/innerkits/rust/src/parcel/types/string16.rs b/interfaces/innerkits/rust/src/parcel/types/string16.rs index 906a3c72..fac2c713 100644 --- a/interfaces/innerkits/rust/src/parcel/types/string16.rs +++ b/interfaces/innerkits/rust/src/parcel/types/string16.rs @@ -16,6 +16,14 @@ use super::*; use crate::{ipc_binding, BorrowedMsgParcel, Result, result_status, AsRawPtr}; use std::convert::TryInto; +use std::ffi::{CString}; +use hilog_rust::{error, hilog, HiLogLabel, LogType}; + +const LOG_LABEL: HiLogLabel = HiLogLabel { + log_type: LogType::LogCore, + domain: 0xd001510, + tag: "RustString16" +}; /// String16 packed a String type which transfered with C++ std::u16string. pub struct String16(String); @@ -68,11 +76,11 @@ impl Deserialize for String16 { if let Some(val) = result { Ok(Self(val)) } else { - println!("convert native string16 to String fail"); + error!(LOG_LABEL, "convert native string16 to String fail"); Err(-1) } } else { - println!("read string16 from native fail"); + error!(LOG_LABEL, "read string16 from native fail"); Err(-1) } } diff --git a/interfaces/innerkits/rust/src/parcel/types/strings.rs b/interfaces/innerkits/rust/src/parcel/types/strings.rs index 4ba2d44e..9d844ce9 100644 --- a/interfaces/innerkits/rust/src/parcel/types/strings.rs +++ b/interfaces/innerkits/rust/src/parcel/types/strings.rs @@ -20,6 +20,14 @@ use crate::{ }; use std::convert::TryInto; use std::mem::MaybeUninit; +use std::ffi::{CString}; +use hilog_rust::{error, hilog, HiLogLabel, LogType}; + +const LOG_LABEL: HiLogLabel = HiLogLabel { + log_type: LogType::LogCore, + domain: 0xd001510, + tag: "RustString" +}; impl SerOption for str {} impl SerOption for String {} @@ -89,7 +97,6 @@ impl SerArray for String { on_string_writer, ) }; - println!("write String array result: {}", ret); result_status::<()>(ret, ()) } } @@ -116,7 +123,7 @@ impl DeArray for String { }; Ok(vec) } else { - println!("read string from native fail"); + error!(LOG_LABEL, "read string from native fail"); Err(-1) } } @@ -213,11 +220,11 @@ unsafe extern "C" fn on_string_reader( if let Some(new_vec) = vec { new_vec.push(MaybeUninit::new(string)); } else { - println!("on_string_reader allocate vec failed"); + error!(LOG_LABEL, "on_string_reader allocate vec failed"); return false; } } else { - println!("on_string_reader vec_to_string failed"); + error!(LOG_LABEL, "on_string_reader vec_to_string failed"); return false; } } else { @@ -236,7 +243,7 @@ fn vec_to_string(vec: Option>) -> Result { if let Some(ret) = value { ret } else { - println!("convert vector u8 to String fail"); + error!(LOG_LABEL, "convert vector u8 to String fail"); Err(-1) } } diff --git a/interfaces/innerkits/rust/src/process.rs b/interfaces/innerkits/rust/src/process.rs index 2fb15745..a7689f83 100644 --- a/interfaces/innerkits/rust/src/process.rs +++ b/interfaces/innerkits/rust/src/process.rs @@ -17,6 +17,14 @@ use crate::{ ipc_binding, MsgParcel, RemoteObj, IRemoteObj, InterfaceToken, String16, Result, }; +use std::ffi::{CString, c_char}; +use hilog_rust::{info, hilog, HiLogLabel, LogType}; + +const LOG_LABEL: HiLogLabel = HiLogLabel { + log_type: LogType::LogCore, + domain: 0xd001510, + tag: "RustProcess" +}; /// Get proxy object of samgr pub fn get_context_object() -> Option @@ -41,7 +49,7 @@ pub fn add_service(service: &RemoteObj, said: i32) -> Result<()> data.write(&String16::new(""))?; let reply = samgr.send_request(3, &data, false)?; let reply_value: i32 = reply.read()?; - println!("register service result: {}", reply_value); + info!(LOG_LABEL, "register service result: {}", reply_value); if reply_value == 0 { Ok(())} else { Err(reply_value) } } @@ -54,7 +62,7 @@ pub fn get_service(said: i32) -> Result data.write(&said)?; let reply = samgr.send_request(2, &data, false)?; let remote: RemoteObj = reply.read()?; - println!("get service success"); + info!(LOG_LABEL, "get service success"); Ok(remote) } diff --git a/ipc/native/src/c_wrapper/source/c_ashmem.cpp b/ipc/native/src/c_wrapper/source/c_ashmem.cpp index 81e028bc..9d910142 100644 --- a/ipc/native/src/c_wrapper/source/c_ashmem.cpp +++ b/ipc/native/src/c_wrapper/source/c_ashmem.cpp @@ -15,17 +15,20 @@ #include "c_ashmem.h" #include "c_ashmem_internal.h" +#include "log_tags.h" +#include "ipc_debug.h" using namespace OHOS; +static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "CAshmem" }; static bool IsValidCAshmem(const CAshmem *ashmem, const char *promot) { if (ashmem == nullptr) { - printf("%s: cashmem is null\n", promot); + ZLOGE(LOG_LABEL, "%{public}s: cashmem is null\n", promot); return false; } if (ashmem->ashmem_ == nullptr) { - printf("%s: wrapper ashmem is null\n", promot); + ZLOGE(LOG_LABEL, "%{public}s: wrapper ashmem is null\n", promot); return false; } return true; @@ -43,12 +46,12 @@ CAshmem *CreateCAshmem(const char *name, int32_t size) { sptr ashmem = Ashmem::CreateAshmem(name, size); if (ashmem == nullptr) { - printf("%s: create native ashmem failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: create native ashmem failed\n", __func__); return nullptr; } CAshmem *cashmem = new (std::nothrow) CAshmem(ashmem); if (cashmem == nullptr) { - printf("%s: create cashmem failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: create cashmem failed\n", __func__); ashmem->CloseAshmem(); return nullptr; } @@ -59,7 +62,7 @@ CAshmem *CreateCAshmem(const char *name, int32_t size) void CAshmemIncStrongRef(CAshmem *ashmem) { if (ashmem == nullptr) { - printf("%s: ashmem is nullptr\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: ashmem is nullptr\n", __func__); return; } ashmem->IncStrongRef(nullptr); @@ -68,7 +71,7 @@ void CAshmemIncStrongRef(CAshmem *ashmem) void CAshmemDecStrongRef(CAshmem *ashmem) { if (ashmem == nullptr) { - printf("%s: ashmem is nullptr\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: ashmem is nullptr\n", __func__); return; } ashmem->DecStrongRef(nullptr); diff --git a/ipc/native/src/c_wrapper/source/c_parcel.cpp b/ipc/native/src/c_wrapper/source/c_parcel.cpp index c3cc677e..d95eed6d 100644 --- a/ipc/native/src/c_wrapper/source/c_parcel.cpp +++ b/ipc/native/src/c_wrapper/source/c_parcel.cpp @@ -18,8 +18,11 @@ #include #include #include "c_remote_object_internal.h" +#include "log_tags.h" +#include "ipc_debug.h" using namespace OHOS; +static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "CParcel" }; MessageParcelHolder::MessageParcelHolder(OHOS::MessageParcel *parcel) : isExternal_(true) @@ -42,11 +45,11 @@ MessageParcelHolder::~MessageParcelHolder(void) static bool IsValidParcel(const CParcel *parcel, const char *promot) { if (parcel == nullptr) { - printf("%s: parcel is null\n", promot); + ZLOGE(LOG_LABEL, "%{public}s: parcel is null\n", promot); return false; } if (parcel->parcel_ == nullptr) { - printf("%s: wrapper parcel is null\n", promot); + ZLOGE(LOG_LABEL, "%{public}s: wrapper parcel is null\n", promot); return false; } return true; @@ -58,11 +61,11 @@ static bool WriteAndCheckArrayLength(CParcel *parcel, bool isNull, int32_t len) return false; } if (!isNull && len < 0) { - printf("%s: not null array has invalid length: %d\n", __func__, len); + ZLOGE(LOG_LABEL, "%{public}s: not null array has invalid length: %d\n", __func__, len); return false; } if (isNull && len > 0) { - printf("%s: null array has invalid length: %d\n", __func__, len); + ZLOGE(LOG_LABEL, "%{public}s: null array has invalid length: %d\n", __func__, len); return false; } return parcel->parcel_->WriteInt32(len); @@ -71,18 +74,18 @@ static bool WriteAndCheckArrayLength(CParcel *parcel, bool isNull, int32_t len) static bool ReadAndCheckArrayLength(const CParcel *parcel, int32_t &len) { if (!parcel->parcel_->ReadInt32(len)) { - printf("%s: read array length from native parcel failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: read array length from native parcel failed\n", __func__); return false; } if (len < -1) { - printf("%s: length is invalid: %d\n", __func__, len); + ZLOGE(LOG_LABEL, "%{public}s: length is invalid: %d\n", __func__, len); return false; } if (len <= 0) { // null array return true; } if (static_cast(len) > parcel->parcel_->GetReadableBytes()) { - printf("%s: readable bytes are too short in parcel: %d\n", __func__, len); + ZLOGE(LOG_LABEL, "%{public}s: readable bytes are too short in parcel: %d\n", __func__, len); return false; } return true; @@ -92,7 +95,7 @@ CParcel *CParcelObtain(void) { CParcel *holder = new (std::nothrow) MessageParcelHolder(); if (holder == nullptr) { - printf("%s: malloc messsage parcel holder failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: malloc messsage parcel holder failed\n", __func__); return nullptr; } holder->IncStrongRef(nullptr); @@ -102,7 +105,7 @@ CParcel *CParcelObtain(void) void CParcelIncStrongRef(CParcel *parcel) { if (parcel == nullptr) { - printf("%s: parcel is nullptr\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: parcel is nullptr\n", __func__); return; } parcel->IncStrongRef(nullptr); @@ -111,7 +114,7 @@ void CParcelIncStrongRef(CParcel *parcel) void CParcelDecStrongRef(CParcel *parcel) { if (parcel == nullptr) { - printf("%s: parcel is nullptr\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: parcel is nullptr\n", __func__); return; } parcel->DecStrongRef(nullptr); @@ -236,14 +239,14 @@ bool CParcelWriteString(CParcel *parcel, const char *stringData, int32_t length) } if (stringData == nullptr) { if (length != -1) { - printf("%s: stringData is null, len: %d\n", __func__, length); + ZLOGE(LOG_LABEL, "%{public}s: stringData is null, len: %d\n", __func__, length); return false; } std::string value; return parcel->parcel_->WriteString(value); } if (length < 0) { - printf("%s: stringData len is invalid: %d\n", __func__, length); + ZLOGE(LOG_LABEL, "%{public}s: stringData len is invalid: %d\n", __func__, length); return false; } std::string value(stringData, length); @@ -257,17 +260,17 @@ bool CParcelReadString(const CParcel *parcel, void *stringData, OnCParcelBytesAl } std::string value; if (!parcel->parcel_->ReadString(value)) { - printf("%s: read string from parcel failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: read string from parcel failed\n", __func__); return false; } char *buffer = nullptr; bool isSuccess = allocator(stringData, &buffer, value.length()); if (!isSuccess) { - printf("%s: allocate string buffer is null\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: allocate string buffer is null\n", __func__); return false; } if (value.length() > 0 && memcpy_s(buffer, value.length(), value.data(), value.length()) != EOK) { - printf("%s: memcpy string failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: memcpy string failed\n", __func__); return false; } return true; @@ -280,19 +283,19 @@ bool CParcelWriteString16(CParcel *parcel, const char *str, int32_t strLen) } if (str == nullptr) { if (strLen != -1) { - printf("%s: str is null, len: %d\n", __func__, strLen); + ZLOGE(LOG_LABEL, "%{public}s: str is null, len: %d\n", __func__, strLen); return false; } std::u16string value; return parcel->parcel_->WriteString16(value); } if (strLen < 0) { - printf("%s: str len is invalid: %d\n", __func__, strLen); + ZLOGE(LOG_LABEL, "%{public}s: str len is invalid: %d\n", __func__, strLen); return false; } std::u16string u16string = Str8ToStr16(std::string(str, strLen)); if (u16string.length() == 0 && strLen != 0) { - printf("%s: convert u16string failed: %d\n", __func__, strLen); + ZLOGE(LOG_LABEL, "%{public}s: convert u16string failed: %d\n", __func__, strLen); return false; } return parcel->parcel_->WriteString16(u16string); @@ -305,23 +308,23 @@ bool CParcelReadString16(const CParcel *parcel, void *stringData, OnCParcelBytes } std::u16string u16string; if (!parcel->parcel_->ReadString16(u16string)) { - printf("%s: read u16string from parcel failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: read u16string from parcel failed\n", __func__); return false; } std::string value = Str16ToStr8(u16string); if (u16string.length() != 0 && value.length() == 0) { - printf("%s: u16string len: %u, string len: %u\n", __func__, + ZLOGE(LOG_LABEL, "%{public}s: u16string len: %u, string len: %u\n", __func__, static_cast(u16string.length()), static_cast(value.length())); return false; } char *buffer = nullptr; bool isSuccess = allocator(stringData, &buffer, value.length()); if (!isSuccess) { - printf("%s: allocate string buffer is null\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: allocate string buffer is null\n", __func__); return false; } if (value.length() > 0 && memcpy_s(buffer, value.length(), value.data(), value.length()) != EOK) { - printf("%s: memcpy string16 failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: memcpy string16 failed\n", __func__); return false; } return true; @@ -333,12 +336,12 @@ bool CParcelWriteInterfaceToken(CParcel *parcel, const char *token, int32_t toke return false; } if (token == nullptr || tokenLen < 0) { - printf("%s: token len is invalid: %d\n", __func__, tokenLen); + ZLOGE(LOG_LABEL, "%{public}s: token len is invalid: %d\n", __func__, tokenLen); return false; } std::u16string u16string = Str8ToStr16(std::string(token, tokenLen)); if (u16string.length() == 0 && tokenLen != 0) { - printf("%s: convert token to u16string failed: %d\n", __func__, tokenLen); + ZLOGE(LOG_LABEL, "%{public}s: convert token to u16string failed: %d\n", __func__, tokenLen); return false; } return parcel->parcel_->WriteInterfaceToken(u16string); @@ -352,18 +355,18 @@ bool CParcelReadInterfaceToken(const CParcel *parcel, void *token, OnCParcelByte std::u16string u16string = parcel->parcel_->ReadInterfaceToken(); std::string value = Str16ToStr8(u16string); if (u16string.length() != 0 && value.length() == 0) { - printf("%s: u16string len: %u, string len: %u\n", __func__, + ZLOGE(LOG_LABEL, "%{public}s: u16string len: %u, string len: %u\n", __func__, static_cast(u16string.length()), static_cast(value.length())); return false; } char *buffer = nullptr; bool isSuccess = allocator(token, &buffer, value.length()); if (!isSuccess) { - printf("%s: allocate interface token buffer failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: allocate interface token buffer failed\n", __func__); return false; } if (value.length() > 0 && memcpy_s(buffer, value.length(), value.data(), value.length()) != EOK) { - printf("%s: memcpy interface token failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: memcpy interface token failed\n", __func__); return false; } return true; @@ -384,13 +387,13 @@ CRemoteObject *CParcelReadRemoteObject(const CParcel *parcel) } sptr remote = parcel->parcel_->ReadRemoteObject(); if (remote == nullptr) { - printf("%s: read remote object is null\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: read remote object is null\n", __func__); return nullptr; } CRemoteObject *holder = nullptr; holder = new (std::nothrow) CRemoteObjectHolder(); if (holder == nullptr) { - printf("%s: create remote object holder failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: craete remote object holder failed\n", __func__); return nullptr; } holder->remote_ = remote; @@ -412,7 +415,7 @@ bool CParcelReadFileDescriptor(const CParcel *parcel, int32_t *fd) return false; } if (fd == nullptr) { - printf("%s: fd is null\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: fd is null\n", __func__); return false; } *fd = parcel->parcel_->ReadFileDescriptor(); @@ -425,7 +428,7 @@ bool CParcelWriteBuffer(CParcel *parcel, const uint8_t *buffer, uint32_t len) return false; } if (buffer == nullptr) { - printf("%s: buffer is null: %d\n", __func__, len); + ZLOGE(LOG_LABEL, "%{public}s: buffer is null: %d\n", __func__, len); return false; } return parcel->parcel_->WriteBuffer(buffer, len); @@ -438,11 +441,11 @@ bool CParcelReadBuffer(const CParcel *parcel, uint8_t *value, uint32_t len) } const uint8_t *data = parcel->parcel_->ReadBuffer(len); if (data == nullptr) { - printf("%s: read buffer failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: read buffer failed\n", __func__); return false; } if (len > 0 && memcpy_s(value, len, data, len) != EOK) { - printf("%s: copy buffer failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: copy buffer failed\n", __func__); return false; } return true; @@ -463,7 +466,7 @@ const uint8_t *CParcelReadRawData(const CParcel *parcel, uint32_t len) } const void *data = parcel->parcel_->ReadRawData(len); if (data == nullptr) { - printf("%s: read raw data from native failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: read raw data from native failed\n", __func__); return nullptr; } return reinterpret_cast(data); @@ -490,18 +493,18 @@ static bool ReadVector(const CParcel *parcel, const char *func, void *value, } std::vector array; if (!(parcel->parcel_->*Read)(&array)) { - printf("%s: read type vector from native failed\n", func); + ZLOGE(LOG_LABEL, "%{public}s: read type vector from native failed\n", func); return false; } T *buffer = nullptr; bool isSuccess = OnCParcelTypeAllocator(value, &buffer, array.size()); if (!isSuccess) { - printf("%s: allocate type array buffer failed\n", func); + ZLOGE(LOG_LABEL, "%{public}s: allocate type array buffer failed\n", func); return false; } int32_t len = array.size() * sizeof(T); if (array.size() > 0 && memcpy_s(buffer, len, array.data(), len) != EOK) { - printf("%s: memcpy type buffer failed\n", func); + ZLOGE(LOG_LABEL, "%{public}s: memcpy type buffer failed\n", func); return false; } return true; @@ -519,13 +522,13 @@ bool CParcelReadBoolArray(const CParcel *parcel, void *value, OnCParcelBoolAlloc } std::vector array; if (!parcel->parcel_->ReadBoolVector(&array)) { - printf("%s: read bool vector from native failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: read bool vector from native failed\n", __func__); return false; } bool *buffer = nullptr; bool isSuccess = allocator(value, &buffer, array.size()); if (!isSuccess) { - printf("%s: allocate bool array buffer failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: allocate bool array buffer failed\n", __func__); return false; } if (array.size() > 0) { @@ -605,11 +608,11 @@ bool CParcelWriteStringArray(CParcel *parcel, const void *value, std::vector stringVector; if (len > 0 && !writer(reinterpret_cast(&stringVector), value, static_cast(len))) { - printf("%s: write string array to vector failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: write string array to vector failed\n", __func__); return false; } if (!parcel->parcel_->WriteStringVector(stringVector)) { - printf("%s: write string array to parcel failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: write string array to parcel failed\n", __func__); return false; } return true; @@ -619,11 +622,11 @@ bool CParcelWriteStringElement(void *data, const char *value, int32_t len) { std::vector *stringVector = reinterpret_cast *>(data); if (stringVector == nullptr) { - printf("%s: stringVector is null\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: stringVector is null\n", __func__); return false; } if (len < 0) { - printf("%s: string len is invalid: %d\n", __func__, len); + ZLOGE(LOG_LABEL, "%{public}s: string len is invalid: %d\n", __func__, len); return false; } stringVector->push_back(std::string(value, len)); @@ -637,46 +640,42 @@ bool CParcelReadStringArray(const CParcel *parcel, void *value, OnStringArrayRea } std::vector stringVector; if (!parcel->parcel_->ReadStringVector(&stringVector)) { - printf("%s: read string array from parcel failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: read string array from parcel failed\n", __func__); return false; } - printf("%s: read string array len: %u\n", __func__, - static_cast(stringVector.size())); if (!reader(reinterpret_cast(&stringVector), value, stringVector.size())) { - printf("%s: read string to vector failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: read string to vector failed\n", __func__); return false; } - printf("%s: read string array success\n", __func__); return true; } bool CParcelReadStringElement(uint32_t index, const void *data, void *value, OnCParcelBytesAllocator allocator) { - printf("%s: enter\n", __func__); if (data == nullptr || allocator == nullptr) { - printf("%s: invalid data and allocator\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: invalid data and allocator\n", __func__); return false; } const std::vector *stringVector = reinterpret_cast *>(data); if (index >= stringVector->size()) { - printf("%s: invalid index: %u, size: %u\n", __func__, + ZLOGE(LOG_LABEL, "%{public}s: invalid index: %u, size: %u\n", __func__, index, static_cast(stringVector->size())); return false; } - printf("%s: index: %u\n", __func__, index); + const std::string &stringValue = (*stringVector)[index]; char *buffer = nullptr; bool isSuccess = allocator(value, &buffer, stringValue.length()); if (!isSuccess) { - printf("%s: allocate string buffer failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: allocate string buffer failed\n", __func__); return false; } - printf("%s: read string element: %s\n", __func__, stringValue.c_str()); + if (stringValue.length() > 0 && memcpy_s(buffer, stringValue.length(), stringValue.data(), stringValue.length()) != EOK) { - printf("%s: memcpy string failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: memcpy string failed\n", __func__); return false; } return true; @@ -694,7 +693,7 @@ bool CParcelWriteParcelableArray(CParcel *parcel, const void *value, int32_t len } for (int32_t i = 0; i < len; ++i) { if (!elementWriter(parcel, value, static_cast(i))) { - printf("%s: write parcelable for index: %d failed\n", __func__, i); + ZLOGE(LOG_LABEL, "%{public}s: write parcelable for index: %d failed\n", __func__, i); parcel->parcel_->RewindWrite(pos); return false; } @@ -714,13 +713,13 @@ bool CParcelReadParcelableArray(const CParcel *parcel, void *value, return false; } if (!allocator(value, length)) { - printf("%s: allocator failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: allocator failed\n", __func__); return false; } // length == -1 means null array, and will return true for (int32_t i = 0; i < length; ++i) { if (!elementReader(parcel, value, static_cast(i))) { - printf("%s: read parcelable for index: %d failed\n", __func__, i); + ZLOGE(LOG_LABEL, "%{public}s: read parcelable for index: %d failed\n", __func__, i); parcel->parcel_->RewindRead(pos); return false; } @@ -839,12 +838,12 @@ CAshmem *CParcelReadAshmem(const CParcel *parcel) } sptr ashmem = parcel->parcel_->ReadAshmem(); if (ashmem == nullptr) { - printf("%s: read ashmem failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: read ashmem failed\n", __func__); return nullptr; } CAshmem *cashmem = new (std::nothrow) CAshmem(ashmem); if (cashmem == nullptr) { - printf("%s: new ashmem failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: new ashmem failed\n", __func__); return nullptr; } ashmem->IncStrongRef(nullptr); diff --git a/ipc/native/src/c_wrapper/source/c_process.cpp b/ipc/native/src/c_wrapper/source/c_process.cpp index 318fa8a4..576f67b5 100644 --- a/ipc/native/src/c_wrapper/source/c_process.cpp +++ b/ipc/native/src/c_wrapper/source/c_process.cpp @@ -16,9 +16,12 @@ #include "c_process.h" #include "c_remote_object_internal.h" +#include "log_tags.h" +#include "ipc_debug.h" #include "ipc_skeleton.h" using namespace OHOS; +static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "CProcess" }; CRemoteObject *GetContextManager(void) { @@ -28,7 +31,7 @@ CRemoteObject *GetContextManager(void) } CRemoteObject *holder = new (std::nothrow) CRemoteObjectHolder(); if (holder == nullptr) { - printf("%s: create samgr proxy holder failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: create samgr proxy holder failed\n", __func__); return nullptr; } holder->IncStrongRef(nullptr); diff --git a/ipc/native/src/c_wrapper/source/c_remote_object.cpp b/ipc/native/src/c_wrapper/source/c_remote_object.cpp index 7429acc5..2e88139e 100644 --- a/ipc/native/src/c_wrapper/source/c_remote_object.cpp +++ b/ipc/native/src/c_wrapper/source/c_remote_object.cpp @@ -18,8 +18,11 @@ #include #include "c_parcel_internal.h" #include "c_remote_object_internal.h" +#include "log_tags.h" +#include "ipc_debug.h" using namespace OHOS; +static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "CRemoteObject" }; RemoteServiceHolderStub::RemoteServiceHolderStub(std::u16string &desc, OnRemoteRequestCb callback, const void *userData, OnRemoteObjectDestroyCb destroy) @@ -41,7 +44,7 @@ int RemoteServiceHolderStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel { (void)option; if (callback_ == nullptr) { - printf("%s: callback is null for code: %u\n", __func__, code); + ZLOGE(LOG_LABEL, "%{public}s: callback is null for code: %u\n", __func__, code); return -1; } CParcel parcelData(&data); @@ -77,11 +80,11 @@ void CDeathRecipient::OnRemoteDied(const wptr &object) bool IsValidRemoteObject(const CRemoteObject *object, const char *promot) { if (object == nullptr) { - printf("[%s] RemoteObject is null\n", promot); + ZLOGE(LOG_LABEL, "[%{public}s] RemoteObject is null\n", promot); return false; } if (object->remote_ == nullptr) { - printf("[%s]wrapper RemoteObject is null\n", promot); + ZLOGE(LOG_LABEL, "[%{public}s]wrapper RemoteObject is null\n", promot); return false; } return true; @@ -95,14 +98,14 @@ CRemoteObject *CreateRemoteStub(const char *desc, OnRemoteRequestCb callback, } auto holder = new (std::nothrow) CRemoteObjectHolder(); if (holder == nullptr) { - printf("%s: new CRemoteObjectHolder failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: new CRemoteObjectHolder failed\n", __func__); return nullptr; } std::u16string descriptor = Str8ToStr16(std::string(desc)); holder->remote_ = new (std::nothrow) RemoteServiceHolderStub( descriptor, callback, userData, destroy); if (holder->remote_ == nullptr) { - printf("%s: new RemoteServiceHolderStub failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: new RemoteServiceHolderStub failed\n", __func__); delete holder; return nullptr; } @@ -113,7 +116,7 @@ CRemoteObject *CreateRemoteStub(const char *desc, OnRemoteRequestCb callback, void RemoteObjectIncStrongRef(CRemoteObject *object) { if (object == nullptr) { - printf("%s: unexpected CRemoteObject\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: unexpected CRemoteObject\n", __func__); return; } object->IncStrongRef(nullptr); @@ -122,7 +125,7 @@ void RemoteObjectIncStrongRef(CRemoteObject *object) void RemoteObjectDecStrongRef(CRemoteObject *object) { if (object == nullptr) { - printf("%s: unexpected CRemoteObject\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: unexpected CRemoteObject\n", __func__); return; } object->DecStrongRef(nullptr); @@ -140,7 +143,7 @@ int RemoteObjectSendRequest(const CRemoteObject *object, uint32_t code, const CParcel *data, CParcel *reply, bool isAsync) { if (!IsValidRemoteObject(object, __func__) || data == nullptr || reply == nullptr) { - printf("%s: object and data must be not null\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: object and data must be not null\n", __func__); return -EINVAL; } MessageOption option(isAsync ? MessageOption::TF_ASYNC : MessageOption::TF_SYNC); @@ -151,13 +154,13 @@ CDeathRecipient *CreateDeathRecipient(OnDeathRecipientCb onDeathRecipient, OnDeathRecipientDestroyCb onDestroy, const void *userData) { if (onDeathRecipient == nullptr || onDestroy == nullptr || userData == nullptr) { - printf("%s: args must not be null\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: args must not be null\n", __func__); return nullptr; } CDeathRecipient *recipient = new (std::nothrow) CDeathRecipient(onDeathRecipient, onDestroy, userData); if (recipient == nullptr) { - printf("%s: create CDeathRecipient object failed\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: create CDeathRecipient object failed\n", __func__); return nullptr; } recipient->IncStrongRef(nullptr); @@ -167,7 +170,7 @@ CDeathRecipient *CreateDeathRecipient(OnDeathRecipientCb onDeathRecipient, void DeathRecipientIncStrongRef(CDeathRecipient *recipient) { if (recipient == nullptr) { - printf("%s: unexpected CDeathRecipient\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: unexpected CDeathRecipient\n", __func__); return; } recipient->IncStrongRef(nullptr); @@ -176,7 +179,7 @@ void DeathRecipientIncStrongRef(CDeathRecipient *recipient) void DeathRecipientDecStrongRef(CDeathRecipient *recipient) { if (recipient == nullptr) { - printf("%s: unexpected CDeathRecipient\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: unexpected CDeathRecipient\n", __func__); return; } recipient->DecStrongRef(nullptr); @@ -188,7 +191,7 @@ bool AddDeathRecipient(CRemoteObject *object, CDeathRecipient *recipient) return false; } if (!object->remote_->IsProxyObject()) { - printf("%s: this is not a proxy object", __func__); + ZLOGE(LOG_LABEL, "%{public}s: this is not a proxy object", __func__); return false; } sptr callback(recipient); @@ -198,11 +201,11 @@ bool AddDeathRecipient(CRemoteObject *object, CDeathRecipient *recipient) bool RemoveDeathRecipient(CRemoteObject *object, CDeathRecipient *recipient) { if (!IsValidRemoteObject(object, __func__) || recipient == nullptr) { - printf("%s: recipient is null\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: recipient is null\n", __func__); return false; } if (!object->remote_->IsProxyObject()) { - printf("%s: this is not a proxy object\n", __func__); + ZLOGE(LOG_LABEL, "%{public}s: this is not a proxy object\n", __func__); return false; }; sptr callback(recipient);