• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 #include "hitls_build.h"
17 #ifdef HITLS_BSL_LOG
18 
19 #include <stdbool.h>
20 #include "securec.h"
21 #include "bsl_errno.h"
22 #include "bsl_log.h"
23 #include "bsl_log_internal.h"
24 
25 /* string of HiTLS version */
26 static char g_openHiTLSVersion[HITLS_VERSION_LEN] = OPENHITLS_VERSION_S;
27 static uint64_t g_openHiTLSNumVersion = OPENHITLS_VERSION_I;
28 
BSL_LOG_GetVersion(char * version,uint32_t * versionLen)29 int32_t BSL_LOG_GetVersion(char *version, uint32_t *versionLen)
30 {
31     if (version == NULL || versionLen == NULL) {
32         return BSL_LOG_ERR_BAD_PARAM;
33     }
34 
35     if (*versionLen < HITLS_VERSION_LEN) {
36         return BSL_LOG_ERR_BAD_PARAM;
37     }
38 
39     uint32_t len = (uint32_t)strlen(g_openHiTLSVersion);
40     if (memcpy_s(version, *versionLen, g_openHiTLSVersion, len) != EOK) {
41         return BSL_MEMCPY_FAIL;
42     }
43 
44     *versionLen = len;
45     return BSL_SUCCESS;
46 }
47 
BSL_LOG_GetVersionNum(void)48 uint64_t BSL_LOG_GetVersionNum(void)
49 {
50     return g_openHiTLSNumVersion;
51 }
52 
53 static BSL_LOG_BinLogFixLenFunc g_fixLenFunc = NULL;
54 static BSL_LOG_BinLogVarLenFunc g_varLenFunc = NULL;
55 static uint32_t g_binlogLevel = BSL_LOG_LEVEL_ERR; // error-level is enabled by default
56 static uint32_t g_binlogType = BSL_LOG_BINLOG_TYPE_RUN; // type run is enabled by default, other types can be added.
57 
BSL_LOG_RegBinLogFunc(const BSL_LOG_BinLogFuncs * funcs)58 int32_t BSL_LOG_RegBinLogFunc(const BSL_LOG_BinLogFuncs *funcs)
59 {
60     bool invalid = funcs == NULL || funcs->fixLenFunc == NULL || funcs->varLenFunc == NULL;
61     if (invalid) {
62         return BSL_NULL_INPUT;
63     }
64     g_fixLenFunc = funcs->fixLenFunc;
65     g_varLenFunc = funcs->varLenFunc;
66     return BSL_SUCCESS;
67 }
68 
BSL_LOG_SetBinLogLevel(uint32_t level)69 int32_t BSL_LOG_SetBinLogLevel(uint32_t level)
70 {
71     if (level > BSL_LOG_LEVEL_DEBUG) {
72         return BSL_LOG_ERR_BAD_PARAM;
73     }
74     g_binlogLevel = level;
75     return BSL_SUCCESS;
76 }
77 
BSL_LOG_GetBinLogLevel(void)78 uint32_t BSL_LOG_GetBinLogLevel(void)
79 {
80     return g_binlogLevel;
81 }
82 
BSL_LOG_BinLogFixLen(uint32_t logId,uint32_t logLevel,uint32_t logType,void * format,void * para1,void * para2,void * para3,void * para4)83 void BSL_LOG_BinLogFixLen(uint32_t logId, uint32_t logLevel, uint32_t logType,
84     void *format, void *para1, void *para2, void *para3, void *para4)
85 {
86     bool invalid = (logLevel > g_binlogLevel) || ((logType & g_binlogType) == 0) || (g_fixLenFunc == NULL);
87     if (!invalid) {
88         g_fixLenFunc(logId, logLevel, logType, format, para1, para2, para3, para4);
89     }
90 }
91 
BSL_LOG_BinLogVarLen(uint32_t logId,uint32_t logLevel,uint32_t logType,void * format,void * para)92 void BSL_LOG_BinLogVarLen(uint32_t logId, uint32_t logLevel, uint32_t logType, void *format, void *para)
93 {
94     bool invalid = (logLevel > g_binlogLevel) || ((logType & g_binlogType) == 0) || (g_varLenFunc == NULL);
95     if (!invalid) {
96         g_varLenFunc(logId, logLevel, logType, format, para);
97     }
98 }
99 #endif /* HITLS_BSL_LOG */
100