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