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