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