Home | History | Annotate | Download | only in utils
      1 /* Copyright (C) 2009 The Android Open Source Project
      2 **
      3 ** This software is licensed under the terms of the GNU General Public
      4 ** License version 2, as published by the Free Software Foundation, and
      5 ** may be copied, distributed, and modified under those terms.
      6 **
      7 ** This program is distributed in the hope that it will be useful,
      8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 ** GNU General Public License for more details.
     11 */
     12 #include "android/utils/assert.h"
     13 #include "android/utils/panic.h"
     14 #include <stdio.h>
     15 
     16 typedef struct {
     17     const char*  file;
     18     long         lineno;
     19     const char*  function;
     20 } AssertLoc;
     21 
     22 AssertLoc*
     23 _get_assert_loc(void)
     24 {
     25     /* XXX: Use thread-local storage instead ? */
     26     static AssertLoc  loc[1];
     27     return loc;
     28 }
     29 
     30 void
     31 _android_assert_loc( const char*  fileName,
     32                      long         fileLineno,
     33                      const char*  functionName )
     34 {
     35     AssertLoc*  loc = _get_assert_loc();
     36 
     37     loc->file     = fileName;
     38     loc->lineno   = fileLineno;
     39     loc->function = functionName;
     40 }
     41 
     42 static void
     43 _android_assert_log_default( const char*  fmt, va_list  args )
     44 {
     45     vfprintf(stderr, fmt, args);
     46 }
     47 
     48 static AAssertLogFunc  _assert_log = _android_assert_log_default;
     49 
     50 void  android_assert_fail(const char*  messageFmt, ...)
     51 {
     52     AssertLoc*  loc = _get_assert_loc();
     53     va_list  args;
     54 
     55     va_start(args, messageFmt);
     56     _assert_log(messageFmt, args);
     57     va_end(args);
     58 
     59     android_panic("ASSERTION FAILURE (%s:%d) in %s\n", loc->file, loc->lineno, loc->function);
     60 }
     61 
     62 void  android_assert_registerLog( AAssertLogFunc  logger )
     63 {
     64     if (logger == NULL)
     65         android_panic("Passing NULL to %s\n", __FUNCTION__);
     66 
     67     _assert_log = logger;
     68 }
     69