• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "av_trans_log.h"
17 
18 #include "securec.h"
19 
20 #ifdef HI_LOG_ENABLE
21 #include "hilog/log.h"
22 #else
23 #include <cstdio>
24 #endif
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 const std::string DAVTRANS_LOG_TITLE_TAG = "DAVTRANS";
29 constexpr int32_t AV_LOG_MAX_LEN = 4096;
30 
AVTransLogOut(DHLogLevel logLevel,const char * logBuf)31 static void AVTransLogOut(DHLogLevel logLevel, const char *logBuf)
32 {
33 #ifdef HI_LOG_ENABLE
34     LogLevel hiLogLevel = LOG_INFO;
35     switch (logLevel) {
36         case DH_LOG_DEBUG:
37             hiLogLevel = LOG_DEBUG;
38             break;
39         case DH_LOG_INFO:
40             hiLogLevel = LOG_INFO;
41             break;
42         case DH_LOG_WARN:
43             hiLogLevel = LOG_WARN;
44             break;
45         case DH_LOG_ERROR:
46             hiLogLevel = LOG_ERROR;
47             break;
48         default:
49             break;
50     }
51     (void)HiLogPrint(LOG_CORE, hiLogLevel, LOG_DOMAIN, DAVTRANS_LOG_TITLE_TAG.c_str(), "%{public}s", logBuf);
52 #else
53     switch (logLevel) {
54         case DH_LOG_DEBUG:
55             printf("[D]%s\n", logBuf);
56             break;
57         case DH_LOG_INFO:
58             printf("[I]%s\n", logBuf);
59             break;
60         case DH_LOG_WARN:
61             printf("[W]%s\n", logBuf);
62             break;
63         case DH_LOG_ERROR:
64             printf("[E]%s\n", logBuf);
65             break;
66         default:
67             break;
68     }
69 #endif
70 }
71 
AVTransLog(DHLogLevel logLevel,const char * fmt,...)72 void AVTransLog(DHLogLevel logLevel, const char *fmt, ...)
73 {
74     char logBuf[AV_LOG_MAX_LEN] = {0};
75     va_list arg;
76 
77     (void)memset_s(&arg, sizeof(va_list), 0, sizeof(va_list));
78     va_start(arg, fmt);
79     int32_t ret = vsprintf_s(logBuf, sizeof(logBuf), fmt, arg);
80     va_end(arg);
81     if (ret < 0) {
82         AVTransLogOut(logLevel, "AVTrans log length error.");
83         return;
84     }
85     AVTransLogOut(logLevel, logBuf);
86 }
87 
GetAnonyString(const std::string & value)88 std::string GetAnonyString(const std::string &value)
89 {
90     constexpr size_t INT32_SHORT_ID_LENGTH = 20;
91     constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
92     constexpr size_t INT32_MIN_ID_LENGTH = 3;
93     std::string res;
94     std::string tmpStr("******");
95     size_t strLen = value.length();
96     if (strLen < INT32_MIN_ID_LENGTH) {
97         return tmpStr;
98     }
99 
100     if (strLen <= INT32_SHORT_ID_LENGTH) {
101         res += value[0];
102         res += tmpStr;
103         res += value[strLen - 1];
104     } else {
105         res.append(value, 0, INT32_PLAINTEXT_LENGTH);
106         res += tmpStr;
107         res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
108     }
109 
110     return res;
111 }
112 
GetAnonyInt32(const int32_t value)113 std::string GetAnonyInt32(const int32_t value)
114 {
115     constexpr int32_t INT32_STRING_LENGTH = 40;
116     char tempBuffer[INT32_STRING_LENGTH] = "";
117     int32_t secRet = sprintf_s(tempBuffer, INT32_STRING_LENGTH, "%d", value);
118     if (secRet <= 0) {
119         std::string nullString("");
120         return nullString;
121     }
122     size_t length = strlen(tempBuffer);
123     for (size_t i = 1; i <= length - 1; i++) {
124         tempBuffer[i] = '*';
125     }
126     if (length == 0x01) {
127         tempBuffer[0] = '*';
128     }
129 
130     std::string tempString(tempBuffer);
131     return tempString;
132 }
133 } // namespace DistributedHardware
134 } // namespace OHOS