add hal-tdm-log.c file
[platform/hal/api/tdm.git] / src / common.h
1 /**************************************************************************
2  *
3  * hal-api-tdm
4  *
5  * Copyright 2021 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: SooChan Lim <sc1.lim@samsung.com>,
8  *          Junkyeong Kim <jk0430.kim@samsung.com>,
9  *          Shawn Lee <shiin.lee@samsung.com>,
10  *          Changyeon Lee <cyeon.lee@samsung.com>,
11  *          Joonbum Ko <joonbum.ko@samsung.com>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sub license, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice (including the
22  * next paragraph) shall be included in all copies or substantial portions
23  * of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
28  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
29  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
30  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
31  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32  *
33 **************************************************************************/
34
35 #ifndef __COMMON_H__
36 #define __COMMON_H__
37
38 #include <sys/syscall.h>
39 #include <time.h>
40 #include <dlog.h>
41
42 /* log level */
43 void hal_tdm_log_print(int level, const char *fmt, ...);
44
45 #define HAL_TDM_DBG(fmt, args...) \
46         do { \
47                 struct timespec ts; \
48                 clock_gettime(CLOCK_MONOTONIC, &ts); \
49                 hal_tdm_log_print(HAL_TDM_LOG_LEVEL_DBG, "[%5d.%06d][%d][%s %d]"fmt, \
50                                           (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
51                                           (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
52         } while (0)
53
54 #define HAL_TDM_INFO(fmt, args...) \
55         do { \
56                 struct timespec ts; \
57                 clock_gettime(CLOCK_MONOTONIC, &ts); \
58                 hal_tdm_log_print(HAL_TDM_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d]"fmt, \
59                                           (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
60                                           (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
61         } while (0)
62
63 #define HAL_TDM_WRN(fmt, args...) \
64         do { \
65                 struct timespec ts; \
66                 clock_gettime(CLOCK_MONOTONIC, &ts); \
67                 hal_tdm_log_print(HAL_TDM_LOG_LEVEL_WRN, "[%5d.%06d][%d][%s %d]"fmt, \
68                                           (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
69                                           (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
70         } while (0)
71
72 #define HAL_TDM_ERR(fmt, args...) \
73         do { \
74                 struct timespec ts; \
75                 clock_gettime(CLOCK_MONOTONIC, &ts); \
76                 hal_tdm_log_print(HAL_TDM_LOG_LEVEL_ERR, "[%5d.%06d][%d][%s %d]"fmt, \
77                                           (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
78                                           (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
79         } while (0)
80
81 #define HAL_TDM_RETURN_IF_FAIL(cond) {\
82         if (!(cond)) {\
83                 HAL_TDM_ERR("'%s' failed.\n", #cond);\
84                 return;\
85         } \
86 }
87 #define HAL_TDM_RETURN_VAL_IF_FAIL(cond, val) {\
88         if (!(cond)) {\
89                 HAL_TDM_ERR("'%s' failed.\n", #cond);\
90                 return val;\
91         } \
92 }
93 #define HAL_TDM_GOTO_VAL_IF_FAIL(cond, val) {\
94         if (!(cond)) {\
95                 HAL_TDM_ERR("'%s' failed.\n", #cond);\
96                 goto val;\
97         } \
98 }
99
100 enum {
101         HAL_TDM_LOG_LEVEL_NONE,
102         HAL_TDM_LOG_LEVEL_ERR,
103         HAL_TDM_LOG_LEVEL_WRN,
104         HAL_TDM_LOG_LEVEL_INFO,
105         HAL_TDM_LOG_LEVEL_DBG,
106 };
107
108 #endif  /* __COMMON_H__ */