1 /*
2 * Copyright (c) 2022 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 "daudio_log.h"
17
18 #include <cstdio>
19 #include <string>
20 #include "securec.h"
21
22 #ifdef HI_LOG_ENABLE
23 #include "hilog/log.h"
24 #else
25 #endif
26
27 namespace OHOS {
28 namespace DistributedHardware {
29 const std::string DAUDIO_LOG_TITLE_TAG = "DAUDIO";
30
DHLogOut(DHLogLevel logLevel,const char * logBuf)31 static void DHLogOut(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, DAUDIO_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
DHLog(DHLogLevel logLevel,const char * fmt,...)72 void DHLog(DHLogLevel logLevel, const char *fmt, ...)
73 {
74 constexpr int32_t logMaxLen = 4096;
75 char logBuf[logMaxLen] = {0};
76 va_list arg;
77 va_start(arg, fmt);
78
79 int32_t ret = vsprintf_s(logBuf, sizeof(logBuf), fmt, arg);
80 va_end(arg);
81 if (ret < 0) {
82 DHLogOut(logLevel, "DH log length error.");
83 return;
84 }
85 DHLogOut(logLevel, logBuf);
86 }
87 } // Distributedaudio
88 } // OHOS
89