1 /*
2 * Copyright (c) 2020-2021 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 "hks_test_log.h"
17
18 #include "securec.h"
19
20 #include "stdlib.h"
21
22 #include "time.h"
23
24 #define MAX_LOG_BUFF_LEN 512
25 #define WAIT_TO_LOG_DONE 100000
26
HksTestLog(uint32_t logLevel,const char * funcName,int32_t lineNo,const char * format,...)27 void HksTestLog(uint32_t logLevel, const char *funcName, int32_t lineNo, const char *format, ...)
28 {
29 char buf[MAX_LOG_BUFF_LEN] = { 0 };
30
31 va_list ap;
32 va_start(ap, format);
33 int32_t ret = vsnprintf_s(buf, MAX_LOG_BUFF_LEN, MAX_LOG_BUFF_LEN - 1, format, ap);
34 va_end(ap);
35 if (ret < 0) {
36 printf("hks log concatenate error.");
37 return;
38 }
39
40 switch (logLevel) {
41 case HKS_LOG_LEVEL_I:
42 printf("[INFO] %s [%d]: %s\n", funcName, lineNo, buf);
43 break;
44 case HKS_LOG_LEVEL_E:
45 printf("[ERROR] %s [%d]: %s\n", funcName, lineNo, buf);
46 break;
47 case HKS_LOG_LEVEL_W:
48 printf("[WARNING] %s [%d]: %s\n", funcName, lineNo, buf);
49 break;
50 case HKS_LOG_LEVEL_D:
51 printf("[DEBUG] %s [%d]: %s\n", funcName, lineNo, buf);
52 break;
53 default:
54 return;
55 }
56
57 usleep(WAIT_TO_LOG_DONE);
58 }
59
HksAssertLog(bool test)60 void HksAssertLog(bool test)
61 {
62 if (!(test)) {
63 HKS_TEST_LOG_E("[ASSERT] :fail\n");
64 }
65 }
66