update for beta universally
[framework/system/devman.git] / devlog.h
1 /*
2  *  devman
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongGi Jang <dg0402.jang@samsung.com>
7  * 
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20 */ 
21
22
23 #ifndef __DEVLOG_H__
24 #define __DEVLOG_H__
25
26 /*
27  * SYSLOG_INFO(), SYSLOG_ERR(), SYSLOG_DBG() are syslog() wrappers.
28  * PRT_INFO(), PRT_ERR(), PRT_DBG() are fprintf() wrappers.
29  *
30  * If SLP_DEBUG is not defined, SYSLOG_DBG() and PRT_DBG() is ignored.
31  *
32  * IF SLP_SYSLOG_OUT is defined, 
33  *   INFO(), ERR(), DBG() are SYSLOG_XXX()
34  * IF ENABLE_DLOG_OUT is defined,
35  *   INFO(), ERR(), DBG() are SLOGI(), SLOGE(), SLOGD()
36  * Otherwise,
37  *   They are PRT_XXX()
38  *
39  *
40  * warn_if(exrp, fmt, ...)
41  *   If expr is true, The fmt string is printed using ERR().
42  * 
43  * ret_if(), retv_if(), retm_if(), retvm_if() 
44  *   If expr is true, current function return. 
45  *   Postfix 'v' means that it has a return value and 'm' means that it has output message.
46  *
47  */
48 #include <stdio.h>
49 #include <stdlib.h>
50
51 #if defined(ENABLE_DLOG_OUT)
52 #  define LOG_TAG               "DEVMAN"
53 #  include <dlog.h>
54 #else
55 #  include <syslog.h>
56 #  define __LOG(prio, fmt, arg...) \
57                 do { syslog(prio, fmt, ##arg); } while (0)
58 #  define __LOGD(prio, fmt, arg...) \
59                 do { \
60                         if (getenv("SLP_DEBUG")) { \
61                                 syslog(prio, "[%s:%d] "fmt"\n", __FILE__, __LINE__, ##arg); \
62                         } \
63                 } while (0)
64 #endif
65
66 #define __PRTI(fmt, arg...) \
67         do { fprintf(stdout, fmt"\n", ##arg); } while (0)
68 #define __PRTE(fmt, arg...) \
69         do { fprintf(stderr, fmt"\n", ##arg); } while (0)
70 #define __PRTD(fmt, arg...) \
71         do { \
72                 if (getenv("SLP_DEBUG")) { \
73                         fprintf(stdout, "[%s:%d] "fmt"\n", __FILE__, __LINE__, ##arg); \
74                 } \
75         } while(0)
76 #define _NOUT(arg...) do { } while (0)
77
78 #ifdef SLP_DEBUG
79 #  define _LOGD __LOGD
80 #  define _LOG  __LOG
81 #  define _PRTD __PRTD
82 #  define _PRTI __PRTI
83 #  define _PRTE __PRTE
84 #else
85 #  define _LOGD _NOUT
86 #  define _LOG  __LOG
87 #  define _PRTD _NOUT
88 #  define _PRTI __PRTI
89 #  define _PRTE __PRTE
90 #endif
91
92 #define PRT_INFO(fmt, arg...) _PRTI(fmt, ##arg)
93 #define PRT_ERR(fmt, arg...) _PRTE(fmt, ##arg)
94 #define PRT_DBG(fmt, arg...) _PRTD(fmt, ##arg)
95
96 #if defined(SLP_SYSLOG_OUT)
97 #  define SYSLOG_INFO(fmt, arg...) _LOG(LOG_INFO, fmt, ##arg)
98 #  define SYSLOG_ERR(fmt, arg...) _LOG(LOG_ERR, fmt, ##arg)
99 #  define SYSLOG_DBG(fmt, arg...) _LOGD(LOG_DEBUG, fmt, ##arg)
100 #  define INFO SYSLOG_INFO
101 #  define ERR SYSLOG_ERR
102 #  define DBG SYSLOG_DBG
103 #elif defined(ENABLE_DLOG_OUT)
104 #  define INFO SLOGI
105 #  define ERR SLOGE
106 #  define DBG SLOGD
107 #else
108 #  define INFO PRT_INFO
109 #  define ERR PRT_ERR
110 #  define DBG PRT_DBG
111 #endif
112
113 #ifdef SLP_DEBUG
114 #  define warn_if(expr, fmt, arg...) do { \
115                 if (expr) { \
116                         DBG("(%s) -> "fmt, #expr, ##arg); \
117                 } \
118         } while (0)
119 #  define ret_if(expr) do { \
120                 if (expr) { \
121                         DBG("(%s) -> %s() return", #expr, __FUNCTION__); \
122                         return; \
123                 } \
124         } while (0)
125 #  define retv_if(expr, val) do { \
126                 if (expr) { \
127                         DBG("(%s) -> %s() return", #expr, __FUNCTION__); \
128                         return (val); \
129                 } \
130         } while (0)
131 #  define retm_if(expr, fmt, arg...) do { \
132                 if (expr) { \
133                         ERR(fmt, ##arg); \
134                         DBG("(%s) -> %s() return", #expr, __FUNCTION__); \
135                         return; \
136                 } \
137         } while (0)
138 #  define retvm_if(expr, val, fmt, arg...) do { \
139                 if (expr) { \
140                         ERR(fmt, ##arg); \
141                         DBG("(%s) -> %s() return", #expr, __FUNCTION__); \
142                         return (val); \
143                 } \
144         } while (0)
145
146 #else
147 #  define warn_if(expr, fmt, arg...) do { \
148                 if (expr) { \
149                         ERR(fmt, ##arg); \
150                 } \
151         } while (0)
152 #  define ret_if(expr) do { \
153                 if (expr) { \
154                         return; \
155                 } \
156         } while (0)
157 #  define retv_if(expr, val) do { \
158                 if (expr) { \
159                         return (val); \
160                 } \
161         } while (0)
162 #  define retm_if(expr, fmt, arg...) do { \
163                 if (expr) { \
164                         ERR(fmt, ##arg); \
165                         return; \
166                 } \
167         } while (0)
168 #  define retvm_if(expr, val, fmt, arg...) do { \
169                 if (expr) { \
170                         ERR(fmt, ##arg); \
171                         return (val); \
172                 } \
173         } while (0)
174
175 #endif
176
177 #endif                          /* __DEVLOG_H__ */