EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / include / eina_inline_log.x
1 /* EINA - EFL data type library
2  * Copyright (C) 2002-2008 Gustavo Sverzut Barbieri
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library;
16  * if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef EINA_LOG_INLINE_H_
20 #define EINA_LOG_INLINE_H_
21
22 /**
23  * @addtogroup Eina_Log_Group Log
24  *
25  * @{
26  */
27
28 /**
29  * @brief Checks whenever the given level should be printed out.
30  *
31  * @param level The level to print
32  *
33  * This is useful to enable certain blocks of code just when given
34  * level is to be used.
35  *
36  * @code
37  * #include <Eina.h>
38  *
39  * void my_func(void *data)
40  * {
41  *    if (eina_log_level_check(EINA_LOG_LEVEL_WARN))
42  *       expensive_debugging_code(data);
43  *
44  *    my_func_code(data);
45  * }
46  * @endcode
47  *
48  * @return #EINA_TRUE if level is equal or smaller than the current global
49  *         logging level.
50  */
51 static inline Eina_Bool
52 eina_log_level_check(int level)
53 {
54    return eina_log_level_get() >= level;
55 }
56
57 /**
58  * @brief Checks whenever the given level should be printed out.
59  *
60  * @param domain The domain to check
61  * @param level The level to print
62  *
63  * This is useful to enable certain blocks of code just when given
64  * level is to be used.
65  *
66  * @code
67  * #include <Eina.h>
68  *
69  * extern int _my_log_dom;
70  *
71  * void my_func(void *data)
72  * {
73  *    if (eina_log_domain_level_check(_my_log_dom, EINA_LOG_LEVEL_WARN))
74  *       expensive_debugging_code(data);
75  *
76  *    my_func_code(data);
77  * }
78  * @endcode
79  *
80  * @return #EINA_TRUE if level is equal or smaller than the current
81  *         domain logging level.
82  */
83 static inline Eina_Bool
84 eina_log_domain_level_check(int domain, int level)
85 {
86    int dom_level = eina_log_domain_registered_level_get(domain);
87    if (EINA_UNLIKELY(dom_level == EINA_LOG_LEVEL_UNKNOWN))
88      return EINA_FALSE;
89    return dom_level >= level;
90 }
91
92 /**
93  * Function to format the level as a 3 character (+1 null byte) string.
94  *
95  * This function converts the given level to a known string name (CRI,
96  * ERR, WRN, INF or DBG) or a zero-padded 3-character string. In any
97  * case the last byte will contain a trailing null byte.
98  *
99  * If extreme level values are used (greater than 999 and smaller than
100  * -99), then the value will just consider the less significant
101  * part. This is so uncommon that users should handle this in their
102  * code.
103  *
104  * @param level what level value to use.
105  * @param name where to write the actual value.
106  *
107  * @return pointer to @p name.
108  */
109 static inline const char *
110 eina_log_level_name_get(int level, char name[4])
111 {
112 #define BCPY(A, B, C) \
113    do { name[0] = A; name[1] = B; name[2] = C; } while (0)
114
115    if (EINA_UNLIKELY(level < 0))
116      {
117         name[0] = '-';
118         name[1] = '0' + (-level / 10) % 10;
119         name[2] = '0' + (-level % 10);
120      }
121    else if (EINA_UNLIKELY(level >= EINA_LOG_LEVELS))
122      {
123         name[0] = '0' + (level / 100) % 10;
124         name[1] = '0' + (level / 10) % 10;
125         name[2] = '0' + level % 10;
126      }
127    else if (level == 0)
128      BCPY('C', 'R', 'I');
129    else if (level == 1)
130      BCPY('E', 'R', 'R');
131    else if (level == 2)
132      BCPY('W', 'R', 'N');
133    else if (level == 3)
134      BCPY('I', 'N', 'F');
135    else if (level == 4)
136      BCPY('D', 'B', 'G');
137    else
138      BCPY('?', '?', '?');
139
140    name[3] = '\0';
141
142    return name;
143 }
144
145 /**
146  * Function to get recommended color value for level.
147  *
148  * This function will not check if colors are enabled or not before
149  * returning the level color. If you desire such check, use
150  * eina_log_level_color_if_enabled_get().
151  *
152  * @param level what level value to use.
153  *
154  * @return pointer to null byte terminated ANSI color string to be
155  *         used in virtual terminals supporting VT100 color codes.
156  *
157  * @see eina_log_level_color_if_enabled_get()
158  */
159 static inline const char *
160 eina_log_level_color_get(int level)
161 {
162    if (level <= 0)
163      return EINA_COLOR_LIGHTRED;
164    else if (level == 1)
165      return EINA_COLOR_RED;
166    else if (level == 2)
167      return EINA_COLOR_YELLOW;
168    else if (level == 3)
169      return EINA_COLOR_GREEN;
170    else if (level == 4)
171      return EINA_COLOR_LIGHTBLUE;
172    else
173      return EINA_COLOR_BLUE;
174 }
175
176 /**
177  * Function to get recommended color value for level, if colors are
178  * enabled.
179  *
180  * This function will check if colors are enabled or not before
181  * returning the level color. If colors are disabled, then empty
182  * string is returned.
183  *
184  * @param level what level value to use.
185  *
186  * @return pointer to null byte terminated ANSI color string to be
187  *         used in virtual terminals supporting VT100 color codes. If
188  *         colors are disabled, the empty string is returned.
189  */
190 static inline const char *
191 eina_log_level_color_if_enabled_get(int level)
192 {
193    if (eina_log_color_disable_get())
194      return "";
195    return eina_log_level_color_get(level);
196 }
197
198 /**
199  * @}
200  */
201
202 #endif /* EINA_LOG_INLINE_H_ */