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 "cm_log.h"
17
18 #include "securec.h"
19
20 #include "hilog/log.h"
21 #include "cm_mem.h"
22
23 #define MAX_LOG_BUFF_LEN 512
24
CmLog(uint32_t logLevel,const char * funcName,uint32_t lineNo,const char * format,...)25 void CmLog(uint32_t logLevel, const char *funcName, uint32_t lineNo, const char *format, ...)
26 {
27 char *buf = (char *)CmMalloc(MAX_LOG_BUFF_LEN);
28 if (buf == NULL) {
29 HILOG_ERROR(LOG_CORE, "certificate manager log malloc fail");
30 return;
31 }
32 (void)memset_s(buf, MAX_LOG_BUFF_LEN, 0, MAX_LOG_BUFF_LEN);
33
34 va_list ap;
35 va_start(ap, format);
36 int32_t ret = vsnprintf_s(buf, MAX_LOG_BUFF_LEN, MAX_LOG_BUFF_LEN - 1, format, ap);
37 va_end(ap);
38 if (ret < 0) {
39 HILOG_ERROR(LOG_CORE, "certificate manager log concatenate error.");
40 CM_FREE_PTR(buf);
41 return;
42 }
43
44 switch (logLevel) {
45 case CM_LOG_LEVEL_I:
46 HILOG_INFO(LOG_CORE, "%{public}s[%{public}u]: %{public}s\n", funcName, lineNo, buf);
47 break;
48 case CM_LOG_LEVEL_E:
49 HILOG_ERROR(LOG_CORE, "%{public}s[%{public}u]: %{public}s\n", funcName, lineNo, buf);
50 break;
51 case CM_LOG_LEVEL_W:
52 HILOG_WARN(LOG_CORE, "%{public}s[%{public}u]: %{public}s\n", funcName, lineNo, buf);
53 break;
54 case CM_LOG_LEVEL_D:
55 HILOG_DEBUG(LOG_CORE, "%{public}s[%{public}u]: %{private}s\n", funcName, lineNo, buf);
56 break;
57 default:
58 CM_FREE_PTR(buf);
59 return;
60 }
61
62 CM_FREE_PTR(buf);
63 }
64