1 /*
2  * Copyright (c) 2020-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 
16 #include "ace_log.h"
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <algorithm>
20 #if defined(TARGET_SIMULATOR) && (TARGET_SIMULATOR == 1)
21 #include <string>
22 
23 namespace OHOS {
24 namespace ACELite {
25 using namespace std;
ReplaceHiLogPrivacyKeyWords(string &original, const string &toBeReplaced, const string &targetStr)26 void ReplaceHiLogPrivacyKeyWords(string &original, const string &toBeReplaced, const string &targetStr)
27 {
28     string::size_type toBeReplacedLen = toBeReplaced.length();
29     do {
30         string::size_type foundPos = original.find(toBeReplaced);
31         if (foundPos == string::npos) {
32             // replace all end
33             break;
34         }
35         // replace to target
36         original.replace(foundPos, toBeReplacedLen, targetStr);
37     } while (1);
38 }
39 
PrintInfo(const char *format, va_list args)40 void PrintInfo(const char *format, va_list args)
41 {
42     string originalStr(format);
43     string toBeReplacedStr("{public}");
44     string emptyStr("");
45     ReplaceHiLogPrivacyKeyWords(originalStr, toBeReplacedStr, emptyStr);
46     vprintf(originalStr.c_str(), args);
47     printf("\n");
48 }
49 
HILOG_FATAL(HiLogModuleType mod, const char *msg, ...)50 void HILOG_FATAL(HiLogModuleType mod, const char *msg, ...)
51 {
52     (void)(mod);
53     printf("[ACELite][FATAL]:");
54     va_list args;
55     va_start(args, msg);
56     PrintInfo(msg, args);
57     va_end(args);
58 }
59 
HILOG_ERROR(HiLogModuleType mod, const char *msg, ...)60 void HILOG_ERROR(HiLogModuleType mod, const char *msg, ...)
61 {
62     (void)(mod);
63     printf("[ACELite][ERROR]:");
64     va_list args;
65     va_start(args, msg);
66     PrintInfo(msg, args);
67     va_end(args);
68 }
69 
HILOG_INFO(HiLogModuleType mod, const char *msg, ...)70 void HILOG_INFO(HiLogModuleType mod, const char *msg, ...)
71 {
72     (void)(mod);
73     printf("[ACELite][INFO]:");
74     va_list args;
75     va_start(args, msg);
76     PrintInfo(msg, args);
77     va_end(args);
78 }
79 
HILOG_WARN(HiLogModuleType mod, const char *msg, ...)80 void HILOG_WARN(HiLogModuleType mod, const char *msg, ...)
81 {
82     (void)(mod);
83     printf("[ACELite][WARN]:");
84     va_list args;
85     va_start(args, msg);
86     PrintInfo(msg, args);
87     va_end(args);
88 }
89 
HILOG_DEBUG(HiLogModuleType mod, const char *msg, ...)90 void HILOG_DEBUG(HiLogModuleType mod, const char *msg, ...)
91 {
92     (void)(mod);
93     printf("[ACELite][DEBUG]:");
94     va_list args;
95     va_start(args, msg);
96     PrintInfo(msg, args);
97     va_end(args);
98 }
99 } // namespace ACELite
100 } // namespace OHOS
101 #endif // TARGET_SIMULATOR
102 
103 #if defined(FEATURE_ACELITE_MC_LOG_PRINTF) && (FEATURE_ACELITE_MC_LOG_PRINTF == 1)
HILOG_CHARACTERS(const size_t size, const char* buffer)104 void HILOG_CHARACTERS(const size_t size, const char* buffer)
105 {
106     if (size == 0 || buffer == nullptr) {
107         return;
108     }
109 
110     const size_t len1 = 1;
111     const size_t len2 = 2;
112     const size_t len3 = 3;
113     const size_t len4 = 4;
114     const size_t len5 = 5;
115     const size_t chunkSize = 6;
116 
117     for (size_t index = 0; index < size; index += chunkSize) {
118         size_t len = std::min(size - index, chunkSize);
119         if (len == chunkSize) {
120             HILOG_ERROR(HILOG_MODULE_ACE, "%{public}c%{public}c%{public}c%{public}c%{public}c%{public}c",
121                 buffer[index], buffer[index + len1], buffer[index + len2],
122                 buffer[index + len3], buffer[index + len4], buffer[index + len5]);
123         } else if (len == len5) {
124             HILOG_ERROR(HILOG_MODULE_ACE, "%{public}c%{public}c%{public}c%{public}c%{public}c",
125                 buffer[index], buffer[index + len1], buffer[index + len2],
126                 buffer[index + len3], buffer[index + len4]);
127         } else if (len == len4) {
128             HILOG_ERROR(HILOG_MODULE_ACE, "%{public}c%{public}c%{public}c%{public}c",
129                 buffer[index], buffer[index + len1], buffer[index + len2], buffer[index + len3]);
130         } else if (len == len3) {
131             HILOG_ERROR(HILOG_MODULE_ACE, "%{public}c%{public}c%{public}c",
132                 buffer[index], buffer[index + len1], buffer[index + len2]);
133         } else if (len == len2) {
134             HILOG_ERROR(HILOG_MODULE_ACE, "%{public}c%{public}c", buffer[index], buffer[index + len1]);
135         } else if (len == len1) {
136             HILOG_ERROR(HILOG_MODULE_ACE, "%{public}c", buffer[index]);
137         }
138     }
139 }
140 #endif
141