widget/gonk/libui/cutils_log.h

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /*
     2  * Copyright (C) 2005 The Android Open Source Project
     3  *
     4  * Licensed under the Apache License, Version 2.0 (the "License");
     5  * you may not use this file except in compliance with the License.
     6  * You may obtain a copy of the License at
     7  *
     8  *      http://www.apache.org/licenses/LICENSE-2.0
     9  *
    10  * Unless required by applicable law or agreed to in writing, software
    11  * distributed under the License is distributed on an "AS IS" BASIS,
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  * See the License for the specific language governing permissions and
    14  * limitations under the License.
    15  */
    17 //
    18 // C/C++ logging functions.  See the logging documentation for API details.
    19 //
    20 // We'd like these to be available from C code (in case we import some from
    21 // somewhere), so this has a C interface.
    22 //
    23 // The output will be correct when the log file is shared between multiple
    24 // threads and/or multiple processes so long as the operating system
    25 // supports O_APPEND.  These calls have mutex-protected data structures
    26 // and so are NOT reentrant.  Do not use LOG in a signal handler.
    27 //
    28 #if !defined(_LIBS_CUTILS_LOG_H) && !defined(_LIBS_LOG_LOG_H)
    29 #define _LIBS_LOG_LOG_H
    30 #define _LIBS_CUTILS_LOG_H
    32 #include <stdio.h>
    33 #include <time.h>
    34 #include <sys/types.h>
    35 #include <unistd.h>
    36 #ifdef HAVE_PTHREADS
    37 #include <pthread.h>
    38 #endif
    39 #include <stdarg.h>
    41 #if ANDROID_VERSION >= 19
    42 #include <log/uio.h>
    43 #include <log/logd.h>
    44 #else
    45 #include <cutils/uio.h>
    46 #include <cutils/logd.h>
    47 #endif
    49 #ifdef __cplusplus
    50 extern "C" {
    51 #endif
    53 // ---------------------------------------------------------------------
    55 /*
    56  * Normally we strip ALOGV (VERBOSE messages) from release builds.
    57  * You can modify this (for example with "#define LOG_NDEBUG 0"
    58  * at the top of your source file) to change that behavior.
    59  */
    60 #ifndef LOG_NDEBUG
    61 #ifdef NDEBUG
    62 #define LOG_NDEBUG 1
    63 #else
    64 #define LOG_NDEBUG 0
    65 #endif
    66 #endif
    68 /*
    69  * This is the local tag used for the following simplified
    70  * logging macros.  You can change this preprocessor definition
    71  * before using the other macros to change the tag.
    72  */
    73 #ifndef LOG_TAG
    74 #define LOG_TAG NULL
    75 #endif
    77 // ---------------------------------------------------------------------
    79 /*
    80  * Simplified macro to send a verbose log message using the current LOG_TAG.
    81  */
    82 #ifndef ALOGV
    83 #if LOG_NDEBUG
    84 #define ALOGV(...)   ((void)0)
    85 #else
    86 #define ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
    87 #endif
    88 #endif
    90 #define CONDITION(cond)     (__builtin_expect((cond)!=0, 0))
    92 #ifndef ALOGV_IF
    93 #if LOG_NDEBUG
    94 #define ALOGV_IF(cond, ...)   ((void)0)
    95 #else
    96 #define ALOGV_IF(cond, ...) \
    97     ( (CONDITION(cond)) \
    98     ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
    99     : (void)0 )
   100 #endif
   101 #endif
   103 /*
   104  * Simplified macro to send a debug log message using the current LOG_TAG.
   105  */
   106 #ifndef ALOGD
   107 #define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
   108 #endif
   110 #ifndef ALOGD_IF
   111 #define ALOGD_IF(cond, ...) \
   112     ( (CONDITION(cond)) \
   113     ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
   114     : (void)0 )
   115 #endif
   117 /*
   118  * Simplified macro to send an info log message using the current LOG_TAG.
   119  */
   120 #ifndef ALOGI
   121 #define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
   122 #endif
   124 #ifndef ALOGI_IF
   125 #define ALOGI_IF(cond, ...) \
   126     ( (CONDITION(cond)) \
   127     ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
   128     : (void)0 )
   129 #endif
   131 /*
   132  * Simplified macro to send a warning log message using the current LOG_TAG.
   133  */
   134 #ifndef ALOGW
   135 #define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
   136 #endif
   138 #ifndef ALOGW_IF
   139 #define ALOGW_IF(cond, ...) \
   140     ( (CONDITION(cond)) \
   141     ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
   142     : (void)0 )
   143 #endif
   145 /*
   146  * Simplified macro to send an error log message using the current LOG_TAG.
   147  */
   148 #ifndef ALOGE
   149 #define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
   150 #endif
   152 #ifndef ALOGE_IF
   153 #define ALOGE_IF(cond, ...) \
   154     ( (CONDITION(cond)) \
   155     ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
   156     : (void)0 )
   157 #endif
   159 // ---------------------------------------------------------------------
   161 /*
   162  * Conditional based on whether the current LOG_TAG is enabled at
   163  * verbose priority.
   164  */
   165 #ifndef IF_ALOGV
   166 #if LOG_NDEBUG
   167 #define IF_ALOGV() if (false)
   168 #else
   169 #define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)
   170 #endif
   171 #endif
   173 /*
   174  * Conditional based on whether the current LOG_TAG is enabled at
   175  * debug priority.
   176  */
   177 #ifndef IF_ALOGD
   178 #define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)
   179 #endif
   181 /*
   182  * Conditional based on whether the current LOG_TAG is enabled at
   183  * info priority.
   184  */
   185 #ifndef IF_ALOGI
   186 #define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)
   187 #endif
   189 /*
   190  * Conditional based on whether the current LOG_TAG is enabled at
   191  * warn priority.
   192  */
   193 #ifndef IF_ALOGW
   194 #define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)
   195 #endif
   197 /*
   198  * Conditional based on whether the current LOG_TAG is enabled at
   199  * error priority.
   200  */
   201 #ifndef IF_ALOGE
   202 #define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)
   203 #endif
   206 // ---------------------------------------------------------------------
   208 /*
   209  * Simplified macro to send a verbose system log message using the current LOG_TAG.
   210  */
   211 #ifndef SLOGV
   212 #if LOG_NDEBUG
   213 #define SLOGV(...)   ((void)0)
   214 #else
   215 #define SLOGV(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
   216 #endif
   217 #endif
   219 #define CONDITION(cond)     (__builtin_expect((cond)!=0, 0))
   221 #ifndef SLOGV_IF
   222 #if LOG_NDEBUG
   223 #define SLOGV_IF(cond, ...)   ((void)0)
   224 #else
   225 #define SLOGV_IF(cond, ...) \
   226     ( (CONDITION(cond)) \
   227     ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
   228     : (void)0 )
   229 #endif
   230 #endif
   232 /*
   233  * Simplified macro to send a debug system log message using the current LOG_TAG.
   234  */
   235 #ifndef SLOGD
   236 #define SLOGD(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
   237 #endif
   239 #ifndef SLOGD_IF
   240 #define SLOGD_IF(cond, ...) \
   241     ( (CONDITION(cond)) \
   242     ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
   243     : (void)0 )
   244 #endif
   246 /*
   247  * Simplified macro to send an info system log message using the current LOG_TAG.
   248  */
   249 #ifndef SLOGI
   250 #define SLOGI(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
   251 #endif
   253 #ifndef SLOGI_IF
   254 #define SLOGI_IF(cond, ...) \
   255     ( (CONDITION(cond)) \
   256     ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
   257     : (void)0 )
   258 #endif
   260 /*
   261  * Simplified macro to send a warning system log message using the current LOG_TAG.
   262  */
   263 #ifndef SLOGW
   264 #define SLOGW(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
   265 #endif
   267 #ifndef SLOGW_IF
   268 #define SLOGW_IF(cond, ...) \
   269     ( (CONDITION(cond)) \
   270     ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
   271     : (void)0 )
   272 #endif
   274 /*
   275  * Simplified macro to send an error system log message using the current LOG_TAG.
   276  */
   277 #ifndef SLOGE
   278 #define SLOGE(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
   279 #endif
   281 #ifndef SLOGE_IF
   282 #define SLOGE_IF(cond, ...) \
   283     ( (CONDITION(cond)) \
   284     ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
   285     : (void)0 )
   286 #endif
   288 // ---------------------------------------------------------------------
   290 /*
   291  * Simplified macro to send a verbose radio log message using the current LOG_TAG.
   292  */
   293 #ifndef RLOGV
   294 #if LOG_NDEBUG
   295 #define RLOGV(...)   ((void)0)
   296 #else
   297 #define RLOGV(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
   298 #endif
   299 #endif
   301 #define CONDITION(cond)     (__builtin_expect((cond)!=0, 0))
   303 #ifndef RLOGV_IF
   304 #if LOG_NDEBUG
   305 #define RLOGV_IF(cond, ...)   ((void)0)
   306 #else
   307 #define RLOGV_IF(cond, ...) \
   308     ( (CONDITION(cond)) \
   309     ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
   310     : (void)0 )
   311 #endif
   312 #endif
   314 /*
   315  * Simplified macro to send a debug radio log message using the current LOG_TAG.
   316  */
   317 #ifndef RLOGD
   318 #define RLOGD(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
   319 #endif
   321 #ifndef RLOGD_IF
   322 #define RLOGD_IF(cond, ...) \
   323     ( (CONDITION(cond)) \
   324     ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
   325     : (void)0 )
   326 #endif
   328 /*
   329  * Simplified macro to send an info radio log message using the current LOG_TAG.
   330  */
   331 #ifndef RLOGI
   332 #define RLOGI(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
   333 #endif
   335 #ifndef RLOGI_IF
   336 #define RLOGI_IF(cond, ...) \
   337     ( (CONDITION(cond)) \
   338     ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
   339     : (void)0 )
   340 #endif
   342 /*
   343  * Simplified macro to send a warning radio log message using the current LOG_TAG.
   344  */
   345 #ifndef RLOGW
   346 #define RLOGW(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
   347 #endif
   349 #ifndef RLOGW_IF
   350 #define RLOGW_IF(cond, ...) \
   351     ( (CONDITION(cond)) \
   352     ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
   353     : (void)0 )
   354 #endif
   356 /*
   357  * Simplified macro to send an error radio log message using the current LOG_TAG.
   358  */
   359 #ifndef RLOGE
   360 #define RLOGE(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
   361 #endif
   363 #ifndef RLOGE_IF
   364 #define RLOGE_IF(cond, ...) \
   365     ( (CONDITION(cond)) \
   366     ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
   367     : (void)0 )
   368 #endif
   371 // ---------------------------------------------------------------------
   373 /*
   374  * Log a fatal error.  If the given condition fails, this stops program
   375  * execution like a normal assertion, but also generating the given message.
   376  * It is NOT stripped from release builds.  Note that the condition test
   377  * is -inverted- from the normal assert() semantics.
   378  */
   379 #ifndef LOG_ALWAYS_FATAL_IF
   380 #define LOG_ALWAYS_FATAL_IF(cond, ...) \
   381     ( (CONDITION(cond)) \
   382     ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \
   383     : (void)0 )
   384 #endif
   386 #ifndef LOG_ALWAYS_FATAL
   387 #define LOG_ALWAYS_FATAL(...) \
   388     ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) )
   389 #endif
   391 /*
   392  * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that
   393  * are stripped out of release builds.
   394  */
   395 #if LOG_NDEBUG
   397 #ifndef LOG_FATAL_IF
   398 #define LOG_FATAL_IF(cond, ...) ((void)0)
   399 #endif
   400 #ifndef LOG_FATAL
   401 #define LOG_FATAL(...) ((void)0)
   402 #endif
   404 #else
   406 #ifndef LOG_FATAL_IF
   407 #define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__)
   408 #endif
   409 #ifndef LOG_FATAL
   410 #define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
   411 #endif
   413 #endif
   415 /*
   416  * Assertion that generates a log message when the assertion fails.
   417  * Stripped out of release builds.  Uses the current LOG_TAG.
   418  */
   419 #ifndef ALOG_ASSERT
   420 #define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__)
   421 //#define ALOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond)
   422 #endif
   424 // ---------------------------------------------------------------------
   426 /*
   427  * Basic log message macro.
   428  *
   429  * Example:
   430  *  ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
   431  *
   432  * The second argument may be NULL or "" to indicate the "global" tag.
   433  */
   434 #ifndef ALOG
   435 #define ALOG(priority, tag, ...) \
   436     LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
   437 #endif
   439 /*
   440  * Log macro that allows you to specify a number for the priority.
   441  */
   442 #ifndef LOG_PRI
   443 #define LOG_PRI(priority, tag, ...) \
   444     android_printLog(priority, tag, __VA_ARGS__)
   445 #endif
   447 /*
   448  * Log macro that allows you to pass in a varargs ("args" is a va_list).
   449  */
   450 #ifndef LOG_PRI_VA
   451 #define LOG_PRI_VA(priority, tag, fmt, args) \
   452     android_vprintLog(priority, NULL, tag, fmt, args)
   453 #endif
   455 /*
   456  * Conditional given a desired logging priority and tag.
   457  */
   458 #ifndef IF_ALOG
   459 #define IF_ALOG(priority, tag) \
   460     if (android_testLog(ANDROID_##priority, tag))
   461 #endif
   463 // ---------------------------------------------------------------------
   465 /*
   466  * Event logging.
   467  */
   469 /*
   470  * Event log entry types.  These must match up with the declarations in
   471  * java/android/android/util/EventLog.java.
   472  */
   473 typedef enum {
   474     EVENT_TYPE_INT      = 0,
   475     EVENT_TYPE_LONG     = 1,
   476     EVENT_TYPE_STRING   = 2,
   477     EVENT_TYPE_LIST     = 3,
   478 } AndroidEventLogType;
   481 #ifndef LOG_EVENT_INT
   482 #define LOG_EVENT_INT(_tag, _value) {                                       \
   483         int intBuf = _value;                                                \
   484         (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf,            \
   485             sizeof(intBuf));                                                \
   486     }
   487 #endif
   488 #ifndef LOG_EVENT_LONG
   489 #define LOG_EVENT_LONG(_tag, _value) {                                      \
   490         long long longBuf = _value;                                         \
   491         (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf,          \
   492             sizeof(longBuf));                                               \
   493     }
   494 #endif
   495 #ifndef LOG_EVENT_STRING
   496 #define LOG_EVENT_STRING(_tag, _value)                                      \
   497     ((void) 0)  /* not implemented -- must combine len with string */
   498 #endif
   499 /* TODO: something for LIST */
   501 /*
   502  * ===========================================================================
   503  *
   504  * The stuff in the rest of this file should not be used directly.
   505  */
   507 #define android_printLog(prio, tag, fmt...) \
   508     __android_log_print(prio, tag, fmt)
   510 #define android_vprintLog(prio, cond, tag, fmt...) \
   511     __android_log_vprint(prio, tag, fmt)
   513 /* XXX Macros to work around syntax errors in places where format string
   514  * arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF
   515  * (happens only in debug builds).
   516  */
   518 /* Returns 2nd arg.  Used to substitute default value if caller's vararg list
   519  * is empty.
   520  */
   521 #define __android_second(dummy, second, ...)     second
   523 /* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
   524  * returns nothing.
   525  */
   526 #define __android_rest(first, ...)               , ## __VA_ARGS__
   528 #define android_printAssert(cond, tag, fmt...) \
   529     __android_log_assert(cond, tag, \
   530         __android_second(0, ## fmt, NULL) __android_rest(fmt))
   532 #define android_writeLog(prio, tag, text) \
   533     __android_log_write(prio, tag, text)
   535 #define android_bWriteLog(tag, payload, len) \
   536     __android_log_bwrite(tag, payload, len)
   537 #define android_btWriteLog(tag, type, payload, len) \
   538     __android_log_btwrite(tag, type, payload, len)
   540 // TODO: remove these prototypes and their users
   541 #define android_testLog(prio, tag) (1)
   542 #define android_writevLog(vec,num) do{}while(0)
   543 #define android_write1Log(str,len) do{}while (0)
   544 #define android_setMinPriority(tag, prio) do{}while(0)
   545 //#define android_logToCallback(func) do{}while(0)
   546 #define android_logToFile(tag, file) (0)
   547 #define android_logToFd(tag, fd) (0)
   549 typedef enum {
   550     LOG_ID_MAIN = 0,
   551     LOG_ID_RADIO = 1,
   552     LOG_ID_EVENTS = 2,
   553     LOG_ID_SYSTEM = 3,
   555     LOG_ID_MAX
   556 } log_id_t;
   558 /*
   559  * Send a simple string to the log.
   560  */
   561 int __android_log_buf_write(int bufID, int prio, const char *tag, const char *text);
   562 int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...);
   565 #ifdef __cplusplus
   566 }
   567 #endif
   569 #endif // _LIBS_CUTILS_LOG_H

mercurial