| 1 | /* |
| 2 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 3 | * Copyright (C) 2003-2008 Benny Prijono <[email protected]> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #ifndef __PJ_ASSERT_H__ |
| 20 | #define __PJ_ASSERT_H__ |
| 21 | |
| 22 | /** |
| 23 | * @file assert.h |
| 24 | * @brief Assertion macro pj_assert(). |
| 25 | */ |
| 26 | |
| 27 | #include <pj/config.h> |
| 28 | #include <pj/compat/assert.h> |
| 29 | |
| 30 | /** |
| 31 | * @defgroup pj_assert Assertion Macro |
| 32 | * @ingroup PJ_MISC |
| 33 | * @{ |
| 34 | * |
| 35 | * Assertion and other helper macros for sanity checking. |
| 36 | */ |
| 37 | |
| 38 | /** |
| 39 | * @hideinitializer |
| 40 | * Check during debug build that an expression is true. If the expression |
| 41 | * computes to false during run-time, then the program will stop at the |
| 42 | * offending statements. |
| 43 | * For release build, this macro will only log the assertion, while the |
| 44 | * program continues running. |
| 45 | * |
| 46 | * @param expr The expression to be evaluated. |
| 47 | */ |
| 48 | #if PJ_DEBUG==0 |
| 49 | |
| 50 | # ifndef pj_assert |
| 51 | # include "pj/os.h" |
| 52 | # include "pj/log.h" |
| 53 | # define pj_assert(expr) \ |
| 54 | do { \ |
| 55 | if (!(expr)) { \ |
| 56 | if (pj_thread_is_registered()) \ |
| 57 | PJ_LOG(1, (__FILE__, "Assert failed: %s", #expr)); \ |
| 58 | } \ |
| 59 | } while (0) |
| 60 | # endif |
| 61 | |
| 62 | #else /* PJ_DEBUG != 0 */ |
| 63 | |
| 64 | # ifndef pj_assert |
| 65 | # define pj_assert(expr) assert(expr) |
| 66 | # endif |
| 67 | |
| 68 | #endif |
| 69 | |
| 70 | /** |
| 71 | * @hideinitializer |
| 72 | * For all builds, log the message. |
| 73 | * Check during debug build that an expression is true. If the expression |
| 74 | * computes to false during run-time, then the program will stop at the |
| 75 | * offending statements. |
| 76 | * For release build, this macro only print message on the log. |
| 77 | * @param expr The expression to be evaluated. |
| 78 | * @param ... File name. The format string for the log message |
| 79 | * ("config.c", " PJ_VERSION: %s", PJ_VERSION) |
| 80 | */ |
| 81 | #ifndef PJ_ASSERT_LOG |
| 82 | #include "pj/log.h" |
| 83 | #define PJ_ASSERT_LOG(expr,...) \ |
| 84 | do { \ |
| 85 | if (!(expr)) { PJ_LOG(1,(__VA_ARGS__)); assert(expr); } \ |
| 86 | } while (0) |
| 87 | |
| 88 | #endif |
| 89 | |
| 90 | /** |
| 91 | * @hideinitializer |
| 92 | * If the expression yields false, assertion will be triggered |
| 93 | * and the current function will return with the specified return value. |
| 94 | */ |
| 95 | // #if defined(PJ_ENABLE_EXTRA_CHECK) && PJ_ENABLE_EXTRA_CHECK != 0 |
| 96 | #define PJ_ASSERT_RETURN(expr,retval) \ |
| 97 | do { \ |
| 98 | if (!(expr)) { pj_assert(expr); return retval; } \ |
| 99 | } while (0) |
| 100 | //#else |
| 101 | //# define PJ_ASSERT_RETURN(expr,retval) pj_assert(expr) |
| 102 | //#endif |
| 103 | |
| 104 | /** |
| 105 | * @hideinitializer |
| 106 | * If the expression yields false, assertion will be triggered |
| 107 | * and @a exec_on_fail will be executed. |
| 108 | */ |
| 109 | //#if defined(PJ_ENABLE_EXTRA_CHECK) && PJ_ENABLE_EXTRA_CHECK != 0 |
| 110 | #define PJ_ASSERT_ON_FAIL(expr,exec_on_fail) \ |
| 111 | { \ |
| 112 | pj_assert(expr); \ |
| 113 | if (!(expr)) exec_on_fail; \ |
| 114 | } |
| 115 | //#else |
| 116 | //# define PJ_ASSERT_ON_FAIL(expr,exec_on_fail) pj_assert(expr) |
| 117 | //#endif |
| 118 | |
| 119 | /** @} */ |
| 120 | |
| 121 | #endif /* __PJ_ASSERT_H__ */ |
| 122 |