1 /* 2 * Copyright (c) 2021 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 16 #ifndef HILOG_DATA_H 17 #define HILOG_DATA_H 18 19 #include <cstring> 20 #include <iostream> 21 #include <securec.h> 22 23 #include <hilog/log.h> 24 #include <hilog_common.h> 25 26 namespace OHOS { 27 namespace HiviewDFX { 28 struct HilogData { 29 uint16_t len; /* tag length plus fmt length include '\0' */ 30 uint16_t version : 3; 31 uint16_t type : 3; /* APP,CORE,INIT,SEC etc */ 32 uint16_t level : 4; 33 uint16_t tagLen : 6; /* include '\0' */ 34 uint32_t tv_sec; 35 uint32_t tv_nsec; 36 uint32_t mono_sec; 37 uint32_t pid; 38 uint32_t tid; 39 uint32_t domain; 40 char* tag; 41 char* content; 42 InitHilogData43 void Init(const char *mtag, uint16_t mtagLen, const char *mfmt, size_t mfmtLen) 44 { 45 if (unlikely(mtagLen > MAX_TAG_LEN || mtagLen == 0 || mfmtLen > MAX_LOG_LEN || mfmtLen <= 0)) { 46 return; 47 } 48 49 len = mtagLen + mfmtLen; 50 char* tmp = new (std::nothrow) char[len]; 51 if (unlikely(tmp == nullptr)) { 52 return; 53 } 54 tag = tmp; 55 content = tmp + mtagLen; 56 if (strncpy_s(tag, mtagLen + 1, mtag, mtagLen - 1)) { 57 return; 58 } 59 if (strncpy_s(content, mfmtLen + 1, mfmt, mfmtLen - 1)) { 60 return; 61 } 62 } 63 HilogDataHilogData64 HilogData() : len(0), tag(nullptr), content(nullptr) {} HilogDataHilogData65 explicit HilogData(const HilogMsg& msg) 66 : len(0), version(msg.version), type(msg.type), level(msg.level), tagLen(msg.tagLen), 67 tv_sec(msg.tv_sec), tv_nsec(msg.tv_nsec), mono_sec(msg.mono_sec), pid(msg.pid), tid(msg.tid), 68 domain(msg.domain), tag(nullptr), content(nullptr) 69 { 70 Init(msg.tag, msg.tagLen, CONTENT_PTR((&msg)), CONTENT_LEN((&msg))); 71 } 72 HilogDataHilogData73 HilogData(const HilogData& copy) 74 { 75 if (unlikely(memcpy_s(this, sizeof(HilogData), ©, sizeof(HilogData)) != 0)) { 76 std::cerr << "HilogData copy error." << std::endl; 77 } 78 tag = new (std::nothrow) char[len]; 79 if (unlikely(tag == nullptr)) { 80 return; 81 } 82 if (unlikely(memcpy_s(tag, len, copy.tag, len) != 0)) { 83 return; 84 } 85 content = tag + tagLen; 86 } 87 88 HilogData& operator=(const HilogData&) = delete; 89 90 HilogData(HilogData&&) = delete; 91 ~HilogDataHilogData92 ~HilogData() 93 { 94 delete []tag; 95 tag = nullptr; 96 content = nullptr; 97 } 98 }; 99 } // namespace HiviewDFX 100 } // namespace OHOS 101 #endif /* HILOG_DATA_H */ 102