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