• 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 #include "faultlogger_client_test.h"
16 
17 #include <securec.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 
21 #include <cinttypes>
22 #include <mutex>
23 #include <string>
24 
25 #include "file_util.h"
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace {
29 constexpr int DEFAULT_BUFFER_SIZE = 64;
30 constexpr uint64_t TIME_RATIO = 1000;
31 
GetFormatedTime(uint64_t target)32 std::string GetFormatedTime(uint64_t target)
33 {
34     time_t now = time(nullptr);
35     if (target > static_cast<uint64_t>(now)) {
36         target = target / TIME_RATIO; // 1000 : convert millisecond to seconds
37     }
38 
39     time_t out = static_cast<time_t>(target);
40     struct tm tmStruct {0};
41     struct tm* timeInfo = localtime_r(&out, &tmStruct);
42     if (timeInfo == nullptr) {
43         return "00000000000000";
44     }
45 
46     char buf[DEFAULT_BUFFER_SIZE] = {0};
47     int charsWritten = strftime(buf, DEFAULT_BUFFER_SIZE - 1, "%Y%m%d%H%M%S", timeInfo);
48     if ((charsWritten < 0) || (charsWritten >= DEFAULT_BUFFER_SIZE - 1)) {
49         return "00000000000000";
50     }
51     return std::string(buf, strlen(buf));
52 }
53 
GetFormatedTimeWithMillsec(uint64_t time)54 std::string GetFormatedTimeWithMillsec(uint64_t time)
55 {
56     char millBuf[DEFAULT_BUFFER_SIZE] = {0};
57     int ret = snprintf_s(millBuf, sizeof(millBuf), sizeof(millBuf) - 1, "%03lu", time % TIME_RATIO);
58     if (ret <= 0) {
59         return GetFormatedTime(time) + "000";
60     }
61     std::string millStr(millBuf);
62     return GetFormatedTime(time) + millStr;
63 }
64 } // namespace
65 
GetFaultLogName(const time_t & time,int32_t id,const std::string & type,const std::string & module)66 std::string GetFaultLogName(const time_t& time, int32_t id, const std::string& type, const std::string& module)
67 {
68     static std::mutex localMutex;
69     std::lock_guard<std::mutex> lock(localMutex);
70     auto fileName = type + "-" + module + "-" + std::to_string(id) + "-" + GetFormatedTimeWithMillsec(time) + ".log";
71     return fileName;
72 }
73 
CreateFaultLogInfo(const time_t & time,int32_t id,int32_t type,const std::string & module)74 FaultLogInfoInner CreateFaultLogInfo(const time_t& time, int32_t id, int32_t type, const std::string& module)
75 {
76     FaultLogInfoInner info;
77     info.time = time;
78     info.id = id;
79     info.pid = getpid();
80     info.faultLogType = type;
81     info.module = module;
82     info.summary = "faultlogger_client_test";
83     info.sectionMaps["APPVERSION"] = "1.0";
84     info.sectionMaps["FAULT_MESSAGE"] = "Nullpointer";
85     info.sectionMaps["TRACEID"] = "0x1646145645646";
86     info.sectionMaps["KEY_THREAD_INFO"] = "Test Thread Info";
87     info.sectionMaps["REASON"] = "TestReason";
88     info.sectionMaps["STACKTRACE"] = "#01 xxxxxx\n#02 xxxxxx\n";
89     return info;
90 }
91 
CheckLogFileExist(const time_t & time,int32_t id,const std::string & type,const std::string & module)92 bool CheckLogFileExist(const time_t& time, int32_t id, const std::string& type, const std::string& module)
93 {
94     auto fileName = GetFaultLogName(time, id, type, module);
95     auto path = "/data/log/faultlog/faultlogger/" + fileName;
96     return FileUtil::FileExists(path);
97 }
98 } // namespace HiviewDFX
99 } // namespace OHOS