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 #include <string>
16 #include <vector>
17
18 #include "interop-logging.h"
19
20 namespace {
21
22 struct Log {
23 std::string log;
24 bool isActive = true;
25 };
26
27 std::vector<Log*> groupedLogs;
28
startGroupedLog(int index)29 void startGroupedLog(int index) {
30 if (index >= (int)groupedLogs.size()) {
31 groupedLogs.resize(index + 1);
32 for (int i = 0; i <= index; i++) {
33 if (!groupedLogs[i]) groupedLogs[i] = new Log();
34 }
35 }
36 groupedLogs[index]->isActive = true;
37 groupedLogs[index]->log.clear();
38 }
39
stopGroupedLog(int index)40 void stopGroupedLog(int index) {
41 if (index < (int)groupedLogs.size()) {
42 groupedLogs[index]->isActive = false;
43 }
44 }
45
appendGroupedLog(int index,const char * str)46 void appendGroupedLog(int index, const char* str) {
47 if (index < (int)groupedLogs.size()) {
48 groupedLogs[index]->log.append(str);
49 }
50 }
51
getGroupedLog(int index)52 const char* getGroupedLog(int index) {
53 if (index < (int)groupedLogs.size()) {
54 auto result = groupedLogs[index]->log.c_str();
55 return result;
56 }
57 return "";
58 }
59
needGroupedLog(int index)60 int needGroupedLog(int index) {
61 if (index < (int)groupedLogs.size()) {
62 return groupedLogs[index]->isActive;
63 }
64 return 0;
65 }
66
67 const GroupLogger defaultInstance = {
68 startGroupedLog,
69 stopGroupedLog,
70 appendGroupedLog,
71 getGroupedLog,
72 needGroupedLog,
73 };
74
75 } // namespace
76
GetDefaultLogger()77 const GroupLogger* GetDefaultLogger() {
78 return &defaultInstance;
79 }