android 中CHECK宏分析

来源:互联网 发布:什么是采集软件 编辑:程序博客网 时间:2024/04/30 02:59

看到CHECK宏,开始以为和ASSERT一样,在release中不起作用。实际上是起作用的,会引起退出。下面是具体的源代码分析

 32 #define CHECK(condition)                                \ 33     LOG_ALWAYS_FATAL_IF(                                \ 34             !(condition),                               \ 35             "%s",                                       \ 36             __FILE__ ":" LITERAL_TO_STRING(__LINE__)    \ 37             " CHECK(" #condition ") failed.")302 /*303  * Log a fatal error.  If the given condition fails, this stops program304  * execution like a normal assertion, but also generating the given message.305  * It is NOT stripped from release builds.  Note that the condition test306  * is -inverted- from the normal assert() semantics.307  */308 #ifndef LOG_ALWAYS_FATAL_IF309 #define LOG_ALWAYS_FATAL_IF(cond, ...) \310     ( (CONDITION(cond)) \311     ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \312     : (void)0 )313 #endif470 #define android_printAssert(cond, tag, fmt...) \471     __android_log_assert(cond, tag, \472         __android_second(0, ## fmt, NULL) __android_rest(fmt))116 void __android_log_assert(const char *cond, const char *tag,117               const char *fmt, ...)118 #if defined(__GNUC__)119     __attribute__ ((noreturn))120     __attribute__ ((format(printf, 3, 4)))121 #endif122     ;223 void __android_log_assert(const char *cond, const char *tag,224               const char *fmt, ...)225 {226     char buf[LOG_BUF_SIZE];227228     if (fmt) {229         va_list ap;230         va_start(ap, fmt);231         vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);232         va_end(ap);233     } else {234         /* Msg not provided, log condition.  N.B. Do not use cond directly as235          * format string as it could contain spurious '%' syntax (e.g.236          * "%d" in "blocks%devs == 0").237          */238         if (cond)239             snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);240         else241             strcpy(buf, "Unspecified assertion failed");242     }243244     __android_log_write(ANDROID_LOG_FATAL, tag, buf);245246     __builtin_trap(); /* trap so we have a chance to debug the situation */247 }


原创粉丝点击