• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 #include "running_status_logger.h"
17 
18 #include "hiview_logger.h"
19 #include "parameter_ex.h"
20 
21 namespace OHOS {
22 namespace HiviewDFX {
23 DEFINE_LOG_TAG("HiView-RunningStatusLogger");
24 namespace {
25 constexpr size_t CNT_STATISTIC_FILE_MAX_CNT = 3;
26 constexpr size_t EVENT_LOG_FILE_MAX_CNT = 20;
27 constexpr size_t RUNNING_STATUS_FILE_MAX_CNT = 10;
28 constexpr uint64_t CNT_STATISTIC_FILE_SIZE_LIMIT = 1024 * 1024;
29 constexpr uint64_t EVENT_LOG_FILE_SIZE_LIMIT = 10 * 1024 * 1024;
30 constexpr uint64_t RUNNING_STATUS_FILE_SIZE_LIMIT = 2 * 1024 * 1024;
31 }
32 
LogEventCountStatisticInfo(const std::string & logInfo)33 void RunningStatusLogger::LogEventCountStatisticInfo(const std::string& logInfo)
34 {
35     LogStrategy strategy { "event_count_statistic", CNT_STATISTIC_FILE_MAX_CNT, CNT_STATISTIC_FILE_SIZE_LIMIT };
36     auto logFileWriter = GetLogFileWriter(strategy);
37     logFileWriter->Write(logInfo);
38 }
39 
LogEventRunningLogInfo(const std::string & logInfo)40 void RunningStatusLogger::LogEventRunningLogInfo(const std::string& logInfo)
41 {
42     if (!Parameter::IsBetaVersion()) {
43         HIVIEW_LOGD("Do not write files on the commercial version.");
44         return;
45     }
46     LogStrategy strategy { "event_running_log", EVENT_LOG_FILE_MAX_CNT, EVENT_LOG_FILE_SIZE_LIMIT };
47     auto logFileWriter = GetLogFileWriter(strategy);
48     logFileWriter->Write(logInfo);
49 }
50 
LogRunningStatusInfo(const std::string & logInfo)51 void RunningStatusLogger::LogRunningStatusInfo(const std::string& logInfo)
52 {
53     if (!Parameter::IsBetaVersion()) {
54         HIVIEW_LOGD("Do not write files on the commercial version.");
55         return;
56     }
57     LogStrategy strategy { "runningstatus", RUNNING_STATUS_FILE_MAX_CNT, RUNNING_STATUS_FILE_SIZE_LIMIT };
58     auto logFileWriter = GetLogFileWriter(strategy);
59     logFileWriter->Write(logInfo);
60 }
61 
GetLogFileWriter(const LogStrategy & strategy)62 std::shared_ptr<LogFileWriter> RunningStatusLogger::GetLogFileWriter(const LogStrategy& strategy)
63 {
64     std::lock_guard<std::mutex> lock(logMutex_);
65     auto iter = allWriters_.find(strategy.fileNamePrefix);
66     if (iter == allWriters_.end()) {
67         auto fileWriter = std::make_shared<LogFileWriter>(strategy);
68         allWriters_.emplace(strategy.fileNamePrefix, fileWriter);
69         return fileWriter;
70     }
71     return iter->second;
72 }
73 } // namespace HiviewDFX
74 } // namespace OHOS