• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef KERNEL_LITE_LOG
17 #define KERNEL_LITE_LOG
18 
19 #include <cstdio>
20 #include <unistd.h>
21 #include <ctime>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #ifdef DEBUG
28     const float SYS_NS_PER_S = 1000 * 1000 * 1000;
29     // get current time, for logging only
GetCurTime(void)30     static float GetCurTime(void)
31     {
32         struct timespec time1 = {0, 0};
33         clock_gettime(CLOCK_MONOTONIC, &time1);
34         return time1.tv_sec + ((float)time1.tv_nsec) / SYS_NS_PER_S;
35     }
36     #define LOGD(format, ...) fprintf(stdout, "[%.06f] " format "\n", GetCurTime(), ##__VA_ARGS__)
37     #define LOG(format, ...)  fprintf(stdout, "[%.06f] " format "\n", GetCurTime(), ##__VA_ARGS__)
38 #else
39     #define LOGD(...)
40     #define LOG(format, ...)  fprintf(stdout, format "\n", ##__VA_ARGS__)
41 #endif
42 
43 #define LOGE(format, ...)  fprintf(stdout,  "\n%s:%d: " format "\n", __FILE__, __LINE__, ##__VA_ARGS__)
44 
45 #define PANIC(format, ...)   do {     \
46         LOGE(format, ##__VA_ARGS__);  \
47         exit(1);                      \
48     } while (0)
49 
50 
51 #ifdef __cplusplus
52 }
53 #endif
54 
55 #endif
56