ADebug.h revision 011778fd4cb9606b68bfea1ed483d73c04fc6dcd
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
2772961230a5890071bcca436eb5630172ce84ec41Andreas Hubernamespace android {
2872961230a5890071bcca436eb5630172ce84ec41Andreas Huber
296e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber#define LITERAL_TO_STRING_INTERNAL(x)    #x
306e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber#define LITERAL_TO_STRING(x) LITERAL_TO_STRING_INTERNAL(x)
3172961230a5890071bcca436eb5630172ce84ec41Andreas Huber
3272961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK(condition)                                \
336e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber    LOG_ALWAYS_FATAL_IF(                                \
346e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber            !(condition),                               \
35f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            "%s",                                       \
366e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber            __FILE__ ":" LITERAL_TO_STRING(__LINE__)    \
376e4c5c499999c04c2477b987f9e64f3ff2bf1a06Andreas Huber            " CHECK(" #condition ") failed.")
3872961230a5890071bcca436eb5630172ce84ec41Andreas Huber
3972961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define MAKE_COMPARATOR(suffix,op)                          \
4072961230a5890071bcca436eb5630172ce84ec41Andreas Huber    template<class A, class B>                              \
4172961230a5890071bcca436eb5630172ce84ec41Andreas Huber    AString Compare_##suffix(const A &a, const B &b) {      \
4272961230a5890071bcca436eb5630172ce84ec41Andreas Huber        AString res;                                        \
4372961230a5890071bcca436eb5630172ce84ec41Andreas Huber        if (!(a op b)) {                                    \
4472961230a5890071bcca436eb5630172ce84ec41Andreas Huber            res.append(a);                                  \
4572961230a5890071bcca436eb5630172ce84ec41Andreas Huber            res.append(" vs. ");                            \
4672961230a5890071bcca436eb5630172ce84ec41Andreas Huber            res.append(b);                                  \
4772961230a5890071bcca436eb5630172ce84ec41Andreas Huber        }                                                   \
4872961230a5890071bcca436eb5630172ce84ec41Andreas Huber        return res;                                         \
4972961230a5890071bcca436eb5630172ce84ec41Andreas Huber    }
5072961230a5890071bcca436eb5630172ce84ec41Andreas Huber
5172961230a5890071bcca436eb5630172ce84ec41Andreas HuberMAKE_COMPARATOR(EQ,==)
5272961230a5890071bcca436eb5630172ce84ec41Andreas HuberMAKE_COMPARATOR(NE,!=)
5372961230a5890071bcca436eb5630172ce84ec41Andreas HuberMAKE_COMPARATOR(LE,<=)
5472961230a5890071bcca436eb5630172ce84ec41Andreas HuberMAKE_COMPARATOR(GE,>=)
5572961230a5890071bcca436eb5630172ce84ec41Andreas HuberMAKE_COMPARATOR(LT,<)
5672961230a5890071bcca436eb5630172ce84ec41Andreas HuberMAKE_COMPARATOR(GT,>)
5772961230a5890071bcca436eb5630172ce84ec41Andreas Huber
5872961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_OP(x,y,suffix,op)                                         \
5972961230a5890071bcca436eb5630172ce84ec41Andreas Huber    do {                                                                \
6072961230a5890071bcca436eb5630172ce84ec41Andreas Huber        AString ___res = Compare_##suffix(x, y);                        \
6172961230a5890071bcca436eb5630172ce84ec41Andreas Huber        if (!___res.empty()) {                                          \
62f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            AString ___full =                                           \
63f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                __FILE__ ":" LITERAL_TO_STRING(__LINE__)                \
64f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    " CHECK_" #suffix "( " #x "," #y ") failed: ";      \
65f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            ___full.append(___res);                                     \
66f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                                                                        \
67f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            LOG_ALWAYS_FATAL("%s", ___full.c_str());                    \
6872961230a5890071bcca436eb5630172ce84ec41Andreas Huber        }                                                               \
6972961230a5890071bcca436eb5630172ce84ec41Andreas Huber    } while (false)
7072961230a5890071bcca436eb5630172ce84ec41Andreas Huber
7172961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_EQ(x,y)   CHECK_OP(x,y,EQ,==)
7272961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_NE(x,y)   CHECK_OP(x,y,NE,!=)
7372961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_LE(x,y)   CHECK_OP(x,y,LE,<=)
7472961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_LT(x,y)   CHECK_OP(x,y,LT,<)
7572961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_GE(x,y)   CHECK_OP(x,y,GE,>=)
7672961230a5890071bcca436eb5630172ce84ec41Andreas Huber#define CHECK_GT(x,y)   CHECK_OP(x,y,GT,>)
7772961230a5890071bcca436eb5630172ce84ec41Andreas Huber
78bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber#define TRESPASS() \
79bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber        LOG_ALWAYS_FATAL(                                       \
80bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber            __FILE__ ":" LITERAL_TO_STRING(__LINE__)            \
81bc7f5b2e56107cfeaeeab13cf8979379e3c2f139Andreas Huber                " Should not be here.");
8272961230a5890071bcca436eb5630172ce84ec41Andreas Huber
83f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnarstruct ADebug {
84f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    enum Level {
85f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugNone,             // no debug
86f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugLifeCycle,        // lifecycle events: creation/deletion
87f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugState,            // commands and events
88f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugConfig,           // configuration
89f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugInternalState,    // internal state changes
90f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugAll,              // all
91f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        kDebugMax = kDebugAll,
92f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
93f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    };
94f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
95f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // parse the property or string to get the debug level for a component name
96f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // string format is:
97f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // <level>[:<glob>][,<level>[:<glob>]...]
98f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // - <level> is 0-5 corresponding to ADebug::Level
99f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // - <glob> is used to match component name case insensitively, if omitted, it
100f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    //   matches all components
101f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // - string is read left-to-right, and the last matching level is returned, or
102f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    //   the def if no terms matched
103f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    static Level GetDebugLevelFromProperty(
104f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            const char *name, const char *propertyName, Level def = kDebugNone);
105f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    static Level GetDebugLevelFromString(
106f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            const char *name, const char *value, Level def = kDebugNone);
107f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
108f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // remove redundant segments of a codec name, and return a newly allocated
109f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // string suitable for debugging
110f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    static char *GetDebugName(const char *name);
111011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar
112011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    inline static bool isExperimentEnabled(
113011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            const char *name __unused /* nonnull */, bool allow __unused = true) {
114011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar#ifdef ENABLE_STAGEFRIGHT_EXPERIMENTS
115011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        if (!strcmp(name, "legacy-adaptive")) {
116011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            return getExperimentFlag(allow, name, 2, 1); // every other day
117011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        } else if (!strcmp(name, "legacy-setsurface")) {
118011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            return getExperimentFlag(allow, name, 3, 1); // every third day
119011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        } else {
120011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            ALOGE("unknown experiment '%s' (disabled)", name);
121011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        }
122011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar#endif
123011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        return false;
124011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    }
125011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar
126011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnarprivate:
127011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    // pass in allow, so we can print in the log if the experiment is disabled
128011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    static bool getExperimentFlag(
129011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            bool allow, const char *name, uint64_t modulo, uint64_t limit,
130011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            uint64_t plus = 0, uint64_t timeDivisor = 24 * 60 * 60 /* 1 day */);
131f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar};
132f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
13372961230a5890071bcca436eb5630172ce84ec41Andreas Huber}  // namespace android
13472961230a5890071bcca436eb5630172ce84ec41Andreas Huber
13572961230a5890071bcca436eb5630172ce84ec41Andreas Huber#endif  // A_DEBUG_H_
13672961230a5890071bcca436eb5630172ce84ec41Andreas Huber
137