Home | History | Annotate | Download | only in include
      1 /*
      2  * ALOG Levels: F - Fatal, E - Error, W - Warning, I - Info, D - Debug, V - Verbose
      3  *
      4  * Using them to work within the Android logcat logging mechanism:
      5  *
      6  *    % logcat '*:v'                    [To display Verbose Logging]
      7  *    % logcat 'fcntl_portable:v'       [To display just this fcntl logging]
      8  *
      9  * NOTE: This assumes you only use the 'PORTABLE_TAG'; which is the default.
     10  *       For debugging LTP it's been helpful to include the LTP program being tested;
     11  *       which is enabled below with #define EXTENDED_LOGGING.
     12  *
     13  * Logging routines also support ALOG*_IF() and ASSERT(); For details See:
     14  *
     15  *       ${ANDROID_TOP}/system/core/include/cutils/log.h
     16  * and
     17  *        http://developer.android.com/tools/debugging/debugging-log.html
     18  *
     19  * ALOGV is turned off by release builds: Use the #define below with LOG_NDEBUG=0 to enable.
     20  *
     21  * Strace works fine with ALOG out if a large max string size is used via the -s option;
     22  * Example:
     23  *
     24  *   strace -s 132 ./sigaction01
     25  *
     26  *   writev(3, [{"\2", 1},
     27  *      {"./sigaction01`signal_portable\0", 30},
     28  *      {"sigaction_portable(portable_signum:10:'SIGUSR1_PORTABLE:10', ...
     29  *      {"map_portable_sigset_to_mips(portable_sigset:0x7fe47a0c, ...
     30  *      ...
     31  */
     32 
     33  /*
     34   * Enable LOG_NDEBUG to have debug code visible in logcat output by default.
     35   * Also possible via the Lib-Portable Android.mk  file. Example:
     36   *
     37   *     # Have logging permanently enable during development.
     38   *     LOCAL_CFLAGS += -DLOG_NDEBUG=0
     39   */
     40 // # define LOG_NDEBUG 0
     41 
     42 
     43 // #define EXTENDED_LOGGING        /* Include the current program name in the LOG_TAG */
     44 #ifdef EXTENDED_LOGGING
     45 /*
     46  * Inline function to put the current program name
     47  * and this library into the logcat prefix. Example:
     48  *
     49  *    V/./sigaction01`signal_portable(605): sigaction_portable(... ) {
     50  *      -----------------------------
     51  *
     52  * Disabled by default in AOSP, enable by removing the // above.
     53  * Useful when debugging more than one program; For example LTP has thousands.
     54  */
     55 #define MAX_TAG_LEN 128
     56 static char my_portable_tag[MAX_TAG_LEN + 1];
     57 
     58 static inline char *portable_tag() {
     59     extern char *__progname;
     60 
     61     if (my_portable_tag[0] == '\000') {
     62         strncat(&my_portable_tag[0], __progname, MAX_TAG_LEN);
     63         strncat(&my_portable_tag[0], ".", MAX_TAG_LEN - strlen(my_portable_tag));
     64         strncat(&my_portable_tag[0], PORTABLE_TAG, MAX_TAG_LEN - strlen(my_portable_tag));
     65     }
     66     return my_portable_tag;
     67 }
     68 #define LOG_TAG  portable_tag()
     69 #else /* !EXTENDED_LOGGING */
     70 #define LOG_TAG PORTABLE_TAG
     71 #endif
     72 
     73 /*
     74  * Override LOG_PRI() defined in ${AOSP}/system/core/include/cutils/log.h
     75  * to preserve the value of errno while logging.
     76  */
     77 #define LOG_PRI(priority, tag, ...) ({                      \
     78     int _errno = *REAL(__errno)();                          \
     79     int _rv = android_printLog(priority, tag, __VA_ARGS__); \
     80     *REAL(__errno)() = _errno;                              \
     81     _rv;                   /* Returned to caller */         \
     82 })
     83 
     84 #if !defined(__HOST__)
     85 #include <cutils/log.h>
     86 
     87 # define PERROR(str)  {                                                                  \
     88     ALOGE("%s: PERROR('%s'): errno:%d:'%s'", __func__, str, *REAL(__errno)(), strerror(errno)); \
     89 }
     90 
     91 # define ASSERT(cond) ALOG_ASSERT(cond, "assertion failed:(%s), file: %s, line: %d:%s",  \
     92                                  #cond, __FILE__, __LINE__, __func__);
     93 #else
     94 #include <assert.h>
     95 # define PERROR(str) fprintf(stderr, "%s: PERROR('%s'): errno:%d:'%s'", __func__, str, *REAL(__errno)(), strerror(*REAL(__errno)()))
     96 # define ASSERT(cond) assert(cond)
     97 # define ALOGV(a,...)
     98 # define ALOGW(a,...)
     99 # define ALOGE(a,...)
    100 
    101 #endif