diff --git a/frameworks/native/BUILD.gn b/frameworks/native/BUILD.gn index a8892925b961c658a1a203699e455a4fe0b1f499..04fb3829790f3d047fd7b82bc429887802d94c10 100644 --- a/frameworks/native/BUILD.gn +++ b/frameworks/native/BUILD.gn @@ -38,8 +38,9 @@ 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 = [ "//base/hiviewdfx/hilog/frameworks/native:libhilogutil", @@ -70,7 +71,7 @@ ohos_shared_library("libhilogutil") { ] public_configs = [ ":libhilogutil_config" ] configs = [ ":libhilogutil_config" ] - + defines = [ "__RECV_MSG_WITH_UCRED_" ] deps = [ "//base/hiviewdfx/hilog/adapter:libhilog_os_adapter", "//utils/native/base:utilsecurec_shared", @@ -96,7 +97,7 @@ ohos_shared_library("libhilogutil_native") { ] public_configs = [ ":libhilogutil_config" ] configs = [ ":libhilogutil_config" ] - + defines = [ "__RECV_MSG_WITH_UCRED_" ] deps = [ "//base/hiviewdfx/hilog/adapter:libhilog_os_adapter_native", "//utils/native/base:utilsecurec_shared", diff --git a/frameworks/native/hilog_printf.cpp b/frameworks/native/hilog_printf.cpp index 81e291f4506f217ca7100d08636dc32792fe5739..611aeea32c5a628204533a573e04432aa79d7240 100644 --- a/frameworks/native/hilog_printf.cpp +++ b/frameworks/native/hilog_printf.cpp @@ -247,8 +247,10 @@ 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; - header.pid = getpid(); header.tid = syscall(SYS_gettid); +#ifndef __RECV_MSG_WITH_UCRED_ + header.pid = getpid(); +#endif header.domain = domain; /* flow control */ diff --git a/frameworks/native/include/hilog_input_socket_server.h b/frameworks/native/include/hilog_input_socket_server.h index a63d419ffb75a171af1064317f7f4865977489c7..3961fa9473ee7635a6d30c7fcda0f40bece933a9 100644 --- a/frameworks/native/include/hilog_input_socket_server.h +++ b/frameworks/native/include/hilog_input_socket_server.h @@ -18,6 +18,7 @@ #include "dgram_socket_server.h" + namespace OHOS { namespace HiviewDFX { #define MAX_SOCKET_PACKET_LEN 4096 diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index a0e435916b6e90ad0e875f09f14ceb5bb82cc4ca..8adfb2ff6bf25acfbcf10ab837020c463dff831e 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -33,7 +33,7 @@ ohos_executable("hilogd") { "log_reader.cpp", "main.cpp", ] - + defines = [ "__RECV_MSG_WITH_UCRED_" ] configs = [ ":hilogd_config" ] deps = [ @@ -65,7 +65,7 @@ ohos_executable("hilogd_native") { "log_reader.cpp", "main.cpp", ] - + defines = [ "__RECV_MSG_WITH_UCRED_" ] configs = [ ":hilogd_config" ] deps = [ diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index 08d8e09c37aa0510a26158fb7052aac9bb8b3a93..3109a7d8a647ff28c70ac761a82b20dc61ba49f1 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -25,8 +25,13 @@ class LogCollector { public: LogCollector(HilogBuffer* buffer); 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_buffer.cpp b/services/hilogd/log_buffer.cpp index fadacdaba3eebb79371e4dc1b5830812d11c114b..81c828070cb26afdc5da09775bc36785d466dd24 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -55,7 +55,6 @@ HilogBuffer::~HilogBuffer() {} size_t HilogBuffer::Insert(const HilogMsg& msg) { - printf("insert: %s\n", CONTENT_PTR((&msg))); size_t eleSize = CONTENT_LEN((&msg)); /* include '\0' */ if (unlikely(msg.tag_len > MAX_TAG_LEN || msg.tag_len == 0 || eleSize > MAX_LOG_LEN || eleSize <= 0)) { diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 24290013a5b772112b7016b93e0b06817a071544..313b2e561c96220bcd238d05d7cb6afdfbc3b1a1 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -26,6 +26,36 @@ 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 +64,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); } - - InsertLogToBuffer(*msg); return 0; } +#endif LogCollector::LogCollector(HilogBuffer* buffer) {