1 /*
2  * Copyright (c) 2022-2023 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 "cipher_log.h"
17 
18 #include "securec.h"
19 
20 #include "hilog/log.h"
21 
22 #define MAX_LOG_BUFF_LEN 4096
23 
CipherLog(uint32_t logLevel,const char * funcName,uint32_t lineNo,const char * format,...)24 void CipherLog(uint32_t logLevel, const char *funcName, uint32_t lineNo, const char *format, ...)
25 {
26     char *buf = (char *)malloc(MAX_LOG_BUFF_LEN);
27     if (buf == NULL) {
28         HILOG_ERROR(LOG_CORE, "cipher log malloc fail");
29         return;
30     }
31     (void)memset_s(buf, MAX_LOG_BUFF_LEN, 0, MAX_LOG_BUFF_LEN);
32 
33     va_list ap;
34     va_start(ap, format);
35     int32_t ret = vsnprintf_s(buf, MAX_LOG_BUFF_LEN, MAX_LOG_BUFF_LEN - 1, format, ap);
36     va_end(ap);
37     if (ret < 0) {
38         HILOG_ERROR(LOG_CORE, "cipher log concatenate error.");
39         free(buf);
40         buf = NULL;
41         return;
42     }
43 
44     switch (logLevel) {
45         case CIPHER_LOG_LEVEL_E:
46             HILOG_ERROR(LOG_CORE, "%{public}s[%{public}u]: %{public}s\n", funcName, lineNo, buf);
47             break;
48         default:
49             free(buf);
50             buf = NULL;
51             return;
52     }
53 
54     free(buf);
55     buf = NULL;
56 }