Home | History | Annotate | Download | only in foundation
      1 /*
      2  * Copyright (C) 2010 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  */
     16 
     17 #ifndef A_DEBUG_H_
     18 
     19 #define A_DEBUG_H_
     20 
     21 #include <string.h>
     22 
     23 #include <media/stagefright/foundation/ABase.h>
     24 #include <media/stagefright/foundation/AString.h>
     25 #include <utils/Log.h>
     26 
     27 namespace android {
     28 
     29 #define LITERAL_TO_STRING_INTERNAL(x)    #x
     30 #define LITERAL_TO_STRING(x) LITERAL_TO_STRING_INTERNAL(x)
     31 
     32 #define CHECK(condition)                                \
     33     LOG_ALWAYS_FATAL_IF(                                \
     34             !(condition),                               \
     35             "%s",                                       \
     36             __FILE__ ":" LITERAL_TO_STRING(__LINE__)    \
     37             " CHECK(" #condition ") failed.")
     38 
     39 #define MAKE_COMPARATOR(suffix,op)                          \
     40     template<class A, class B>                              \
     41     AString Compare_##suffix(const A &a, const B &b) {      \
     42         AString res;                                        \
     43         if (!(a op b)) {                                    \
     44             res.append(a);                                  \
     45             res.append(" vs. ");                            \
     46             res.append(b);                                  \
     47         }                                                   \
     48         return res;                                         \
     49     }
     50 
     51 MAKE_COMPARATOR(EQ,==)
     52 MAKE_COMPARATOR(NE,!=)
     53 MAKE_COMPARATOR(LE,<=)
     54 MAKE_COMPARATOR(GE,>=)
     55 MAKE_COMPARATOR(LT,<)
     56 MAKE_COMPARATOR(GT,>)
     57 
     58 #define CHECK_OP(x,y,suffix,op)                                         \
     59     do {                                                                \
     60         AString ___res = Compare_##suffix(x, y);                        \
     61         if (!___res.empty()) {                                          \
     62             AString ___full =                                           \
     63                 __FILE__ ":" LITERAL_TO_STRING(__LINE__)                \
     64                     " CHECK_" #suffix "( " #x "," #y ") failed: ";      \
     65             ___full.append(___res);                                     \
     66                                                                         \
     67             LOG_ALWAYS_FATAL("%s", ___full.c_str());                    \
     68         }                                                               \
     69     } while (false)
     70 
     71 #define CHECK_EQ(x,y)   CHECK_OP(x,y,EQ,==)
     72 #define CHECK_NE(x,y)   CHECK_OP(x,y,NE,!=)
     73 #define CHECK_LE(x,y)   CHECK_OP(x,y,LE,<=)
     74 #define CHECK_LT(x,y)   CHECK_OP(x,y,LT,<)
     75 #define CHECK_GE(x,y)   CHECK_OP(x,y,GE,>=)
     76 #define CHECK_GT(x,y)   CHECK_OP(x,y,GT,>)
     77 
     78 #define TRESPASS() \
     79         LOG_ALWAYS_FATAL(                                       \
     80             __FILE__ ":" LITERAL_TO_STRING(__LINE__)            \
     81                 " Should not be here.");
     82 
     83 struct ADebug {
     84     enum Level {
     85         kDebugNone,             // no debug
     86         kDebugLifeCycle,        // lifecycle events: creation/deletion
     87         kDebugState,            // commands and events
     88         kDebugConfig,           // configuration
     89         kDebugInternalState,    // internal state changes
     90         kDebugAll,              // all
     91         kDebugMax = kDebugAll,
     92 
     93     };
     94 
     95     // parse the property or string to get the debug level for a component name
     96     // string format is:
     97     // <level>[:<glob>][,<level>[:<glob>]...]
     98     // - <level> is 0-5 corresponding to ADebug::Level
     99     // - <glob> is used to match component name case insensitively, if omitted, it
    100     //   matches all components
    101     // - string is read left-to-right, and the last matching level is returned, or
    102     //   the def if no terms matched
    103     static Level GetDebugLevelFromProperty(
    104             const char *name, const char *propertyName, Level def = kDebugNone);
    105     static Level GetDebugLevelFromString(
    106             const char *name, const char *value, Level def = kDebugNone);
    107 
    108     // remove redundant segments of a codec name, and return a newly allocated
    109     // string suitable for debugging
    110     static char *GetDebugName(const char *name);
    111 };
    112 
    113 }  // namespace android
    114 
    115 #endif  // A_DEBUG_H_
    116 
    117