• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 inline static const char *asString(status_t i, const char *def = "??") {
30     switch (i) {
31         case NO_ERROR:              return "NO_ERROR";
32         case UNKNOWN_ERROR:         return "UNKNOWN_ERROR";
33         case NO_MEMORY:             return "NO_MEMORY";
34         case INVALID_OPERATION:     return "INVALID_OPERATION";
35         case BAD_VALUE:             return "BAD_VALUE";
36         case BAD_TYPE:              return "BAD_TYPE";
37         case NAME_NOT_FOUND:        return "NAME_NOT_FOUND";
38         case PERMISSION_DENIED:     return "PERMISSION_DENIED";
39         case NO_INIT:               return "NO_INIT";
40         case ALREADY_EXISTS:        return "ALREADY_EXISTS";
41         case DEAD_OBJECT:           return "DEAD_OBJECT";
42         case FAILED_TRANSACTION:    return "FAILED_TRANSACTION";
43         case BAD_INDEX:             return "BAD_INDEX";
44         case NOT_ENOUGH_DATA:       return "NOT_ENOUGH_DATA";
45         case WOULD_BLOCK:           return "WOULD_BLOCK";
46         case TIMED_OUT:             return "TIMED_OUT";
47         case UNKNOWN_TRANSACTION:   return "UNKNOWN_TRANSACTION";
48         case FDS_NOT_ALLOWED:       return "FDS_NOT_ALLOWED";
49         default:                    return def;
50     }
51 }
52 
53 #define LITERAL_TO_STRING_INTERNAL(x)    #x
54 #define LITERAL_TO_STRING(x) LITERAL_TO_STRING_INTERNAL(x)
55 
56 #ifdef CHECK
57 #undef CHECK
58 #endif
59 #define CHECK(condition)                                \
60     LOG_ALWAYS_FATAL_IF(                                \
61             !(condition),                               \
62             "%s",                                       \
63             __FILE__ ":" LITERAL_TO_STRING(__LINE__)    \
64             " CHECK(" #condition ") failed.")
65 
66 #define MAKE_COMPARATOR(suffix,op)                          \
67     template<class A, class B>                              \
68     AString Compare_##suffix(const A &a, const B &b) {      \
69         AString res;                                        \
70         if (!(a op b)) {                                    \
71             res.append(a);                                  \
72             res.append(" vs. ");                            \
73             res.append(b);                                  \
74         }                                                   \
75         return res;                                         \
76     }
77 
78 MAKE_COMPARATOR(EQ,==)
79 MAKE_COMPARATOR(NE,!=)
80 MAKE_COMPARATOR(LE,<=)
81 MAKE_COMPARATOR(GE,>=)
82 MAKE_COMPARATOR(LT,<)
83 MAKE_COMPARATOR(GT,>)
84 
85 #define CHECK_OP(x,y,suffix,op)                                         \
86     do {                                                                \
87         AString ___res = Compare_##suffix(x, y);                        \
88         if (!___res.empty()) {                                          \
89             AString ___full =                                           \
90                 __FILE__ ":" LITERAL_TO_STRING(__LINE__)                \
91                     " CHECK_" #suffix "( " #x "," #y ") failed: ";      \
92             ___full.append(___res);                                     \
93                                                                         \
94             LOG_ALWAYS_FATAL("%s", ___full.c_str());                    \
95         }                                                               \
96     } while (false)
97 
98 #ifdef CHECK_EQ
99 #undef CHECK_EQ
100 #undef CHECK_NE
101 #undef CHECK_LE
102 #undef CHECK_LT
103 #undef CHECK_GE
104 #undef CHECK_GT
105 #endif
106 
107 #define CHECK_EQ(x,y)   CHECK_OP(x,y,EQ,==)
108 #define CHECK_NE(x,y)   CHECK_OP(x,y,NE,!=)
109 #define CHECK_LE(x,y)   CHECK_OP(x,y,LE,<=)
110 #define CHECK_LT(x,y)   CHECK_OP(x,y,LT,<)
111 #define CHECK_GE(x,y)   CHECK_OP(x,y,GE,>=)
112 #define CHECK_GT(x,y)   CHECK_OP(x,y,GT,>)
113 
114 #define TRESPASS(...) \
115         LOG_ALWAYS_FATAL(                                       \
116             __FILE__ ":" LITERAL_TO_STRING(__LINE__)            \
117                 " Should not be here. " __VA_ARGS__);
118 
119 #ifdef NDEBUG
120 #define CHECK_DBG CHECK
121 #define CHECK_EQ_DBG CHECK_EQ
122 #define CHECK_NE_DBG CHECK_NE
123 #define CHECK_LE_DBG CHECK_LE
124 #define CHECK_LT_DBG CHECK_LT
125 #define CHECK_GE_DBG CHECK_GE
126 #define CHECK_GT_DBG CHECK_GT
127 #define TRESPASS_DBG TRESPASS
128 #else
129 #define CHECK_DBG(condition)
130 #define CHECK_EQ_DBG(x,y)
131 #define CHECK_NE_DBG(x,y)
132 #define CHECK_LE_DBG(x,y)
133 #define CHECK_LT_DBG(x,y)
134 #define CHECK_GE_DBG(x,y)
135 #define CHECK_GT_DBG(x,y)
136 #define TRESPASS_DBG(...)
137 #endif
138 
139 struct ADebug {
140     enum Level {
141         kDebugNone,             // no debug
142         kDebugLifeCycle,        // lifecycle events: creation/deletion
143         kDebugState,            // commands and events
144         kDebugConfig,           // configuration
145         kDebugInternalState,    // internal state changes
146         kDebugAll,              // all
147         kDebugMax = kDebugAll,
148 
149     };
150 
151     // parse the property or string to get a long-type level for a component name
152     // string format is:
153     // <level>[:<glob>][,<level>[:<glob>]...]
154     // - <level> is 0-5 corresponding to ADebug::Level
155     // - <glob> is used to match component name case insensitively, if omitted, it
156     //   matches all components
157     // - string is read left-to-right, and the last matching level is returned, or
158     //   the def if no terms matched
159     static long GetLevelFromSettingsString(
160             const char *name, const char *value, long def);
161     static long GetLevelFromProperty(
162             const char *name, const char *value, long def);
163 
164     // same for ADebug::Level - performs clamping to valid debug ranges
165     static Level GetDebugLevelFromProperty(
166             const char *name, const char *propertyName, Level def = kDebugNone);
167 
168     // remove redundant segments of a codec name, and return a newly allocated
169     // string suitable for debugging
170     static char *GetDebugName(const char *name);
171 
172     inline static bool isExperimentEnabled(
173             const char *name __unused /* nonnull */, bool allow __unused = true) {
174 #ifdef ENABLE_STAGEFRIGHT_EXPERIMENTS
175         if (!strcmp(name, "legacy-adaptive")) {
176             return getExperimentFlag(allow, name, 2, 1); // every other day
177         } else if (!strcmp(name, "legacy-setsurface")) {
178             return getExperimentFlag(allow, name, 3, 1); // every third day
179         } else {
180             ALOGE("unknown experiment '%s' (disabled)", name);
181         }
182 #endif
183         return false;
184     }
185 
186 private:
187     // pass in allow, so we can print in the log if the experiment is disabled
188     static bool getExperimentFlag(
189             bool allow, const char *name, uint64_t modulo, uint64_t limit,
190             uint64_t plus = 0, uint64_t timeDivisor = 24 * 60 * 60 /* 1 day */);
191 };
192 
193 }  // namespace android
194 
195 #endif  // A_DEBUG_H_
196 
197