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