1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci#ifndef KERNEL_LITE_LOG
17f6603c60Sopenharmony_ci#define KERNEL_LITE_LOG
18f6603c60Sopenharmony_ci
19f6603c60Sopenharmony_ci#ifndef _GNU_SOURCE
20f6603c60Sopenharmony_ci#define _GNU_SOURCE
21f6603c60Sopenharmony_ci#endif
22f6603c60Sopenharmony_ci
23f6603c60Sopenharmony_ci#include <stdio.h>
24f6603c60Sopenharmony_ci#include <unistd.h>
25f6603c60Sopenharmony_ci#include <time.h>
26f6603c60Sopenharmony_ci
27f6603c60Sopenharmony_ci#ifdef __cplusplus
28f6603c60Sopenharmony_ciextern "C" {
29f6603c60Sopenharmony_ci#endif
30f6603c60Sopenharmony_ci
31f6603c60Sopenharmony_ci#ifdef DEBUG
32f6603c60Sopenharmony_ci    const float SYS_NS_PER_S = 1000 * 1000 * 1000;
33f6603c60Sopenharmony_ci    // get current time, for logging only
34f6603c60Sopenharmony_ci    static float GetCurTime(void)
35f6603c60Sopenharmony_ci    {
36f6603c60Sopenharmony_ci        struct timespec time1 = {0, 0};
37f6603c60Sopenharmony_ci        clock_gettime(CLOCK_MONOTONIC, &time1);
38f6603c60Sopenharmony_ci        return time1.tv_sec + ((float)time1.tv_nsec) / SYS_NS_PER_S;
39f6603c60Sopenharmony_ci    }
40f6603c60Sopenharmony_ci    #define LOGD(format, ...) fprintf(stdout, "[%.06f] " format "\n", GetCurTime(), ##__VA_ARGS__)
41f6603c60Sopenharmony_ci    #define LOG(format, ...)  fprintf(stdout, "[%.06f] " format "\n", GetCurTime(), ##__VA_ARGS__)
42f6603c60Sopenharmony_ci#else
43f6603c60Sopenharmony_ci    #define LOGD(...)
44f6603c60Sopenharmony_ci    #define LOG(format, ...)  fprintf(stdout, format "\n", ##__VA_ARGS__)
45f6603c60Sopenharmony_ci#endif
46f6603c60Sopenharmony_ci
47f6603c60Sopenharmony_ci#define LOGE(format, ...)  fprintf(stdout,  "\n%s:%d: " format "\n", __FILE__, __LINE__, ##__VA_ARGS__)
48f6603c60Sopenharmony_ci
49f6603c60Sopenharmony_ci#define PANIC(format, ...)   do {     \
50f6603c60Sopenharmony_ci        LOGE(format, ##__VA_ARGS__);  \
51f6603c60Sopenharmony_ci        exit(1);                      \
52f6603c60Sopenharmony_ci    } while (0)
53f6603c60Sopenharmony_ci
54f6603c60Sopenharmony_ci
55f6603c60Sopenharmony_ci#ifdef __cplusplus
56f6603c60Sopenharmony_ci}
57f6603c60Sopenharmony_ci#endif
58f6603c60Sopenharmony_ci
59f6603c60Sopenharmony_ci#endif
60