2.0_alpha release commit
[framework/messaging/email-service.git] / email-common-use / include / email-debug-log.h
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@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
24 /******************************************************************************
25  * File: email-debug-log.h
26  * Desc: email-common-use debug header
27  *
28  * Auth:
29  *
30  * History:
31  *    2006.08.01 : created
32  *****************************************************************************/
33 #ifndef __EMAIL_DEBUG_LOG_H__
34 #define __EMAIL_DEBUG_LOG_H__
35
36 #ifdef  __cplusplus
37 extern "C"
38 {
39 #endif
40
41 #include <stdio.h>
42 #include <string.h>
43 #include <dlog.h>
44 #include <errno.h>
45
46 #define __FEATURE_DEBUG_LOG__
47
48 #ifdef  __FEATURE_DEBUG_LOG__
49
50 /* definition of LOG_TAG */
51 #ifdef LOG_TAG
52 #undef LOG_TAG
53 #endif
54
55 #define LOG_TAG "email-service"
56
57 #define EM_DEBUG_LOG(format, arg...)   \
58                         SLOGD("[%s() :%s:%d] " format "\n",     __FUNCTION__, \
59                                  (rindex(__FILE__, '/')? rindex(__FILE__,'/')+1 : __FILE__ ),  __LINE__, ##arg)
60 #define EM_DEBUG_EXCEPTION(format, arg...) \
61                         SLOGE("[%s() :%s:%d][EXCEPTION!!] " format "\n", __FUNCTION__, \
62                                 (rindex(__FILE__, '/')? rindex(__FILE__,'/')+1 : __FILE__ ),   __LINE__, ##arg)
63
64 #ifdef  _DEBUG_MIME_PARSE_
65 #define EM_DEBUG_LOG_MIME(format, arg...)   EM_DEBUG_LOG(format, ##arg)
66 #else   /*  _DEBUG_MIME_PARSE */
67 #define EM_DEBUG_LOG_MIME(format, arg...)
68 #endif /*  _DEBUG_MIME_PARSE */
69
70 #define EM_DEBUG_FUNC_BEGIN(format, arg...) EM_DEBUG_LOG("BEGIN - "format, ##arg)
71 #define EM_DEBUG_FUNC_END(format, arg...)   EM_DEBUG_LOG("END - "format, ##arg)
72 #define EM_DEBUG_LINE                       EM_DEBUG_LOG("FUNC[%s : %d]", __FUNCTION__, __LINE__)
73 #define EM_DEBUG_DB_EXEC(eval, expr, X)     if (eval) { EM_DEBUG_LOG X; expr;} else {;}
74
75 #define EM_DEBUG_ERROR_FILE_PATH            "/opt/data/email/.emfdata/.critical_error.log"
76 #define EM_DEBUG_CRITICAL_EXCEPTION(format, arg...)   \
77                         {\
78                                 FILE *fp_error = NULL;\
79                                 fp_error = fopen(EM_DEBUG_ERROR_FILE_PATH, "a");\
80                                 if(fp_error) {\
81                                         fprintf(fp_error, "[%s() :%s:%d] " format "\n", \
82                                                 __FUNCTION__, (rindex(__FILE__, '/')? rindex(__FILE__,'/')+1 : __FILE__ ),\
83                                                 __LINE__, ##arg); \
84                                         fclose(fp_error);\
85                                 }\
86                         }
87 #define EM_DEBUG_PERROR(str)    ({\
88                 char buf[128] = {0};\
89                 strerror_r(errno, buf, sizeof(buf));\
90                 EM_DEBUG_EXCEPTION("%s: %s(%d)", str, buf, errno);\
91         })
92
93 #ifdef _USE_PROFILE_DEBUG_
94 #define EM_PROFILE_BEGIN(pfid) \
95         unsigned int __prf_l1_##pfid = __LINE__;\
96         struct timeval __prf_1_##pfid;\
97         struct timeval __prf_2_##pfid;\
98         do {\
99                 gettimeofday(&__prf_1_##pfid, 0);\
100                 EM_DEBUG_LOG("**PROFILE BEGIN** [EMAILFW: %s() :%s %u ~ ] " #pfid \
101                 " ->  Start Time: %u.%06u seconds\n",\
102                         __FUNCTION__,\
103                 rindex(__FILE__,'/')+1, \
104                 __prf_l1_##pfid,\
105                 (unsigned int)__prf_1_##pfid.tv_sec,\
106                 (unsigned int)__prf_1_##pfid.tv_usec );\
107         } while (0)
108
109 #define EM_PROFILE_END(pfid) \
110         unsigned int __prf_l2_##pfid = __LINE__;\
111         do { \
112                 gettimeofday(&__prf_2_##pfid, 0);\
113                 long __ds = __prf_2_##pfid.tv_sec - __prf_1_##pfid.tv_sec;\
114                 long __dm = __prf_2_##pfid.tv_usec - __prf_1_##pfid.tv_usec;\
115                 if ( __dm < 0 ) { __ds--; __dm = 1000000 + __dm; } \
116                 EM_DEBUG_LOG("**PROFILE END** [EMAILFW: %s() :%s %u ~ %u] " #pfid                            \
117                 " -> Elapsed Time: %u.%06u seconds\n",\
118                         __FUNCTION__,\
119                 rindex(__FILE__, '/')+1,\
120                 __prf_l1_##pfid,\
121                 __prf_l2_##pfid,\
122                 (unsigned int)(__ds),\
123                 (unsigned int)(__dm));\
124         } while (0)
125 #else
126
127 #define EM_PROFILE_BEGIN(pfid)
128 #define EM_PROFILE_END(pfid)
129
130 #endif
131
132
133 #else /* __FEATURE_DEBUG_LOG__ */
134
135         #define EM_DEBUG_LINE
136         #define EM_DEBUG_LOG(format, arg...)
137         #define EM_DEBUG_ASSERT(format, arg...)
138         #define EM_DEBUG_EXCEPTION(format, arg...)
139
140
141         #define EM_DEBUG_DB_EXEC(eval, expr, X)  if (eval) { EM_DEBUG_DB X; expr;} else {;}
142         #define EM_PROFILE_BEGIN(pfid)
143         #define EM_PROFILE_END(pfid)
144
145 #endif /* __FEATURE_DEBUG_LOG__ */
146
147
148 #define EM_NULL_CHECK_FOR_VOID(expr)    \
149         {\
150                 if (!expr) {\
151                         EM_DEBUG_EXCEPTION ("INVALID PARAM: "#expr" NULL ");\
152                         return;\
153                 }\
154         }
155
156 #define EM_IF_NULL_RETURN_VALUE(expr, val) \
157         {\
158                 if (!expr ) {\
159                         EM_DEBUG_EXCEPTION ("INVALID PARAM: "#expr" NULL ");\
160                         return val;     \
161                 }\
162         }
163
164 #define EM_RETURN_ERR_CODE(err_ptr, err_code, ret) \
165         {\
166                 if(err_ptr) *err_ptr = err_code;\
167                 return ret;\
168         }
169
170 #define EM_EXIT_ERR_CODE(err_ptr, err_code) \
171         {\
172                 if(err_ptr) *err_ptr = err_code;\
173                 return;\
174         }
175
176
177 #define EM_SAFE_FREE(expr)       \
178         ({\
179                 if (expr ) {\
180                         free(expr);\
181                         expr = NULL;\
182                 }\
183         })
184
185 #define EM_SAFE_STRDUP(s) \
186         ({\
187                 char* _s = (char*)s;\
188                 (_s)? strdup(_s) : NULL;\
189         })
190
191 #define EM_SAFE_STRCMP(dest, src) \
192         ({\
193                 char* _dest = dest;\
194                 char* _src = src;\
195                 ((_src) && (_dest))? strcmp(_dest, _src) : -1;\
196         })
197
198 #define EM_SAFE_STRCPY(dest, src) \
199         ({\
200                 char* _dest = dest;\
201                 char* _src = src;\
202                 ((_src) && (_dest))? strcpy(_dest, _src) : NULL;\
203         })
204
205 #define EM_SAFE_STRNCPY(dest, src, size) \
206         ({\
207                 char* _dest = dest;\
208                 char* _src = src;\
209                 int _size = size;\
210                 ((_src) && (_dest))? strncpy(_dest, _src, _size) : NULL;\
211         })
212
213 #define EM_SAFE_STRCAT(dest, src) \
214         ({\
215                 char* _dest = dest;\
216                 char* _src = src;\
217                 ((_src) && (_dest))? strcat(_dest, _src) : NULL;\
218         })
219
220 #define EM_SAFE_STRLEN(s) \
221         ({\
222                 char* _s = (char*)s;\
223                 (_s)? strlen(_s) : 0;\
224         })
225
226
227 #define EM_IF_ACCOUNT_ID_NULL(expr, ret) {\
228                 if (expr <= 0) {\
229                         EM_DEBUG_EXCEPTION ("EM_IF_ACCOUNT_ID_NULL: Account ID [ %d ]  ", expr);\
230                         return ret;\
231                 }\
232         }
233
234
235 #define EM_STRERROR(err) ({ char buf[128]; strerror_r(err, buf, sizeof(buf));})
236
237
238 #ifdef  __cplusplus
239 }
240 #endif  /* __cplusplu */
241
242 #endif  /* __EMAIL_DEBUG_LOG_H__ */