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 "dinput_log.h"
17
18 #include "constants_dinput.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 {
29 namespace DistributedInput {
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, DINPUT_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 va_start(arg, fmt);
76
77 int32_t ret = vsprintf_s(logBuf, sizeof(logBuf), fmt, arg);
78 va_end(arg);
79 if (ret < 0) {
80 DHLogOut(logLevel, "DH log length error.");
81 return;
82 }
83 DHLogOut(logLevel, logBuf);
84 }
85 } // namespace DistributedInput
86 } // namespace DistributedHardware
87 } // namespace OHOS
88