diff --git a/include/hiperf_hilog.h b/include/hiperf_hilog.h index 6ef4f60eaadbf0ba0783c2c8b25fa7682f22c12a..d01f7d2aed1175b916fd2f508e7146cbb305e2fa 100644 --- a/include/hiperf_hilog.h +++ b/include/hiperf_hilog.h @@ -65,6 +65,14 @@ enum HiperfModule { MODULE_CPP_API, }; +enum HiperfLogLevel { + HIPERF_LOG_DEBUG, + HIPERF_LOG_INFO, + HIPERF_LOG_WARN, + HIPERF_LOG_ERROR, + HIPERF_LOG_FATAL +}; + static constexpr unsigned int BASE_HIPERF_DOMAIN_ID = 0xD002D0D; static constexpr OHOS::HiviewDFX::HiLogLabel HIPERF_HILOG_LABLE[] = { {LOG_CORE, BASE_HIPERF_DOMAIN_ID, "hiperf"}, @@ -126,17 +134,37 @@ static inline std::string StringFormat(const char* fmt, ...) do { \ if (!(expr)) { \ if (log == 1) { \ - std::string str = StringFormat(fmt, ##__VA_ARGS__); \ - HLOGE("%s", str.c_str()); \ + std::string log_str = StringFormat(fmt, ##__VA_ARGS__); \ + HLOGE("%s", log_str.c_str()); \ } else if (log == LOG_TYPE_PRINTF) { \ printf("%s", StringFormat(fmt, ##__VA_ARGS__).c_str()); \ } else if (log == LOG_TYPE_WITH_HILOG) { \ - std::string str = StringFormat(fmt, ##__VA_ARGS__); \ - HLOGE("%s", str.c_str()); \ - HIPERF_HILOGE(MODULE_DEFAULT, "%{public}s", str.c_str()); \ + std::string log_str = StringFormat(fmt, ##__VA_ARGS__); \ + HLOGE("%s", log_str.c_str()); \ + HIPERF_HILOGE(MODULE_DEFAULT, "%{public}s", log_str.c_str()); \ } \ return retval; \ } \ } while (0) +#define LOG_PRINT_ONLY(log_level, fmt, ...) \ + do { \ + std::string str = StringFormat(fmt, ##__VA_ARGS__); \ + if (log_level == HIPERF_LOG_DEBUG) { \ + HIPERF_HILOGD(MODULE_DEFAULT, "%{public}s", str.c_str()); \ + HLOGD("%s", str.c_str()); \ + } else if (log_level == HIPERF_LOG_INFO) { \ + HIPERF_HILOGI(MODULE_DEFAULT, "%{public}s", str.c_str()); \ + HLOGI("%s", str.c_str()); \ + } else if (log_level == HIPERF_LOG_WARN) { \ + HIPERF_HILOGW(MODULE_DEFAULT, "%{public}s", str.c_str()); \ + HLOGW("%s", str.c_str()); \ + } else if (log_level == HIPERF_LOG_ERROR) { \ + HIPERF_HILOGE(MODULE_DEFAULT, "%{public}s", str.c_str()); \ + HLOGE("%s", str.c_str()); \ + } else if (log_level == HIPERF_LOG_FATAL) { \ + HIPERF_HILOGF(MODULE_DEFAULT, "%{public}s", str.c_str()); \ + HLOGF("%s", str.c_str()); \ + } \ + } while (0) #endif // HIPERF_HILOG diff --git a/src/perf_pipe.cpp b/src/perf_pipe.cpp index 06b3e506e710aebe10e8ae9d78d2247522d48f91..2c8b38601f199638232bd0148f30ed9684746a48 100644 --- a/src/perf_pipe.cpp +++ b/src/perf_pipe.cpp @@ -49,9 +49,7 @@ void PerfPipe::SetFifoFileName(const CommandType& commandType, std::string& cont fifoFileC2S = fifoFileC2S_; fifoFileS2C = fifoFileS2C_; controlCmd_ = controlCmd; - HLOGD("C2S:%s, S2C:%s", fifoFileC2S.c_str(), fifoFileS2C.c_str()); - HIPERF_HILOGD(MODULE_DEFAULT, "[SetFifoFileName] C2S:%{public}s, S2C:%{public}s", - fifoFileC2S_.c_str(), fifoFileS2C.c_str()); + LOG_PRINT_ONLY(HIPERF_LOG_DEBUG, "[SetFifoFileName] C2S:%s, S2C:%s", fifoFileC2S.c_str(), fifoFileS2C.c_str()); } void PerfPipe::RemoveFifoFile() @@ -59,14 +57,12 @@ void PerfPipe::RemoveFifoFile() char errInfo[ERRINFOLEN] = { 0 }; if (remove(fifoFileC2S_.c_str()) != 0) { strerror_r(errno, errInfo, ERRINFOLEN); - HLOGE("remove %s failed, errno:(%d:%s)", fifoFileC2S_.c_str(), errno, errInfo); - HIPERF_HILOGE(MODULE_DEFAULT, "remove %{public}s failed, errno:(%{public}d:%{public}s)", + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[RemoveFifoFile] remove %s failed, errno:(%d:%s)", fifoFileC2S_.c_str(), errno, errInfo); } if (remove(fifoFileS2C_.c_str()) != 0) { strerror_r(errno, errInfo, ERRINFOLEN); - HLOGE("remove %s failed, errno:(%d:%s)", fifoFileS2C_.c_str(), errno, errInfo); - HIPERF_HILOGE(MODULE_DEFAULT, "remove %{public}s failed, errno:(%{public}d:%{public}s)", + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[RemoveFifoFile] remove %s failed, errno:(%d:%s)", fifoFileS2C_.c_str(), errno, errInfo); } } @@ -84,12 +80,12 @@ bool PerfPipe::CreateFifoFile() } if (mkfifo(fifoFileS2C_.c_str(), fifoMode) != 0 || mkfifo(fifoFileC2S_.c_str(), fifoMode) != 0) { + strerror_r(errno, errInfo, ERRINFOLEN); if (errno == EEXIST) { printf("another %s service is running.\n", perfCmd_.c_str()); } else { RemoveFifoFile(); } - strerror_r(errno, errInfo, ERRINFOLEN); HLOGE("create fifo file failed. %d:%s", errno, errInfo); return false; } @@ -101,26 +97,21 @@ bool PerfPipe::SendFifoAndWaitReply(const std::string &cmd, const std::chrono::m // need open for read first, because server maybe send reply before client wait to read char errInfo[ERRINFOLEN] = { 0 }; int fdRead = open(fifoFileS2C_.c_str(), O_RDONLY | O_NONBLOCK); - if (fdRead == -1) { - HLOGE("can not open fifo file(%s)", fifoFileS2C_.c_str()); - HIPERF_HILOGE(MODULE_DEFAULT, - "[SendFifoAndWaitReply] can not open fifo file: %{public}s, errno:(%{public}d:%{public}s)", - fifoFileS2C_.c_str(), errno, errInfo); - return false; - } + strerror_r(errno, errInfo, ERRINFOLEN); + CHECK_TRUE(fdRead != -1, false, LOG_TYPE_WITH_HILOG, + "[SendFifoAndWaitReply] can not open fifo file: %s, errno:(%d:%s)", + fifoFileS2C_.c_str(), errno, errInfo); int fdWrite = open(fifoFileC2S_.c_str(), O_WRONLY | O_NONBLOCK); + strerror_r(errno, errInfo, ERRINFOLEN); if (fdWrite == -1) { - HLOGE("can not open fifo file(%s)", fifoFileC2S_.c_str()); - HIPERF_HILOGE(MODULE_DEFAULT, - "[SendFifoAndWaitReply] can not open fifo file: %{public}s, errno:(%{public}d:%{public}s)", + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[SendFifoAndWaitReply] can not open fifo file: %s, errno:(%d:%s)", fifoFileC2S_.c_str(), errno, errInfo); close(fdRead); return false; } ssize_t size = write(fdWrite, cmd.c_str(), cmd.size()); if (size != static_cast(cmd.size())) { - HLOGE("failed to write fifo file(%s) command(%s)", fifoFileC2S_.c_str(), cmd.c_str()); - HIPERF_HILOGE(MODULE_DEFAULT, "failed to write fifo file(%{public}s) command(%{public}s).", + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[SendFifoAndWaitReply] failed to write fifo file(%s) command(%s)", fifoFileC2S_.c_str(), cmd.c_str()); close(fdWrite); close(fdRead); @@ -154,8 +145,7 @@ void PerfPipe::WaitFifoReply(const int fd, const std::chrono::milliseconds &time char c; ssize_t result = TEMP_FAILURE_RETRY(read(fd, &c, 1)); if (result <= 0) { - HLOGE("[WaitFifoReply] read from fifo file(%s) failed", fifoFileS2C_.c_str()); - HIPERF_HILOGE(MODULE_DEFAULT, "[WaitFifoReply] read from fifo file(%{public}s) failed", + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[WaitFifoReply] read from fifo file(%s) failed", fifoFileS2C_.c_str()); exitLoop = true; } @@ -165,11 +155,9 @@ void PerfPipe::WaitFifoReply(const int fd, const std::chrono::milliseconds &time } } } else if (polled == 0) { - HLOGD("[WaitFifoReply] wait fifo file(%s) timeout", fifoFileS2C_.c_str()); - HIPERF_HILOGD(MODULE_DEFAULT, "[WaitFifoReply] wait fifo file(%{public}s) timeout", fifoFileS2C_.c_str()); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[WaitFifoReply] wait fifo file(%s) timeout", fifoFileS2C_.c_str()); } else { - HLOGD("[WaitFifoReply] wait fifo file(%s) failed", fifoFileS2C_.c_str()); - HIPERF_HILOGD(MODULE_DEFAULT, "[WaitFifoReply] wait fifo file(%{public}s) failed", fifoFileS2C_.c_str()); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[WaitFifoReply] wait fifo file(%s) failed", fifoFileS2C_.c_str()); } } @@ -196,8 +184,7 @@ void PerfPipe::ProcessStopCommand(const bool ret) void PerfPipe::ProcessOutputCommand(bool ret) { if (!ret) { - HLOGI("send fifo and wait repoy fail"); - HIPERF_HILOGI(MODULE_DEFAULT, "send fifo and wait repoy fail"); + LOG_PRINT_ONLY(HIPERF_LOG_INFO, "[ProcessOutputCommand] send fifo and wait repoy fail"); return; } diff --git a/src/subcommand.cpp b/src/subcommand.cpp index 557121ef6a5ff05b19fb89dc5ac25ea912c2dc22..2584b6c4e27b446e63df1f07f90d37c0c0b987cb 100644 --- a/src/subcommand.cpp +++ b/src/subcommand.cpp @@ -77,11 +77,7 @@ bool SubCommand::CheckRestartOption(const std::string &appPackage, const bool ta printf("option --restart and -p/-a is conflict, please check usage\n"); return false; } - - if (!appPackage.empty()) { - return IsRestarted(appPackage); - } - return false; + return IsRestarted(appPackage); } bool SubCommand::HandleSubCommandExclude(const std::vector &excludeTids, diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index e0ed4e2a31342ed482b6e83845494f198ab6bbdf..6e55e2beab844dec2196436f2c80580e555846df 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -1215,26 +1215,17 @@ bool SubCommandRecord::ClientCommandResponse(const bool response) bool SubCommandRecord::ClientCommandResponse(const std::string& str) { + char errInfo[ERRINFOLEN] = { 0 }; if (!isHiperfClient_) { clientPipeOutput_ = open(fifoFileS2C_.c_str(), O_WRONLY); - if (clientPipeOutput_ == -1) { - char errInfo[ERRINFOLEN] = { 0 }; - strerror_r(errno, errInfo, ERRINFOLEN); - HLOGE("open fifo file(%s) failed, errno:%d:%s", fifoFileS2C_.c_str(), errno, errInfo); - HIPERF_HILOGE(MODULE_DEFAULT, "open fifo file(%{public}s) failed, errno:%{public}d:%{public}s", - fifoFileS2C_.c_str(), errno, errInfo); - return false; - } - } - ssize_t size = write(clientPipeOutput_, str.c_str(), str.size()); - if (size != static_cast(str.size())) { - char errInfo[ERRINFOLEN] = { 0 }; strerror_r(errno, errInfo, ERRINFOLEN); - HLOGD("Server:%s -> %d : %zd %d:%s", str.c_str(), clientPipeOutput_, size, errno, errInfo); - HIPERF_HILOGE(MODULE_DEFAULT, "Server:%{public}s -> %{public}d : %{public}zd %{public}d:%{public}s", - str.c_str(), clientPipeOutput_, size, errno, errInfo); - return false; + CHECK_TRUE(clientPipeOutput_ != -1, false, LOG_TYPE_WITH_HILOG, + "open fifo file(%s) failed, errno:%d:%s", fifoFileS2C_.c_str(), errno, errInfo); } + ssize_t size = write(clientPipeOutput_, str.c_str(), str.size()); + strerror_r(errno, errInfo, ERRINFOLEN); + CHECK_TRUE(size == static_cast(str.size()), false, LOG_TYPE_WITH_HILOG, + "Server:%s -> %d : %zd %d:%s", str.c_str(), clientPipeOutput_, size, errno, errInfo); return true; } @@ -1245,17 +1236,12 @@ bool SubCommandRecord::ChildResponseToMain(const bool response) bool SubCommandRecord::ChildResponseToMain(const std::string& str) { + char errInfo[ERRINFOLEN] = { 0 }; int tempFd = isHiperfClient_ ? clientPipeOutput_ : writeFd_; ssize_t size = write(tempFd, str.c_str(), str.size()); - if (size != static_cast(str.size())) { - char errInfo[ERRINFOLEN] = { 0 }; - strerror_r(errno, errInfo, ERRINFOLEN); - HLOGE("write pipe failed. str:%s, size:%zd, errno:%d:%s", str.c_str(), size, errno, errInfo); - HIPERF_HILOGE(MODULE_DEFAULT, - "write pipe failed. str:%{public}s, size:%{public}zd, errno:%{public}d:%{public}s", - str.c_str(), size, errno, errInfo); - return false; - } + strerror_r(errno, errInfo, ERRINFOLEN); + CHECK_TRUE(size == static_cast(str.size()), false, LOG_TYPE_WITH_HILOG, + "write pipe failed. str:%s, size:%zd, errno:%d:%s", str.c_str(), size, errno, errInfo); return true; } @@ -1266,29 +1252,25 @@ bool SubCommandRecord::MainRecvFromChild(const int fd, std::string& reply) }; int polled = poll(&pollFd, 1, CONTROL_WAIT_RESPONSE_TIMEOUT); reply.clear(); + CHECK_TRUE(polled != -1, false, LOG_TYPE_WITH_HILOG, + "wait pipeFd failed"); if (polled > 0) { bool exitLoop = false; while (!exitLoop) { char c; ssize_t result = TEMP_FAILURE_RETRY(read(fd, &c, 1)); if (result <= 0) { - HLOGD("read from pipeFd failed"); - HIPERF_HILOGE(MODULE_DEFAULT, "read from pipeFd failed"); - exitLoop = true; - } - reply.push_back(c); - if (c == '\n') { + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "read from pipeFd failed"); exitLoop = true; + } else { + reply.push_back(c); + if (c == '\n') { + exitLoop = true; + } } } - } else if (polled == 0) { - HLOGD("wait pipeFd timeout"); - HIPERF_HILOGE(MODULE_DEFAULT, "wait pipeFd timeout"); - return false; } else { - HLOGD("wait pipeFd failed"); - HIPERF_HILOGE(MODULE_DEFAULT, "wait pipeFd failed"); - return false; + CHECK_TRUE(false, false, LOG_TYPE_WITH_HILOG, "wait pipeFd timeout"); } return true; } @@ -1404,8 +1386,7 @@ inline void SubCommandRecord::CreateReplyThread() void SubCommandRecord::ReplyCommandHandle() { if (!IsSamplingRunning()) { - HLOGI("IsSamplingRunning() return false"); - HIPERF_HILOGI(MODULE_DEFAULT, "IsSamplingRunning() return false"); + LOG_PRINT_ONLY(HIPERF_LOG_INFO, "IsSamplingRunning() return false"); ChildResponseToMain(false); isHiperfClient_ = false; return; @@ -1447,8 +1428,7 @@ void SubCommandRecord::ClientCommandHandle() char c; ssize_t result = TEMP_FAILURE_RETRY(read(clientPipeInput_, &c, 1)); if (result <= 0) { - HLOGD("server :read from pipe file failed"); - HIPERF_HILOGD(MODULE_DEFAULT, "[ClientCommandHandle] server :read from pipe file failed"); + LOG_PRINT_ONLY(HIPERF_LOG_DEBUG, "[ClientCommandHandle] server :read from pipe file failed"); break; } command.push_back(c); @@ -1456,11 +1436,9 @@ void SubCommandRecord::ClientCommandHandle() break; } } - HLOGD("server:new command %s", command.c_str()); - HIPERF_HILOGI(MODULE_DEFAULT, "[ClientCommandHandle] server:new command : %{public}s", command.c_str()); + LOG_PRINT_ONLY(HIPERF_LOG_INFO, "[ClientCommandHandle] server:new command %s", command.c_str()); if (command.find("STOP") != std::string::npos) { - HLOGD("receive sop command, set g_callStop to true"); - HIPERF_HILOGI(MODULE_DEFAULT, "[ClientCommandHandle] Handlereceive sop command, set g_callStop to true"); + LOG_PRINT_ONLY(HIPERF_LOG_INFO, "[ClientCommandHandle] receive sop command, set g_callStop to true"); g_callStop.store(true); } DispatchControlCommand(command); @@ -1501,14 +1479,12 @@ void SubCommandRecord::RemoveFifoFile() char errInfo[ERRINFOLEN] = { 0 }; if (remove(fifoFileC2S_.c_str()) != 0) { strerror_r(errno, errInfo, ERRINFOLEN); - HLOGE("remove %s failed, errno:(%d:%s)", fifoFileC2S_.c_str(), errno, errInfo); - HIPERF_HILOGE(MODULE_DEFAULT, "remove %{public}s failed, errno:(%{public}d:%{public}s)", + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "remove %s failed, errno:(%d:%s)", fifoFileC2S_.c_str(), errno, errInfo); } if (remove(fifoFileS2C_.c_str()) != 0) { strerror_r(errno, errInfo, ERRINFOLEN); - HLOGE("remove %s failed, errno:(%d:%s)", fifoFileS2C_.c_str(), errno, errInfo); - HIPERF_HILOGE(MODULE_DEFAULT, "remove %{public}s failed, errno:(%{public}d:%{public}s)", + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "remove %s failed, errno:(%d:%s)", fifoFileS2C_.c_str(), errno, errInfo); } } @@ -1523,8 +1499,7 @@ bool SubCommandRecord::CreateFifoServer() int pipeFd[2]; if (pipe(pipeFd) == -1) { strerror_r(errno, errInfo, ERRINFOLEN); - HLOGE("pipe creation error, errno:(%d:%s)", errno, errInfo); - HIPERF_HILOGE(MODULE_DEFAULT, "pipe creation error, errno:(%{public}d:%{public}s)", errno, errInfo); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "pipe creation error, errno:(%d:%s)", errno, errInfo); RemoveFifoFile(); return false; } @@ -1535,8 +1510,7 @@ bool SubCommandRecord::CreateFifoServer() if (pid == -1) { strerror_r(errno, errInfo, ERRINFOLEN); - HLOGE("fork failed. %d:%s", errno, errInfo); - HIPERF_HILOGE(MODULE_DEFAULT, "[CreateFifoServer] fork failed. %{public}d:%{public}s", errno, errInfo); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[CreateFifoServer] fork failed. %d:%s", errno, errInfo); close(pipeFd[PIPE_READ]); close(pipeFd[PIPE_WRITE]); return false; @@ -1564,8 +1538,7 @@ bool SubCommandRecord::CreateFifoServer() isSuccess = true; break; } - HLOGE("reply is (%s)", reply.c_str()); - HIPERF_HILOGE(MODULE_DEFAULT, "reply is (%{public}s)", reply.c_str()); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "reply is (%s)", reply.c_str()); if (ret && reply.find("FAIL") == std::string::npos) { printf("%s", reply.c_str()); if (reply.find("debug application") != std::string::npos) { @@ -1665,8 +1638,7 @@ HiperfError SubCommandRecord::OnSubCommand(std::vector& args) ChildResponseToMain(false); CloseClientThread(); } - HIPERF_HILOGE(MODULE_DEFAULT, "[OnSubCommand] Fail to prepare tracking"); - HLOGE("Fail to prepare tracking"); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[OnSubCommand] Fail to prepare tracking"); return HiperfError::PREPARE_TACKING_FAIL; } HIPERF_HILOGI(MODULE_DEFAULT, "[OnSubCommand] SubCommandRecord perfEvents prepared"); @@ -1676,8 +1648,7 @@ HiperfError SubCommandRecord::OnSubCommand(std::vector& args) ChildResponseToMain(false); CloseClientThread(); } - HLOGE("Fail to create record file %s", outputFilename_.c_str()); - HIPERF_HILOGE(MODULE_DEFAULT, "[OnSubCommand] Fail to create record file"); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[OnSubCommand] Fail to create record file %s", outputFilename_.c_str()); return HiperfError::CREATE_OUTPUT_FILE_FAIL; } HIPERF_HILOGI(MODULE_DEFAULT, "[OnSubCommand] CreateInitRecordFile finished"); @@ -1686,8 +1657,7 @@ HiperfError SubCommandRecord::OnSubCommand(std::vector& args) ChildResponseToMain(false); CloseClientThread(); } - HLOGE("Fail to prepare virtualRuntime"); - HIPERF_HILOGE(MODULE_DEFAULT, "[OnSubCommand] Fail to prepare virtualRuntime"); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[OnSubCommand] Fail to prepare virtualRuntime"); return HiperfError::PREPARE_VIRTUAL_RUNTIME_FAIL; } @@ -1724,12 +1694,10 @@ HiperfError SubCommandRecord::OnSubCommand(std::vector& args) startSaveFileTimes_ = steady_clock::now(); if (!backtrack_) { if (!FinishWriteRecordFile()) { - HLOGE("Fail to finish record file %s", outputFilename_.c_str()); - HIPERF_HILOGE(MODULE_DEFAULT, "Fail to finish record file"); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[OnSubCommand] Fail to finish record file %s", outputFilename_.c_str()); return HiperfError::FINISH_WRITE_RECORD_FILE_FAIL; } else if (!PostProcessRecordFile()) { - HLOGE("Fail to post process record file"); - HIPERF_HILOGE(MODULE_DEFAULT, "Fail to post process record file"); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[OnSubCommand] Fail to post process record file"); return HiperfError::POST_PROCESS_RECORD_FILE; } RecordCompleted(); diff --git a/src/subcommand_stat.cpp b/src/subcommand_stat.cpp index 2ff131614181de0e459598d2ebabd196a16d9ba2..1af379dd64d162f1fc79d7e59ab45b45b48ea224 100644 --- a/src/subcommand_stat.cpp +++ b/src/subcommand_stat.cpp @@ -721,13 +721,9 @@ bool SubCommandStat::CreateFifoServer() close(STDERR_FILENO); isFifoServer_ = true; clientPipeOutput_ = open(fifoFileS2C_.c_str(), O_WRONLY); - if (clientPipeOutput_ == -1) { - strerror_r(errno, errInfo, ERRINFOLEN); - HLOGE("open fifo file(%s) failed. %d:%s", fifoFileS2C_.c_str(), errno, errInfo); - HIPERF_HILOGE(MODULE_DEFAULT, "open fifo file(%{public}s) failed. %d:%s", - fifoFileS2C_.c_str(), errno, errInfo); - return false; - } + strerror_r(errno, errInfo, ERRINFOLEN); + CHECK_TRUE(clientPipeOutput_ != -1, false, LOG_TYPE_WITH_HILOG, + "[CreateFifoServer] open fifo file(%s) failed. %d:%s", fifoFileS2C_.c_str(), errno, errInfo); nullFd_ = open("/dev/null", O_WRONLY); (void)dup2(nullFd_, STDOUT_FILENO); // redirect stdout to /dev/null std::string err = OHOS::Developtools::HiPerf::HandleAppInfo(appPackage_, inputPidTidArgs_); @@ -745,27 +741,23 @@ bool SubCommandStat::CreateFifoServer() if (fd == -1 || reply != HiperfClient::ReplyOK) { if (reply != HiperfClient::ReplyOK) { printf("%s", reply.c_str()); - HLOGE("reply is %s", reply.c_str()); - HIPERF_HILOGE(MODULE_DEFAULT, "reply is %{public}s", reply.c_str()); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[CreateFifoServer] reply is %s", reply.c_str()); } - HLOGI("fd is %d", fd); - HIPERF_HILOGI(MODULE_DEFAULT, "fd is %{public}d", fd); + LOG_PRINT_ONLY(HIPERF_LOG_INFO, "[CreateFifoServer] fd is %d", fd); close(fd); if (kill(pid, SIGTERM) != 0) { - HLOGE("Failed to send SIGTERM: %d", pid); - HIPERF_HILOGE(MODULE_DEFAULT, "Failed to send SIGTERM to pid: %{public}d", pid); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[CreateFifoServer] Failed to send SIGTERM to pid: %d", pid); } // wait for process exit if (waitpid(pid, nullptr, 0) == -1) { - HLOGE("Failed to wait for pid: %d", pid); - HIPERF_HILOGE(MODULE_DEFAULT, "Failed to wait for pid: %{public}d", pid); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[CreateFifoServer] Failed to wait for pid: %d", pid); } remove(fifoFileC2S_.c_str()); remove(fifoFileS2C_.c_str()); strerror_r(errno, errInfo, ERRINFOLEN); printf("create control hiperf counting failed.\n"); - HLOGI("errno is %d:%s", errno, errInfo); - HIPERF_HILOGI(MODULE_DEFAULT, "errno is %{public}d:%{public}s", errno, errInfo); + LOG_PRINT_ONLY(HIPERF_LOG_INFO, "[CreateFifoServer] create control hiperf counting failed, errno is %d:%s", + errno, errInfo); return false; } close(fd); @@ -782,13 +774,11 @@ bool SubCommandStat::ClientCommandResponse(const bool response) bool SubCommandStat::ClientCommandResponse(const std::string& str) { + char errInfo[ERRINFOLEN] = { 0 }; ssize_t size = write(clientPipeOutput_, str.c_str(), str.size()); - if (size != static_cast(str.size())) { - char errInfo[ERRINFOLEN] = { 0 }; - strerror_r(errno, errInfo, ERRINFOLEN); - HLOGD("Server:%s -> %d : %zd %d:%s", str.c_str(), clientPipeOutput_, size, errno, errInfo); - return false; - } + strerror_r(errno, errInfo, ERRINFOLEN); + CHECK_TRUE(size == static_cast(str.size()), false, LOG_TYPE_WITH_HILOG, + "Server:%s -> %d : %zd %d:%s", str.c_str(), clientPipeOutput_, size, errno, errInfo); return true; } @@ -863,8 +853,7 @@ void SubCommandStat::ClientCommandHandle() char c; ssize_t result = TEMP_FAILURE_RETRY(read(clientPipeInput_, &c, 1)); if (result <= 0) { - HLOGD("server :read from pipe file failed"); - HIPERF_HILOGI(MODULE_DEFAULT, "server :read from pipe file failed"); + LOG_PRINT_ONLY(HIPERF_LOG_ERROR, "[ClientCommandHandle] server :read from pipe file failed"); exitLoop = true; } command.push_back(c); @@ -872,8 +861,7 @@ void SubCommandStat::ClientCommandHandle() exitLoop = true; } } - HLOGD("server:new command %s", command.c_str()); - HIPERF_HILOGI(MODULE_DEFAULT, "server:new command : %{public}s", command.c_str()); + LOG_PRINT_ONLY(HIPERF_LOG_INFO, "[ClientCommandHandle] server:new command %s", command.c_str()); DispatchControlCommand(command); } }