summaryrefslogtreecommitdiff
path: root/src/include/eina_inline_log.x
blob: 4cdd7d8393010e00eeaec18138df42d9df767cae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* EINA - EFL data type library
 * Copyright (C) 2002-2008 Gustavo Sverzut Barbieri
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifndef EINA_LOG_INLINE_H_
#define EINA_LOG_INLINE_H_

/**
 * @addtogroup Eina_Log_Group Log
 *
 * @{
 */

/**
 * Checks whenever the given level should be printed out.
 *
 * This is useful to enable certain blocks of code just when given
 * level is to be used.
 *
 * @code
 * #include <Eina.h>
 *
 * void my_func(void *data)
 * {
 *    if (eina_log_level_check(EINA_LOG_LEVEL_WARN))
 *       expensive_debugging_code(data);
 *
 *    my_func_code(data);
 * }
 * @endcode
 *
 * @return #EINA_TRUE if level is equal or smaller than the current global
 *         logging level.
 */
static inline Eina_Bool
eina_log_level_check(int level)
{
   return eina_log_level_get() <= level;
}

/**
 * Checks whenever the given level should be printed out.
 *
 * This is useful to enable certain blocks of code just when given
 * level is to be used.
 *
 * @code
 * #include <Eina.h>
 *
 * extern int _my_log_dom;
 *
 * void my_func(void *data)
 * {
 *    if (eina_log_domain_level_check(_my_log_dom, EINA_LOG_LEVEL_WARN))
 *       expensive_debugging_code(data);
 *
 *    my_func_code(data);
 * }
 * @endcode
 *
 * @return #EINA_TRUE if level is equal or smaller than the current
 *         domain logging level.
 */
static inline Eina_Bool
eina_log_domain_level_check(int domain, int level)
{
   int dom_level = eina_log_domain_registered_level_get(domain);
   if (EINA_UNLIKELY(dom_level == EINA_LOG_LEVEL_UNKNOWN))
     return EINA_FALSE;
   return dom_level <= level;
}

/**
 * Function to format the level as a 3 character (+1 null byte) string.
 *
 * This function converts the given level to a known string name (CRI,
 * ERR, WRN, INF or DBG) or a zero-padded 3-character string. In any
 * case the last byte will contain a trailing null byte.
 *
 * If extreme level values are used (greater than 999 and smaller than
 * -99), then the value will just consider the less significant
 * part. This is so uncommon that users should handle this in their
 * code.
 *
 * @param level what level value to use.
 * @param name where to write the actual value.
 *
 * @return pointer to @p name.
 */
static inline const char *
eina_log_level_name_get(int level, char name[4])
{
#define BCPY(A, B, C) \
   do { name[0] = A; name[1] = B; name[2] = C; } while (0)

   if (EINA_UNLIKELY(level < 0))
     {
	name[0] = '-';
	name[1] = '0' + (-level / 10) % 10;
	name[2] = '0' + (-level % 10);
     }
   else if (EINA_UNLIKELY(level >= EINA_LOG_LEVELS))
     {
	name[0] = '0' + (level / 100) % 10;
	name[1] = '0' + (level / 10) % 10;
	name[2] = '0' + level % 10;
     }
   else if (level == 0)
     BCPY('C', 'R', 'I');
   else if (level == 1)
     BCPY('E', 'R', 'R');
   else if (level == 2)
     BCPY('W', 'R', 'N');
   else if (level == 3)
     BCPY('I', 'N', 'F');
   else if (level == 4)
     BCPY('D', 'B', 'G');
   else
     BCPY('?', '?', '?');

   name[3] = '\0';

   return name;
}

/**
 * Function to get recommended color value for level.
 *
 * This function will not check if colors are enabled or not before
 * returning the level color. If you desire such check, use
 * eina_log_level_color_if_enabled_get().
 *
 * @param level what level value to use.
 *
 * @return pointer to null byte terminated ANSI color string to be
 *         used in virtual terminals supporting VT100 color codes.
 *
 * @see eina_log_level_color_if_enabled_get()
 */
static inline const char *
eina_log_level_color_get(int level)
{
   if (level <= 0)
     return EINA_COLOR_LIGHTRED;
   else if (level == 1)
     return EINA_COLOR_RED;
   else if (level == 2)
     return EINA_COLOR_YELLOW;
   else if (level == 3)
     return EINA_COLOR_GREEN;
   else if (level == 4)
     return EINA_COLOR_LIGHTBLUE;
   else
     return EINA_COLOR_BLUE;
}

/**
 * Function to get recommended color value for level, if colors are
 * enabled.
 *
 * This function will check if colors are enabled or not before
 * returning the level color. If colors are disabled, then empty
 * string is returned.
 *
 * @param level what level value to use.
 *
 * @return pointer to null byte terminated ANSI color string to be
 *         used in virtual terminals supporting VT100 color codes. If
 *         colors are disabled, the empty string is returned.
 */
static inline const char *
eina_log_level_color_if_enabled_get(int level)
{
   if (eina_log_color_disable_get())
     return "";
   return eina_log_level_color_get(level);
}

/**
 * @}
 */

#endif /* EINA_LOG_INLINE_H_ */