[Internal: merge sync-agent]
[platform/core/system/sync-agent.git] / include / utility / sync_util.h
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef SYNC_UTIL_H_
19 #define SYNC_UTIL_H_
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif                          /* __cplusplus */
24
25 #include <string.h>
26
27 #include <dlog.h>
28
29 #include <libgen.h>
30
31 /**
32  * @file sync_util.h
33  * @brief Provides logging macro
34  */
35
36 /** @addtogroup utility
37  *      @{
38  */
39
40 #define COLOR_BLACK             "\033[0;30m"
41 #define COLOR_RED                       "\033[0;31m"
42 #define COLOR_GREEN             "\033[0;32m"
43 #define COLOR_YELLOW            "\033[0;33m"
44 #define COLOR_BLUE              "\033[0;34m"
45 #define COLOR_MARGENTA  "\033[0;35m"
46 #define COLOR_CYAN              "\033[0;36m"
47 #define COLOR_WHITE             "\033[0;37m"
48 #define COLOR_END                       "\033[0;m"
49
50 #if COMPONENT_TAG == SYSTEM
51 #define _DEBUG_VERBOSE(fmt, args...)            SLOGV(" " fmt, ##args)
52 #define _DEBUG_TRACE(fmt, args...)              SLOGD(" " fmt, ##args)
53 #define _DEBUG_INFO(fmt, args...)               SLOGI(COLOR_YELLOW fmt COLOR_END, ##args)
54 #define _DEBUG_WARNING(fmt, args...)            SLOGW(COLOR_MARGENTA" * Warning * " fmt COLOR_END, ##args)
55 #define _DEBUG_ERROR(fmt, args...)              SLOGE(COLOR_RED" * Critical * " fmt COLOR_END, ##args)
56 #define _INNER_FUNC_ENTER                                       SLOGD(COLOR_CYAN" ENTER >>>"COLOR_END)
57 #define _INNER_FUNC_EXIT                                        SLOGD(COLOR_CYAN" EXIT <<<"COLOR_END)
58 #define _EXTERN_FUNC_ENTER                              SLOGI(COLOR_GREEN" ENTER >>>"COLOR_END)
59 #define _EXTERN_FUNC_EXIT                                       SLOGI(COLOR_GREEN" EXIT <<<"COLOR_END)
60 #elif COMPONENT_TAG == APP
61 #define _DEBUG_VERBOSE(fmt, args...)            ALOGV(" " fmt, ##args)
62 #define _DEBUG_TRACE(fmt, args...)              ALOGD(" " fmt, ##args)
63 #define _DEBUG_INFO(fmt, args...)               ALOGI(COLOR_YELLOW fmt COLOR_END, ##args)
64 #define _DEBUG_WARNING(fmt, args...)            ALOGW(COLOR_MARGENTA" * Warning * " fmt COLOR_END, ##args)
65 #define _DEBUG_ERROR(fmt, args...)              ALOGE(COLOR_RED" * Critical * " fmt COLOR_END, ##args)
66 #define _INNER_FUNC_ENTER                                       ALOGD(COLOR_CYAN" ENTER >>>"COLOR_END)
67 #define _INNER_FUNC_EXIT                                        ALOGD(COLOR_CYAN" EXIT <<<"COLOR_END)
68 #define _EXTERN_FUNC_ENTER                              ALOGI(COLOR_GREEN" ENTER >>>"COLOR_END)
69 #define _EXTERN_FUNC_EXIT                                       ALOGI(COLOR_GREEN" EXIT <<<"COLOR_END)
70 #endif
71
72 #define warn_if(expr, fmt, arg...)              \
73         do {                                    \
74                 if (expr) {                     \
75                         _DEBUG_WARNING(fmt, ##arg);     \
76                 }                               \
77         } while (0)
78 #define ret_if(expr)                            \
79         do {                                    \
80                 if (expr) {                     \
81                         return;                 \
82                 }                               \
83         } while (0)
84 #define retv_if(expr, val)\
85         do {                                    \
86                 if (expr) {                     \
87                         return (val);           \
88                 }                               \
89         } while (0)
90 #define retm_if(expr, fmt, arg...)              \
91         do {                                    \
92                 if (expr) {                     \
93                         _DEBUG_ERROR(fmt, ##arg);       \
94                         return;                 \
95                 }                               \
96         } while (0)
97 #define retvm_if(expr, val, fmt, arg...)        \
98         do {                                    \
99                 if (expr) {                     \
100                         _DEBUG_ERROR(fmt, ##arg);       \
101                         return (val);           \
102                 }                               \
103         } while (0)
104
105 #define _ASSERT(cond, ret)                                      \
106         do {                                                    \
107                 if (!(cond)) {                                  \
108                         _DEBUG_ERROR("_ASSERT fails");  \
109                         return (ret);                           \
110                 }                                               \
111         } while (0)
112 #define goto_if(expr, fmt, arg...)              \
113         do {                                    \
114                 if (expr) {             \
115                         _DEBUG_ERROR(fmt, ##arg);       \
116                         goto return_part;               \
117                 }                               \
118         } while (0)
119
120 /**
121  * API to daemonize client agent
122  *
123  * @par Usage:
124  * @code
125                 if (sync_agent_daemonize() < 0) {
126                         _DEBUG_ERROR("daemonize error");
127                         // error handling
128                         ...
129                 }
130  *
131  *      @endcode
132  *
133  *      @return 0 on success, -1 on error
134  *
135  *      @par Since:
136  *
137  *
138  *      @see sync_agent_daemonize()
139  *
140  */
141         int sync_agent_daemonize(void);
142
143 /**
144  *      @}
145  */
146
147 #ifdef __cplusplus
148 }
149 #endif                          /* __cplusplus */
150 #endif                          /* SYNC_UTIL_H_ */