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