• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 tag_len : 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;
initHilogData42     void init(const char *mtag, uint16_t mtagLen, const char *mfmt, size_t mfmtLen)
43     {
44         if (unlikely(mtagLen > MAX_TAG_LEN || mtagLen == 0 || mfmtLen > MAX_LOG_LEN || mfmtLen <= 0)) {
45             return;
46         }
47 
48         len = mtagLen + mfmtLen;
49         char* tmp = new (std::nothrow) char[len];
50         if (unlikely(tmp == nullptr)) {
51             return;
52         }
53         tag = tmp;
54         content = tmp + mtagLen;
55         if (strncpy_s(tag, mtagLen + 1, mtag, mtagLen - 1)) {
56             return;
57         }
58         if (strncpy_s(content, mfmtLen + 1, mfmt, mfmtLen - 1)) {
59             return;
60         }
61     }
deinitHilogData62     void deinit()
63     {
64         delete []tag;
65         tag = nullptr;
66         content = nullptr;
67     }
HilogDataHilogData68     HilogData() : len(0), tag(nullptr), content(nullptr) {}
HilogDataHilogData69     explicit HilogData(const HilogMsg& msg)
70         : len(0), version(msg.version), type(msg.type), level(msg.level), tag_len(msg.tag_len),
71         tv_sec(msg.tv_sec), tv_nsec(msg.tv_nsec), mono_sec(msg.mono_sec), pid(msg.pid), tid(msg.tid),
72         domain(msg.domain), tag(nullptr), content(nullptr)
73     {
74         init(msg.tag, msg.tag_len, CONTENT_PTR((&msg)), CONTENT_LEN((&msg)));
75     }
HilogDataHilogData76     HilogData(const HilogData& copy)
77     {
78         if (unlikely(memcpy_s(this, sizeof(HilogData), &copy, sizeof(HilogData)) != 0)) {
79             std::cerr << "HilogData copy error." << std::endl;
80         }
81         tag = new (std::nothrow) char[len];
82         if (unlikely(tag == nullptr)) {
83             return;
84         }
85         if (unlikely(memcpy_s(tag, len, copy.tag, len) != 0)) {
86             return;
87         }
88         content = tag + tag_len;
89     }
90     HilogData& operator=(const HilogData&) = delete;
91     HilogData(HilogData&&) = delete;
92 
~HilogDataHilogData93     ~HilogData()
94     {
95         deinit();
96     }
97 };
98 } // namespace HiviewDFX
99 } // namespace OHOS
100 #endif /* HILOG_DATA_H */
101