1 /* EINA - EFL data type library
2 * Copyright (C) 2008 Cedric Bail
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.
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.
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/>.
30 #include "eina_config.h"
31 #include "eina_private.h"
32 #include "eina_error.h"
33 #include "eina_inlist.h"
35 #include "eina_magic.h"
37 /*============================================================================*
39 *============================================================================*/
45 typedef struct _Eina_Magic_String Eina_Magic_String;
46 struct _Eina_Magic_String
54 static Eina_Inlist *strings = NULL;
60 /*============================================================================*
62 *============================================================================*/
64 /*============================================================================*
66 *============================================================================*/
69 * @addtogroup Eina_Magic_Group Magic
71 * @brief These functions provide magic checks management for projects.
78 * @brief Initialize the magic string module.
80 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
82 * This function sets up the magic string module of Eina. It is called by
88 eina_magic_string_init(void)
95 * @brief Shut down the magic string module.
97 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
99 * This function shuts down the magic string module set up by
100 * eina_magic string_init(). It is called by eina_shutdown().
102 * @see eina_shutdown()
105 eina_magic_string_shutdown(void)
107 /* Free all strings. */
110 Eina_Magic_String *tmp = (Eina_Magic_String*) strings;
111 strings = eina_inlist_remove(strings, strings);
121 * @brief Return the string associated to the given magic identifier.
123 * @param magic The magic identifier.
124 * @return The string associated to the identifier.
126 * This function returns the string associated to @p magic. If none
127 * are found, the this function returns @c NULL. The returned value
131 eina_magic_string_get(Eina_Magic magic)
133 Eina_Magic_String *ems;
135 EINA_INLIST_FOREACH(strings, ems)
136 if (ems->magic == magic)
143 * @brief Set the string associated to the given magic identifier.
145 * @param magic The magic identifier.
146 * @param The string associated to the identifier.
148 * This function sets the string @p magic_name to @p magic. If a
149 * string is already associated to @p magic, then it is freed and @p
150 * magic_name is duplicated. Otherwise, it is added to the list of
154 eina_magic_string_set(Eina_Magic magic, const char *magic_name)
156 Eina_Magic_String *ems;
158 EINA_INLIST_FOREACH(strings, ems)
159 if (ems->magic == magic)
163 ems->string = strdup(magic_name);
169 ems = malloc(sizeof (Eina_Magic_String));
174 ems->string = strdup(magic_name);
178 strings = eina_inlist_prepend(strings, EINA_INLIST_GET(ems));
181 #ifdef eina_magic_fail
182 # undef eina_magic_fail
186 * @brief Display a message or abort is a magic check failed.
188 * @param d The checked data pointer.
189 * @param m The magic identifer to check.
190 * @param req_m The requested magic identifier to check.
191 * @param file The file in which the magic check failed.
192 * @param fcn The function in which the magic check failed.
193 * @param line The line at which the magic check failed.
195 * This function displays an error message if a magic check has
196 * failed, using the following logic in the following order:
197 * @li If @p d is @c NULL, a message warns about a @c NULL pointer.
198 * @li Otherwise, if @p m is equal to #EINA_MAGIC_NONE, a message
199 * warns about a handle that was already freed.
200 * @li Otherwise, if @p m is equal to @p req_m, a message warns about
201 * a handle that is of wrong type.
202 * @li Otherwise, a message warns you about ab-using that function...
204 * If the environment variable EINA_ERROR_ABORT is set, abort() is
205 * called and the program stops. It is useful for debugging programs
209 eina_magic_fail(void *d, Eina_Magic m, Eina_Magic req_m, const char *file, const char *fnc, int line)
212 eina_log_print(EINA_LOG_DOMAIN_GLOBAL, EINA_LOG_LEVEL_CRITICAL,
214 "*** Eina Magic Check Failed !!!\n"
215 " Input handle pointer is NULL !\n"
216 "*** NAUGHTY PROGRAMMER!!!\n"
217 "*** SPANK SPANK SPANK!!!\n"
218 "*** Now go fix your code. Tut tut tut!\n"
221 if (m == EINA_MAGIC_NONE)
222 eina_log_print(EINA_LOG_DOMAIN_GLOBAL, EINA_LOG_LEVEL_CRITICAL,
224 "*** Eina Magic Check Failed !!!\n"
225 " Input handle has already been freed!\n"
226 "*** NAUGHTY PROGRAMMER!!!\n"
227 "*** SPANK SPANK SPANK!!!\n"
228 "*** Now go fix your code. Tut tut tut!\n"
232 eina_log_print(EINA_LOG_DOMAIN_GLOBAL, EINA_LOG_LEVEL_CRITICAL,
234 "*** Eina Magic Check Failed !!!\n"
235 " Input handle is wrong type\n"
236 " Expected: %08x - %s\n"
237 " Supplied: %08x - %s\n"
238 "*** NAUGHTY PROGRAMMER!!!\n"
239 "*** SPANK SPANK SPANK!!!\n"
240 "*** Now go fix your code. Tut tut tut!\n"
242 req_m, eina_magic_string_get(req_m),
243 m, eina_magic_string_get(m));
245 eina_log_print(EINA_LOG_DOMAIN_GLOBAL, EINA_LOG_LEVEL_CRITICAL,
247 "*** Eina Magic Check Failed !!!\n"
248 " Why did you call me !\n"
249 "*** NAUGHTY PROGRAMMER!!!\n"
250 "*** SPANK SPANK SPANK!!!\n"
251 "*** Now go fix your code. Tut tut tut!\n"