diff --git a/frameworks/native/BUILD.gn b/frameworks/native/BUILD.gn index 4e5bffbdfeced53ec79504acbff22b743f9dd15d..2bb2396dc1ea9f0ab1ecd2ad0a5ce20588d7221a 100644 --- a/frameworks/native/BUILD.gn +++ b/frameworks/native/BUILD.gn @@ -38,7 +38,7 @@ ohos_source_set("libhilog_source") { ] defines = [ "HILOG_DEFAULT_PRIVACY=$ohos_hilog_default_privacy" ] - + defines += [ "__RECV_MSG_WITH_UCRED_" ] public_configs = [ ":libhilog_config" ] configs = [ ":libhilog_config" ] deps = [ diff --git a/frameworks/native/hilog_printf.cpp b/frameworks/native/hilog_printf.cpp index 81e291f4506f217ca7100d08636dc32792fe5739..fd0512945e494f31bef129f846b23e30b95bc7a8 100644 --- a/frameworks/native/hilog_printf.cpp +++ b/frameworks/native/hilog_printf.cpp @@ -247,7 +247,9 @@ int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int int logLen = strnlen(buf, MAX_LOG_LEN - 1); header.type = type; header.level = level; +#ifndef __RECV_MSG_WITH_UCRED_ header.pid = getpid(); +#endif header.tid = syscall(SYS_gettid); header.domain = domain; diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index 59814b3bf92d7688fc7e4894216e1465b6f4f524..e241cd5b412b2c1dfe5268366cf5e3c96d3bb6fe 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -33,6 +33,7 @@ ohos_executable("hilogd") { "main.cpp", ] configs = [ ":hilogd_config" ] + defines = [ "__RECV_MSG_WITH_UCRED_" ] deps = [ "//base/hiviewdfx/hilog/adapter:libhilog_os_adapter", "//base/hiviewdfx/hilog/frameworks/native:libhilogutil", diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index 08d8e09c37aa0510a26158fb7052aac9bb8b3a93..aa37e2c03c5aee5e78f57a345d3dbf7c21f8af96 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -24,9 +24,14 @@ namespace HiviewDFX { class LogCollector { public: LogCollector(HilogBuffer* buffer); - void operator ()(); + void operator ()(); + static int FlowCtrlDataRecv(HilogMsg *msg, int ret); static size_t InsertLogToBuffer(const HilogMsg& msg); +#ifndef __RECV_MSG_WITH_UCRED_ static int onDataRecv(char *data, unsigned int dataLen); +#else + static int onDataRecv(ucred cred, char *data, unsigned int dataLen); +#endif ~LogCollector() = default; private: static HilogBuffer* hilogBuffer; diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 1a6531fbb88dad92b15e254aabb4c5823a80474c..ab73cec3cec9f343c87fa944476d9d484f17910c 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -26,6 +26,34 @@ namespace OHOS { namespace HiviewDFX { using namespace std; HilogBuffer* LogCollector::hilogBuffer = nullptr; +int LogCollector::FlowCtrlDataRecv(HilogMsg *msg, int ret) +{ + string dropLog = to_string(ret) + " line(s) dropped!"; + char tag[] = "LOGLIMITD"; + int len = sizeof(HilogMsg) + sizeof(tag) + 1 + dropLog.size() + 1; + HilogMsg *dropMsg = (HilogMsg*)malloc(len); + if (dropMsg != nullptr) { + dropMsg->len = len; + dropMsg->version = msg->version; + dropMsg->type = msg->type; + dropMsg->level = msg->level; + dropMsg->tag_len = sizeof(tag) + 1; + dropMsg->tv_sec = msg->tv_sec; + dropMsg->tv_nsec = msg->tv_nsec; + dropMsg->pid = msg->pid; + dropMsg->tid = msg->tid; + dropMsg->domain = msg->domain; + if (memcpy_s(dropMsg->tag, len - sizeof(HilogMsg), tag, sizeof(tag) + 1)) { + } + if (memcpy_s(dropMsg->tag + sizeof(tag) + 1, len - sizeof(HilogMsg) - sizeof(tag) - 1, + dropLog.c_str(), dropLog.size() + 1)) { + } + InsertLogToBuffer(*dropMsg); + free(dropMsg); + } + return 0; +} +#ifndef __RECV_MSG_WITH_UCRED_ int LogCollector::onDataRecv(char *data, unsigned int dataLen) { HilogMsg *msg = (HilogMsg *)data; @@ -34,34 +62,27 @@ int LogCollector::onDataRecv(char *data, unsigned int dataLen) if (ret < 0) { return 0; } else if (ret > 0) { /* if >0 !Need print how many lines was dopped */ - string dropLog = to_string(ret) + " line(s) dropped!"; - char tag[] = "LOGLIMITD"; - int len = sizeof(HilogMsg) + sizeof(tag) + 1 + dropLog.size() + 1; - HilogMsg *dropMsg = (HilogMsg*)malloc(len); - if (dropMsg != nullptr) { - dropMsg->len = len; - dropMsg->version = msg->version; - dropMsg->type = msg->type; - dropMsg->level = msg->level; - dropMsg->tag_len = sizeof(tag) + 1; - dropMsg->tv_sec = msg->tv_sec; - dropMsg->tv_nsec = msg->tv_nsec; - dropMsg->pid = msg->pid; - dropMsg->tid = msg->tid; - dropMsg->domain = msg->domain; - if (memcpy_s(dropMsg->tag, len - sizeof(HilogMsg), tag, sizeof(tag) + 1)) { - } - if (memcpy_s(dropMsg->tag + sizeof(tag) + 1, len - sizeof(HilogMsg) - sizeof(tag) - 1, - dropLog.c_str(), dropLog.size() + 1)) { - } - InsertLogToBuffer(*dropMsg); - free(dropMsg); - } + FlowCtrlDataRecv(msg, ret); + } + InsertLogToBuffer(*msg); + return 0; +} +#else +int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) +{ + HilogMsg *msg = (HilogMsg *)data; + msg->pid = cred.pid; + /* Domain flow control */ + int ret = FlowCtrlDomain(msg); + if (ret < 0) { + return 0; + } else if (ret > 0) { /* if >0 !Need print how many lines was dopped */ + FlowCtrlDataRecv(msg, ret); } - InsertLogToBuffer(*msg); return 0; } +#endif LogCollector::LogCollector(HilogBuffer* buffer) {