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 "hilogtool_msg.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 : 4; /* APP,CORE,INIT,SEC etc */ 32 uint16_t level : 3; 33 uint16_t tag_len : 6; /* include '\0' */ 34 uint32_t tv_sec; 35 uint32_t tv_nsec; 36 uint32_t pid; 37 uint32_t tid; 38 uint32_t domain; 39 char* tag; 40 char* content; initHilogData41 void init(const char *mtag, uint16_t mtagLen, const char *mfmt, size_t mfmtLen) 42 { 43 if (unlikely(mtagLen > MAX_TAG_LEN || mtagLen == 0 || mfmtLen > MAX_LOG_LEN || mfmtLen <= 0)) { 44 return; 45 } 46 47 len = mtagLen + mfmtLen; 48 char* tmp = new (std::nothrow) char[len]; 49 if (unlikely(tmp == nullptr)) { 50 return; 51 } 52 tag = tmp; 53 content = tmp + mtagLen; 54 if (strncpy_s(tag, mtagLen + 1, mtag, mtagLen - 1)) { 55 return; 56 } 57 if (strncpy_s(content, mfmtLen + 1, mfmt, mfmtLen - 1)) { 58 return; 59 } 60 }; deinitHilogData61 void deinit() 62 { 63 delete []tag; 64 tag = nullptr; 65 content = nullptr; 66 } HilogDataHilogData67 HilogData() : len(0), tag(nullptr), content(nullptr) {}; HilogDataHilogData68 HilogData(const HilogMsg& msg) 69 : len(0), version(msg.version), type(msg.type), level(msg.level), tag_len(msg.tag_len), 70 tv_sec(msg.tv_sec), tv_nsec(msg.tv_nsec), pid(msg.pid), tid(msg.tid), domain(msg.domain), 71 tag(nullptr), content(nullptr) 72 { 73 init(msg.tag, msg.tag_len, CONTENT_PTR((&msg)), CONTENT_LEN((&msg))); 74 } 75 HilogData(const HilogData&) = delete; 76 HilogData& operator=(const HilogData&) = delete; 77 HilogDataHilogData78 HilogData(HilogData&& cpy) 79 { 80 if (memcpy_s(this, sizeof(HilogData), &cpy, sizeof(HilogData)) != 0) { 81 std::cerr << "HilogData memcpy_s call failed.\n"; 82 } 83 cpy.tag = nullptr; 84 cpy.content = nullptr; 85 } 86 ~HilogDataHilogData87 ~HilogData() 88 { 89 deinit(); 90 } 91 }; 92 } // namespace HiviewDFX 93 } // namespace OHOS 94 #endif /* HILOG_DATA_H */ 95