• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "batch_log_printer.h"
17 #include "res_common_util.h"
18 #include "res_sched_log.h"
19 
20 namespace OHOS {
21 namespace ResourceSchedule {
22 namespace {
23     constexpr char QUEUE_NAME[] = "batch_log_printer_queue";
24     constexpr int32_t PRINT_SIZE = 64;
25     constexpr int32_t BATCH_SIZE = 3000;
26     constexpr int64_t PRINT_DUATION = 10 * 1000;
27 }
28 
29 IMPLEMENT_SINGLE_INSTANCE(BatchLogPrinter);
30 
BatchLogPrinter()31 BatchLogPrinter::BatchLogPrinter()
32 {
33     logQueue_ = std::make_shared<ffrt::queue>(QUEUE_NAME, ffrt::queue_attr().qos(ffrt::qos_default));
34     lastPrintTimestamp_ = ResCommonUtil::GetNowMillTime(true);
35 }
36 
SubmitLog(const std::string & log)37 void BatchLogPrinter::SubmitLog(const std::string& log)
38 {
39     if (logQueue_ == nullptr) {
40         return;
41     }
42     int64_t timestamp = ResCommonUtil::GetCurrentTimestamp();
43     logQueue_->submit([log, timestamp]() {
44         BatchLogPrinter::GetInstance().RecordLog(log, timestamp);
45     });
46 }
47 
RecordLog(const std::string & log,const int64_t & timestamp)48 void BatchLogPrinter::RecordLog(const std::string& log, const int64_t& timestamp)
49 {
50     std::string recordLog = ResCommonUtil::ConvertTimestampToStr(timestamp) + ":" + log;
51     allLogs_.push_back(recordLog);
52     auto currentTImestamp = ResCommonUtil::GetNowMillTime(true);
53     if (allLogs_.size() >= PRINT_SIZE || currentTImestamp - lastPrintTimestamp_ >= PRINT_DUATION) {
54         std::vector<std::string> batchLogs = std::vector<std::string>();
55         GetBatch(batchLogs);
56         for (auto& item : batchLogs) {
57             RESSCHED_LOGI("BatchLog:%{public}s", item.c_str());
58         }
59         lastPrintTimestamp_ = currentTImestamp;
60     }
61 }
62 
GetBatch(std::vector<std::string> & batchLogs)63 void BatchLogPrinter::GetBatch(std::vector<std::string>& batchLogs)
64 {
65     std::string batch;
66     for (auto& item : allLogs_) {
67         if (batch.length() + item.length() > BATCH_SIZE) {
68             batchLogs.push_back(std::move(batch));
69             batch = "";
70         }
71         if (batch.length() != 0) {
72             batch.append("|");
73         }
74         batch.append(item);
75     }
76     if (!batch.empty()) {
77         batchLogs.push_back(std::move(batch));
78     }
79     allLogs_.clear();
80 }
81 } // namespace ResourceSchedule
82 } // namespace OHOS