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