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 "daudio_constants.h"
19 #include "securec.h"
20
21 #ifdef HI_LOG_ENABLE
22 #include "hilog/log.h"
23 #else
24 #include <cstdio>
25 #endif
26
27 namespace OHOS {
28 namespace DistributedHardware {
DHLogOut(DHLogLevel logLevel,const char * logBuf)29 static void DHLogOut(DHLogLevel logLevel, const char *logBuf)
30 {
31 #ifdef HI_LOG_ENABLE
32 LogLevel hiLogLevel = LOG_INFO;
33 switch (logLevel) {
34 case DH_LOG_DEBUG:
35 hiLogLevel = LOG_DEBUG;
36 break;
37 case DH_LOG_INFO:
38 hiLogLevel = LOG_INFO;
39 break;
40 case DH_LOG_WARN:
41 hiLogLevel = LOG_WARN;
42 break;
43 case DH_LOG_ERROR:
44 hiLogLevel = LOG_ERROR;
45 break;
46 default:
47 break;
48 }
49 (void)HiLogPrint(LOG_CORE, hiLogLevel, LOG_DOMAIN, DAUDIO_LOG_TITLE_TAG.c_str(), "%{public}s", logBuf);
50 #else
51 switch (logLevel) {
52 case DH_LOG_DEBUG:
53 printf("[D]%s\n", logBuf);
54 break;
55 case DH_LOG_INFO:
56 printf("[I]%s\n", logBuf);
57 break;
58 case DH_LOG_WARN:
59 printf("[W]%s\n", logBuf);
60 break;
61 case DH_LOG_ERROR:
62 printf("[E]%s\n", logBuf);
63 break;
64 default:
65 break;
66 }
67 #endif
68 }
69
DHLog(DHLogLevel logLevel,const char * fmt,...)70 void DHLog(DHLogLevel logLevel, const char *fmt, ...)
71 {
72 char logBuf[LOG_MAX_LEN] = {0};
73 va_list arg;
74 va_start(arg, fmt);
75
76 int32_t ret = vsprintf_s(logBuf, sizeof(logBuf), fmt, arg);
77 va_end(arg);
78 if (ret < 0) {
79 DHLogOut(logLevel, "DH log length error.");
80 return;
81 }
82 DHLogOut(logLevel, logBuf);
83 }
84 } // namespace DistributedHardware
85 } // namespace OHOS
86