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#include "kmsg_parser.h"
172498b56bSopenharmony_ci#include "hilog/log.h"
182498b56bSopenharmony_ci
192498b56bSopenharmony_ci#include <cstdlib>
202498b56bSopenharmony_ci#include <cinttypes>
212498b56bSopenharmony_ci#include <iostream>
222498b56bSopenharmony_ci#include <string>
232498b56bSopenharmony_ci#include <thread>
242498b56bSopenharmony_ci#include <chrono>
252498b56bSopenharmony_ci#include <fstream>
262498b56bSopenharmony_ci#include <regex>
272498b56bSopenharmony_ci#include <sys/time.h>
282498b56bSopenharmony_ci#include <unistd.h>
292498b56bSopenharmony_ci#include <fcntl.h>
302498b56bSopenharmony_ci#include <sys/types.h>
312498b56bSopenharmony_ci#include <string_view>
322498b56bSopenharmony_ci
332498b56bSopenharmony_cinamespace OHOS {
342498b56bSopenharmony_cinamespace HiviewDFX {
352498b56bSopenharmony_ciusing namespace std::chrono;
362498b56bSopenharmony_ciusing namespace std::literals;
372498b56bSopenharmony_ci
382498b56bSopenharmony_ci// Avoid name collision between sys/syslog.h and our log_c.h
392498b56bSopenharmony_ci#undef LOG_FATAL
402498b56bSopenharmony_ci#undef LOG_ERR
412498b56bSopenharmony_ci#undef LOG_WARN
422498b56bSopenharmony_ci#undef LOG_INFO
432498b56bSopenharmony_ci#undef LOG_DEBUG
442498b56bSopenharmony_ci
452498b56bSopenharmony_ciusing Priority = enum {
462498b56bSopenharmony_ci    PV0 = 0,
472498b56bSopenharmony_ci    PV1,
482498b56bSopenharmony_ci    PV2,
492498b56bSopenharmony_ci    PV3,
502498b56bSopenharmony_ci    PV4,
512498b56bSopenharmony_ci    PV5,
522498b56bSopenharmony_ci    PV6
532498b56bSopenharmony_ci};
542498b56bSopenharmony_ci
552498b56bSopenharmony_ci// Log levels are different in syslog.h and hilog log_c.h
562498b56bSopenharmony_cistatic uint16_t KmsgLevelMap(uint16_t prio)
572498b56bSopenharmony_ci{
582498b56bSopenharmony_ci    uint16_t level;
592498b56bSopenharmony_ci    switch (prio) {
602498b56bSopenharmony_ci        case Priority::PV0:
612498b56bSopenharmony_ci        case Priority::PV1:
622498b56bSopenharmony_ci        case Priority::PV2:
632498b56bSopenharmony_ci            level = LOG_FATAL;
642498b56bSopenharmony_ci            break;
652498b56bSopenharmony_ci        case Priority::PV3:
662498b56bSopenharmony_ci            level = LOG_ERROR;
672498b56bSopenharmony_ci            break;
682498b56bSopenharmony_ci        case Priority::PV4:
692498b56bSopenharmony_ci        case Priority::PV5:
702498b56bSopenharmony_ci            level = LOG_WARN;
712498b56bSopenharmony_ci            break;
722498b56bSopenharmony_ci        case Priority::PV6:
732498b56bSopenharmony_ci            level = LOG_INFO;
742498b56bSopenharmony_ci            break;
752498b56bSopenharmony_ci        default:
762498b56bSopenharmony_ci            level = LOG_DEBUG;
772498b56bSopenharmony_ci            break;
782498b56bSopenharmony_ci    }
792498b56bSopenharmony_ci    return level;
802498b56bSopenharmony_ci}
812498b56bSopenharmony_ci
822498b56bSopenharmony_cistd::optional<HilogMsgWrapper> KmsgParser::ParseKmsg(const std::vector<char>& kmsgBuffer)
832498b56bSopenharmony_ci{
842498b56bSopenharmony_ci    std::string kmsgStr(kmsgBuffer.data());
852498b56bSopenharmony_ci    std::string tagStr = "";
862498b56bSopenharmony_ci    size_t tagLen = tagStr.size();
872498b56bSopenharmony_ci    // Now build HilogMsg and insert it into buffer
882498b56bSopenharmony_ci    auto len = kmsgStr.size() + 1;
892498b56bSopenharmony_ci    auto msgLen = sizeof(HilogMsg) + tagLen + len + 1;
902498b56bSopenharmony_ci    HilogMsgWrapper msgWrap((std::vector<char>(msgLen, '\0')));
912498b56bSopenharmony_ci    HilogMsg& msg = msgWrap.GetHilogMsg();
922498b56bSopenharmony_ci    msg.len = msgLen;
932498b56bSopenharmony_ci    msg.tagLen = tagLen + 1;
942498b56bSopenharmony_ci    msg.type = LOG_KMSG;
952498b56bSopenharmony_ci    msg.level = KmsgLevelMap(LOG_INFO);
962498b56bSopenharmony_ci    struct timespec ts = {0};
972498b56bSopenharmony_ci    (void)clock_gettime(CLOCK_REALTIME, &ts);
982498b56bSopenharmony_ci    msg.tv_sec = static_cast<uint32_t>(ts.tv_sec);
992498b56bSopenharmony_ci    msg.tv_nsec = static_cast<uint32_t>(ts.tv_nsec);
1002498b56bSopenharmony_ci    if (strncpy_s(msg.tag, tagLen + 1, tagStr.c_str(), tagLen) != 0) {
1012498b56bSopenharmony_ci        return {};
1022498b56bSopenharmony_ci    }
1032498b56bSopenharmony_ci    if (strncpy_s(CONTENT_PTR((&msg)), MAX_LOG_LEN, kmsgStr.c_str(), len) != 0) {
1042498b56bSopenharmony_ci        return {};
1052498b56bSopenharmony_ci    }
1062498b56bSopenharmony_ci    return msgWrap;
1072498b56bSopenharmony_ci}
1082498b56bSopenharmony_ci} // namespace HiviewDFX
1092498b56bSopenharmony_ci} // namespace OHOS
110