ADebug.h revision 054219874873b41f1c815552987c10465c34ba2b
172961230a5890071bcca436eb5630172ce84ec41Andreas Huber/*
272961230a5890071bcca436eb5630172ce84ec41Andreas Huber * Copyright (C) 2010 The Android Open Source Project
372961230a5890071bcca436eb5630172ce84ec41Andreas Huber *
472961230a5890071bcca436eb5630172ce84ec41Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
572961230a5890071bcca436eb5630172ce84ec41Andreas Huber * you may not use this file except in compliance with the License.
672961230a5890071bcca436eb5630172ce84ec41Andreas Huber * You may obtain a copy of the License at
772961230a5890071bcca436eb5630172ce84ec41Andreas Huber *
872961230a5890071bcca436eb5630172ce84ec41Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
972961230a5890071bcca436eb5630172ce84ec41Andreas Huber *
1072961230a5890071bcca436eb5630172ce84ec41Andreas Huber * Unless required by applicable law or agreed to in writing, software
1172961230a5890071bcca436eb5630172ce84ec41Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
1272961230a5890071bcca436eb5630172ce84ec41Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1372961230a5890071bcca436eb5630172ce84ec41Andreas Huber * See the License for the specific language governing permissions and
1472961230a5890071bcca436eb5630172ce84ec41Andreas Huber * limitations under the License.
1572961230a5890071bcca436eb5630172ce84ec41Andreas Huber */
1672961230a5890071bcca436eb5630172ce84ec41Andreas Huber
1772961230a5890071bcca436eb5630172ce84ec41Andreas Huber#ifndef A_DEBUG_H_
1872961230a5890071bcca436eb5630172ce84ec41Andreas Huber
1972961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define A_DEBUG_H_
2072961230a5890071bcca436eb5630172ce84ec41Andreas Huber
2172961230a5890071bcca436eb5630172ce84ec41Andreas Huber#include <string.h>
2272961230a5890071bcca436eb5630172ce84ec41Andreas Huber
2372961230a5890071bcca436eb5630172ce84ec41Andreas Huber#include <media/stagefright/foundation/ABase.h>
2472961230a5890071bcca436eb5630172ce84ec41Andreas Huber#include <media/stagefright/foundation/AString.h>
256e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber#include <utils/Log.h>
2672961230a5890071bcca436eb5630172ce84ec41Andreas Huber
27054219874873b41f1c815552987c10465c34ba2bLajos Molnarinline static const char *asString(android::status_t i, const char *def = "??") {
28054219874873b41f1c815552987c10465c34ba2bLajos Molnar    using namespace android;
29054219874873b41f1c815552987c10465c34ba2bLajos Molnar    switch (i) {
30054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case NO_ERROR:              return "NO_ERROR";
31054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case UNKNOWN_ERROR:         return "UNKNOWN_ERROR";
32054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case NO_MEMORY:             return "NO_MEMORY";
33054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case INVALID_OPERATION:     return "INVALID_OPERATION";
34054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case BAD_VALUE:             return "BAD_VALUE";
35054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case BAD_TYPE:              return "BAD_TYPE";
36054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case NAME_NOT_FOUND:        return "NAME_NOT_FOUND";
37054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case PERMISSION_DENIED:     return "PERMISSION_DENIED";
38054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case NO_INIT:               return "NO_INIT";
39054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case ALREADY_EXISTS:        return "ALREADY_EXISTS";
40054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case DEAD_OBJECT:           return "DEAD_OBJECT";
41054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case FAILED_TRANSACTION:    return "FAILED_TRANSACTION";
42054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case BAD_INDEX:             return "BAD_INDEX";
43054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case NOT_ENOUGH_DATA:       return "NOT_ENOUGH_DATA";
44054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case WOULD_BLOCK:           return "WOULD_BLOCK";
45054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case TIMED_OUT:             return "TIMED_OUT";
46054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case UNKNOWN_TRANSACTION:   return "UNKNOWN_TRANSACTION";
47054219874873b41f1c815552987c10465c34ba2bLajos Molnar        case FDS_NOT_ALLOWED:       return "FDS_NOT_ALLOWED";
48054219874873b41f1c815552987c10465c34ba2bLajos Molnar        default:                    return def;
49054219874873b41f1c815552987c10465c34ba2bLajos Molnar    }
50054219874873b41f1c815552987c10465c34ba2bLajos Molnar}
51054219874873b41f1c815552987c10465c34ba2bLajos Molnar
5272961230a5890071bcca436eb5630172ce84ec41Andreas Hubernamespace android {
5372961230a5890071bcca436eb5630172ce84ec41Andreas Huber
546e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber#define LITERAL_TO_STRING_INTERNAL(x)    #x
556e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber#define LITERAL_TO_STRING(x) LITERAL_TO_STRING_INTERNAL(x)
5672961230a5890071bcca436eb5630172ce84ec41Andreas Huber
5772961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK(condition)                                \
586e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    LOG_ALWAYS_FATAL_IF(                                \
596e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber            !(condition),                               \
60f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            "%s",                                       \
616e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber            __FILE__ ":" LITERAL_TO_STRING(__LINE__)    \
626e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber            " CHECK(" #condition ") failed.")
6372961230a5890071bcca436eb5630172ce84ec41Andreas Huber
6472961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define MAKE_COMPARATOR(suffix,op)                          \
6572961230a5890071bcca436eb5630172ce84ec41Andreas Huber    template<class A, class B>                              \
6672961230a5890071bcca436eb5630172ce84ec41Andreas Huber    AString Compare_##suffix(const A &a, const B &b) {      \
6772961230a5890071bcca436eb5630172ce84ec41Andreas Huber        AString res;                                        \
6872961230a5890071bcca436eb5630172ce84ec41Andreas Huber        if (!(a op b)) {                                    \
6972961230a5890071bcca436eb5630172ce84ec41Andreas Huber            res.append(a);                                  \
7072961230a5890071bcca436eb5630172ce84ec41Andreas Huber            res.append(" vs. ");                            \
7172961230a5890071bcca436eb5630172ce84ec41Andreas Huber            res.append(b);                                  \
7272961230a5890071bcca436eb5630172ce84ec41Andreas Huber        }                                                   \
7372961230a5890071bcca436eb5630172ce84ec41Andreas Huber        return res;                                         \
7472961230a5890071bcca436eb5630172ce84ec41Andreas Huber    }
7572961230a5890071bcca436eb5630172ce84ec41Andreas Huber
7672961230a5890071bcca436eb5630172ce84ec41Andreas HuberMAKE_COMPARATOR(EQ,==)
7772961230a5890071bcca436eb5630172ce84ec41Andreas HuberMAKE_COMPARATOR(NE,!=)
7872961230a5890071bcca436eb5630172ce84ec41Andreas HuberMAKE_COMPARATOR(LE,<=)
7972961230a5890071bcca436eb5630172ce84ec41Andreas HuberMAKE_COMPARATOR(GE,>=)
8072961230a5890071bcca436eb5630172ce84ec41Andreas HuberMAKE_COMPARATOR(LT,<)
8172961230a5890071bcca436eb5630172ce84ec41Andreas HuberMAKE_COMPARATOR(GT,>)
8272961230a5890071bcca436eb5630172ce84ec41Andreas Huber
8372961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_OP(x,y,suffix,op)                                         \
8472961230a5890071bcca436eb5630172ce84ec41Andreas Huber    do {                                                                \
8572961230a5890071bcca436eb5630172ce84ec41Andreas Huber        AString ___res = Compare_##suffix(x, y);                        \
8672961230a5890071bcca436eb5630172ce84ec41Andreas Huber        if (!___res.empty()) {                                          \
87f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            AString ___full =                                           \
88f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                __FILE__ ":" LITERAL_TO_STRING(__LINE__)                \
89f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    " CHECK_" #suffix "( " #x "," #y ") failed: ";      \
90f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            ___full.append(___res);                                     \
91f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                                                                        \
92f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            LOG_ALWAYS_FATAL("%s", ___full.c_str());                    \
9372961230a5890071bcca436eb5630172ce84ec41Andreas Huber        }                                                               \
9472961230a5890071bcca436eb5630172ce84ec41Andreas Huber    } while (false)
9572961230a5890071bcca436eb5630172ce84ec41Andreas Huber
9672961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_EQ(x,y)   CHECK_OP(x,y,EQ,==)
9772961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_NE(x,y)   CHECK_OP(x,y,NE,!=)
9872961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_LE(x,y)   CHECK_OP(x,y,LE,<=)
9972961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_LT(x,y)   CHECK_OP(x,y,LT,<)
10072961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_GE(x,y)   CHECK_OP(x,y,GE,>=)
10172961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_GT(x,y)   CHECK_OP(x,y,GT,>)
10272961230a5890071bcca436eb5630172ce84ec41Andreas Huber
103bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber#define TRESPASS() \
104bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        LOG_ALWAYS_FATAL(                                       \
105bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber            __FILE__ ":" LITERAL_TO_STRING(__LINE__)            \
106bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber                " Should not be here.");
10772961230a5890071bcca436eb5630172ce84ec41Andreas Huber
108f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnarstruct ADebug {
109f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    enum Level {
110f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugNone,             // no debug
111f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugLifeCycle,        // lifecycle events: creation/deletion
112f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugState,            // commands and events
113f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugConfig,           // configuration
114f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugInternalState,    // internal state changes
115f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugAll,              // all
116f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugMax = kDebugAll,
117f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
118f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    };
119f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
120f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // parse the property or string to get the debug level for a component name
121f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // string format is:
122f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // <level>[:<glob>][,<level>[:<glob>]...]
123f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // - <level> is 0-5 corresponding to ADebug::Level
124f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // - <glob> is used to match component name case insensitively, if omitted, it
125f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    //   matches all components
126f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // - string is read left-to-right, and the last matching level is returned, or
127f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    //   the def if no terms matched
128f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    static Level GetDebugLevelFromProperty(
129f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            const char *name, const char *propertyName, Level def = kDebugNone);
130f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    static Level GetDebugLevelFromString(
131f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            const char *name, const char *value, Level def = kDebugNone);
132f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
133f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // remove redundant segments of a codec name, and return a newly allocated
134f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // string suitable for debugging
135f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    static char *GetDebugName(const char *name);
136011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar
137011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    inline static bool isExperimentEnabled(
138011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            const char *name __unused /* nonnull */, bool allow __unused = true) {
139011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar#ifdef ENABLE_STAGEFRIGHT_EXPERIMENTS
140011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        if (!strcmp(name, "legacy-adaptive")) {
141011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            return getExperimentFlag(allow, name, 2, 1); // every other day
142011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        } else if (!strcmp(name, "legacy-setsurface")) {
143011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            return getExperimentFlag(allow, name, 3, 1); // every third day
144011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        } else {
145011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            ALOGE("unknown experiment '%s' (disabled)", name);
146011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        }
147011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar#endif
148011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        return false;
149011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    }
150011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar
151011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnarprivate:
152011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    // pass in allow, so we can print in the log if the experiment is disabled
153011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    static bool getExperimentFlag(
154011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            bool allow, const char *name, uint64_t modulo, uint64_t limit,
155011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            uint64_t plus = 0, uint64_t timeDivisor = 24 * 60 * 60 /* 1 day */);
156f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar};
157f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
15872961230a5890071bcca436eb5630172ce84ec41Andreas Huber}  // namespace android
15972961230a5890071bcca436eb5630172ce84ec41Andreas Huber
16072961230a5890071bcca436eb5630172ce84ec41Andreas Huber#endif  // A_DEBUG_H_
16172961230a5890071bcca436eb5630172ce84ec41Andreas Huber
162