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#include "faultlogger_client_test.h"
16
17#include <sys/types.h>
18#include <unistd.h>
19
20#include <cinttypes>
21#include <mutex>
22#include <string>
23
24#include "file_util.h"
25namespace OHOS {
26namespace HiviewDFX {
27std::string GetFaultLogName(const time_t& time, int32_t id, const std::string& type, const std::string& module)
28{
29    static std::mutex localMutex;
30    std::lock_guard<std::mutex> lock(localMutex);
31    const int32_t bufLen = 64;
32    struct tm* timeInfo = localtime(&time);
33    if (timeInfo == nullptr) {
34        return "";
35    }
36
37    char buf[bufLen] = {0};
38    strftime(buf, bufLen - 1, "%Y%m%d%H%M%S", timeInfo);
39    auto fileName = type + "-" + module + "-" + std::to_string(id) + "-" + std::string(buf, strlen(buf));
40    return fileName;
41}
42
43FaultLogInfoInner CreateFaultLogInfo(const time_t& time, int32_t id, int32_t type, const std::string& module)
44{
45    FaultLogInfoInner info;
46    info.time = time;
47    info.id = id;
48    info.pid = getpid();
49    info.faultLogType = type;
50    info.module = module;
51    info.summary = "faultlogger_client_test";
52    info.sectionMaps["APPVERSION"] = "1.0";
53    info.sectionMaps["FAULT_MESSAGE"] = "Nullpointer";
54    info.sectionMaps["TRACEID"] = "0x1646145645646";
55    info.sectionMaps["KEY_THREAD_INFO"] = "Test Thread Info";
56    info.sectionMaps["REASON"] = "TestReason";
57    info.sectionMaps["STACKTRACE"] = "#01 xxxxxx\n#02 xxxxxx\n";
58    return info;
59}
60
61bool CheckLogFileExist(const time_t& time, int32_t id, const std::string& type, const std::string& module)
62{
63    auto fileName = GetFaultLogName(time, id, type, module);
64    auto path = "/data/log/faultlog/faultlogger/" + fileName;
65    return FileUtil::FileExists(path);
66}
67} // namespace HiviewDFX
68} // namespace OHOS