ADebug.h revision 72961230a5890071bcca436eb5630172ce84ec41
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 26namespace android { 27 28enum LogType { 29 VERBOSE, 30 INFO, 31 WARNING, 32 ERROR, 33 FATAL, 34}; 35 36struct Logger { 37 Logger(LogType type); 38 virtual ~Logger(); 39 40 template<class T> Logger &operator<<(const T &x) { 41 mMessage.append(x); 42 43 return *this; 44 } 45 46private: 47 android::AString mMessage; 48 LogType mLogType; 49 50 DISALLOW_EVIL_CONSTRUCTORS(Logger); 51}; 52 53const char *LeafName(const char *s); 54 55#undef LOG 56#define LOG(type) Logger(type) << LeafName(__FILE__) << ":" << __LINE__ << " " 57 58#define CHECK(condition) \ 59 do { \ 60 if (!(condition)) { \ 61 LOG(FATAL) << "CHECK(" #condition ") failed."; \ 62 } \ 63 } while (false) 64 65#define MAKE_COMPARATOR(suffix,op) \ 66 template<class A, class B> \ 67 AString Compare_##suffix(const A &a, const B &b) { \ 68 AString res; \ 69 if (!(a op b)) { \ 70 res.append(a); \ 71 res.append(" vs. "); \ 72 res.append(b); \ 73 } \ 74 return res; \ 75 } 76 77MAKE_COMPARATOR(EQ,==) 78MAKE_COMPARATOR(NE,!=) 79MAKE_COMPARATOR(LE,<=) 80MAKE_COMPARATOR(GE,>=) 81MAKE_COMPARATOR(LT,<) 82MAKE_COMPARATOR(GT,>) 83 84#define CHECK_OP(x,y,suffix,op) \ 85 do { \ 86 AString ___res = Compare_##suffix(x, y); \ 87 if (!___res.empty()) { \ 88 LOG(FATAL) << "CHECK_" #suffix "(" #x "," #y ") failed: " \ 89 << ___res; \ 90 } \ 91 } while (false) 92 93#define CHECK_EQ(x,y) CHECK_OP(x,y,EQ,==) 94#define CHECK_NE(x,y) CHECK_OP(x,y,NE,!=) 95#define CHECK_LE(x,y) CHECK_OP(x,y,LE,<=) 96#define CHECK_LT(x,y) CHECK_OP(x,y,LT,<) 97#define CHECK_GE(x,y) CHECK_OP(x,y,GE,>=) 98#define CHECK_GT(x,y) CHECK_OP(x,y,GT,>) 99 100#define TRESPASS() LOG(FATAL) << "Should not be here." 101 102} // namespace android 103 104#endif // A_DEBUG_H_ 105 106