12498b56bSopenharmony_ci/*
22498b56bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42498b56bSopenharmony_ci * you may not use this file except in compliance with the License.
52498b56bSopenharmony_ci * You may obtain a copy of the License at
62498b56bSopenharmony_ci *
72498b56bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
82498b56bSopenharmony_ci *
92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122498b56bSopenharmony_ci * See the License for the specific language governing permissions and
132498b56bSopenharmony_ci * limitations under the License.
142498b56bSopenharmony_ci */
152498b56bSopenharmony_ci
162498b56bSopenharmony_ci#ifndef HILOG_DATA_H
172498b56bSopenharmony_ci#define HILOG_DATA_H
182498b56bSopenharmony_ci
192498b56bSopenharmony_ci#include <cstring>
202498b56bSopenharmony_ci#include <iostream>
212498b56bSopenharmony_ci#include <securec.h>
222498b56bSopenharmony_ci
232498b56bSopenharmony_ci#include <hilog/log.h>
242498b56bSopenharmony_ci#include <hilog_common.h>
252498b56bSopenharmony_ci
262498b56bSopenharmony_cinamespace OHOS {
272498b56bSopenharmony_cinamespace HiviewDFX {
282498b56bSopenharmony_cistruct HilogData {
292498b56bSopenharmony_ci    uint16_t len; /* tag length plus fmt length include '\0' */
302498b56bSopenharmony_ci    uint16_t version : 3;
312498b56bSopenharmony_ci    uint16_t type : 3;  /* APP,CORE,INIT,SEC etc */
322498b56bSopenharmony_ci    uint16_t level : 4;
332498b56bSopenharmony_ci    uint16_t tagLen : 6; /* include '\0' */
342498b56bSopenharmony_ci    uint32_t tv_sec;
352498b56bSopenharmony_ci    uint32_t tv_nsec;
362498b56bSopenharmony_ci    uint32_t mono_sec;
372498b56bSopenharmony_ci    uint32_t pid;
382498b56bSopenharmony_ci    uint32_t tid;
392498b56bSopenharmony_ci    uint32_t domain;
402498b56bSopenharmony_ci    char* tag;
412498b56bSopenharmony_ci    char* content;
422498b56bSopenharmony_ci
432498b56bSopenharmony_ci    void Init(const char *mtag, uint16_t mtagLen, const char *mfmt, size_t mfmtLen)
442498b56bSopenharmony_ci    {
452498b56bSopenharmony_ci        if (unlikely(mtagLen > MAX_TAG_LEN || mtagLen == 0 || mfmtLen > MAX_LOG_LEN || mfmtLen <= 0)) {
462498b56bSopenharmony_ci            return;
472498b56bSopenharmony_ci        }
482498b56bSopenharmony_ci
492498b56bSopenharmony_ci        len = mtagLen + mfmtLen;
502498b56bSopenharmony_ci        char* tmp = new (std::nothrow) char[len];
512498b56bSopenharmony_ci        if (unlikely(tmp == nullptr)) {
522498b56bSopenharmony_ci            return;
532498b56bSopenharmony_ci        }
542498b56bSopenharmony_ci        tag = tmp;
552498b56bSopenharmony_ci        content = tmp + mtagLen;
562498b56bSopenharmony_ci        if (strncpy_s(tag, mtagLen + 1, mtag, mtagLen - 1)) {
572498b56bSopenharmony_ci            return;
582498b56bSopenharmony_ci        }
592498b56bSopenharmony_ci        if (strncpy_s(content, mfmtLen + 1, mfmt, mfmtLen - 1)) {
602498b56bSopenharmony_ci            return;
612498b56bSopenharmony_ci        }
622498b56bSopenharmony_ci    }
632498b56bSopenharmony_ci
642498b56bSopenharmony_ci    HilogData() : len(0), tag(nullptr), content(nullptr) {}
652498b56bSopenharmony_ci    explicit HilogData(const HilogMsg& msg)
662498b56bSopenharmony_ci        : len(0), version(msg.version), type(msg.type), level(msg.level), tagLen(msg.tagLen),
672498b56bSopenharmony_ci        tv_sec(msg.tv_sec), tv_nsec(msg.tv_nsec), mono_sec(msg.mono_sec), pid(msg.pid), tid(msg.tid),
682498b56bSopenharmony_ci        domain(msg.domain), tag(nullptr), content(nullptr)
692498b56bSopenharmony_ci    {
702498b56bSopenharmony_ci        Init(msg.tag, msg.tagLen, CONTENT_PTR((&msg)), CONTENT_LEN((&msg)));
712498b56bSopenharmony_ci    }
722498b56bSopenharmony_ci
732498b56bSopenharmony_ci    HilogData(const HilogData& copy)
742498b56bSopenharmony_ci    {
752498b56bSopenharmony_ci        if (unlikely(memcpy_s(this, sizeof(HilogData), &copy, sizeof(HilogData)) != 0)) {
762498b56bSopenharmony_ci            std::cerr << "HilogData copy error." << std::endl;
772498b56bSopenharmony_ci        }
782498b56bSopenharmony_ci        tag = new (std::nothrow) char[len];
792498b56bSopenharmony_ci        if (unlikely(tag == nullptr)) {
802498b56bSopenharmony_ci            return;
812498b56bSopenharmony_ci        }
822498b56bSopenharmony_ci        if (unlikely(memcpy_s(tag, len, copy.tag, len) != 0)) {
832498b56bSopenharmony_ci            return;
842498b56bSopenharmony_ci        }
852498b56bSopenharmony_ci        content = tag + tagLen;
862498b56bSopenharmony_ci    }
872498b56bSopenharmony_ci
882498b56bSopenharmony_ci    HilogData& operator=(const HilogData&) = delete;
892498b56bSopenharmony_ci
902498b56bSopenharmony_ci    HilogData(HilogData&&) = delete;
912498b56bSopenharmony_ci
922498b56bSopenharmony_ci    ~HilogData()
932498b56bSopenharmony_ci    {
942498b56bSopenharmony_ci        delete []tag;
952498b56bSopenharmony_ci        tag = nullptr;
962498b56bSopenharmony_ci        content = nullptr;
972498b56bSopenharmony_ci    }
982498b56bSopenharmony_ci};
992498b56bSopenharmony_ci} // namespace HiviewDFX
1002498b56bSopenharmony_ci} // namespace OHOS
1012498b56bSopenharmony_ci#endif /* HILOG_DATA_H */
102