log.h 1.7 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef KERNEL_LITE_LOG
#define KERNEL_LITE_LOG

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <stdio.h>
#include <unistd.h>
#include <time.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifdef DEBUG
M
mamingshuai 已提交
32 33 34 35 36 37 38 39 40 41
    const float SYS_NS_PER_S = 1000 * 1000 * 1000;
    // get current time, for logging only
    static float GetCurTime(void)
    {
        struct timespec time1 = {0, 0};
        clock_gettime(CLOCK_MONOTONIC, &time1);
        return time1.tv_sec + ((float)time1.tv_nsec) / SYS_NS_PER_S;
    }
    #define LOGD(format, ...) fprintf(stdout, "[%.06f] " format "\n", GetCurTime(), ##__VA_ARGS__)
    #define LOG(format, ...)  fprintf(stdout, "[%.06f] " format "\n", GetCurTime(), ##__VA_ARGS__)
W
wenjun 已提交
42
#else
M
mamingshuai 已提交
43 44
    #define LOGD(...)
    #define LOG(format, ...)  fprintf(stdout, format "\n", ##__VA_ARGS__)
W
wenjun 已提交
45 46
#endif

M
mamingshuai 已提交
47
#define LOGE(format, ...)  fprintf(stdout,  "\n%s:%d: " format "\n", __FILE__, __LINE__, ##__VA_ARGS__)
W
wenjun 已提交
48 49 50 51 52 53 54 55 56 57 58 59

#define PANIC(format, ...)   do {     \
        LOGE(format, ##__VA_ARGS__);  \
        exit(1);                      \
    } while (0)


#ifdef __cplusplus
}
#endif

#endif