我們在調試Android系統代碼的時候,出於學習/debug的目的,經常需要加ALOG來輸出LOG。因爲print函數是輸出到terminal終端的,系統在運行的時候並沒有這樣的終端,所以需要用Android的LOG系統,也就是ALOG來輸出我們想獲得的debug信息。有些模塊是從Linux移植過來的,往往打印LOG的時候,原本的實現要依賴於vsnprint或者vscprint這樣的函數,而這類函數是在Android中不工作的,此時我們就需要將原來的LOG實現進行改寫
分兩種情況:
情況1: 在Android的Source Code裏編譯,也就是In Code Tree編譯
首先需要在.mk文件中include進 system/core/include,然後引用#include <cutils/log.h>之後,就可以直接使用ALOG函數了。這裏的system/code/include就是指Android Source Tree中的相對路徑,裏面包含了lib log等庫,其中就有ALOG的定義以及實現
diff --git a/Android.mk b/Android.mk index 4ef0868..49ce951 100644 --- a/Android.mk +++ b/Android.mk LOCAL_CPPFLAGS = \ -fexceptions \ @@ -104,7 +105,8 @@ LOCAL_C_INCLUDES = \ $(LOCAL_PATH)/Source/inc \ $(LOCAL_PATH)/Source/inc/common \ $(LOCAL_PATH)/Source/inc/umKmInc \ - $(LOCAL_PATH)/Source/install + $(LOCAL_PATH)/Source/install \ + system/core/include LOCAL_EXPORT_C_INCLUDE_DIRS = \ $(LOCAL_PATH)/Source/GmmLib/inc \ diff --git a/Source/GmmLib/GlobalInfo/GmmInfo.cpp b/Source/GmmLib/GlobalInfo/GmmInfo.cpp index e203855..6cb9dcf 100644 --- a/Source/GmmLib/GlobalInfo/GmmInfo.cpp +++ b/Source/GmmLib/GlobalInfo/GmmInfo.cpp @@ -21,6 +21,7 @@ OTHER DEALINGS IN THE SOFTWARE. ============================================================================*/ #include "Internal/Common/GmmLibInc.h" +#include <cutils/log.h> //=========================================================================== // Global Variable: @@ -438,6 +439,8 @@ GMM_CLIENT ClientType) this->GtSysInfo = *pGtSysInfo; OverrideSkuWa(); + ALOGE("test ALOGE debug in %s", __func__); + GMM_DPF(GFXDBG_NORMAL, "test wrap ALOGE%s-->\n", __FUNCTION__); pGmmGlobalContext->pPlatformInfo = GmmLib::PlatformInfo::Create(Platform, false); diff --git a/Source/GmmLib/inc/External/Common/GmmDebug.h b/Source/GmmLib/inc/External/Common/GmmDebug.h index a299f50..ed40035 100644 --- a/Source/GmmLib/inc/External/Common/GmmDebug.h +++ b/Source/GmmLib/inc/External/Common/GmmDebug.h @@ -31,6 +31,7 @@ OTHER DEALINGS IN THE SOFTWARE. #else #include "GmmCommonExt.h" #include "GmmLog/GmmLog.h" +#include <cutils/log.h> //===================== Debug Message Levels======================== #define GFXDBG_OFF (0x00000000) #define GFXDBG_CRITICAL (0x00000001) @@ -85,6 +86,13 @@ OTHER DEALINGS IN THE SOFTWARE. #define GMM_DBG_BREAK assert(0) #endif +#if defined(__ANDROID__) +#define GMMLibDebugMessage(DebugLevel, message, ...) \ +{ \ + ALOGD(message, ##__VA_ARGS__); \ +} + +#else #define GMMLibDebugMessage(DebugLevel, message, ...) \ { \ if(DebugLevel == GFXDBG_CRITICAL) \ @@ -104,6 +112,7 @@ OTHER DEALINGS IN THE SOFTWARE. GMM_LOG_INFO(message, ##__VA_ARGS__); \ } \ } +#endif #define GMM_LIB_ASSERT(expr) \ { \
情況2: 利用NDK進行out of tree編譯,這種情況往往見於Binary/App從Linux porting 到Android(移植)。
由於沒有Android Source Code,自然沒有辦法include進 system/core/include和引用#include <cutils/log.h>,所以沒有辦法直接使用ALOG函數。這種情況只能用<android/libutils>代替,也就是在CMakeList.txt的target_link_libraries中,加上-landroid,這裏的意思是鏈接的時候把libandroid這個庫給鏈接上。也沒有辦法直接用ALOG,可以用__android_log_print代替.
需要值得注意的是有時候我們在CMakeList.txt里加了-landroid,卻還是找不到__android__log_print,會報error:undefined reference to '__android_log_print'的錯誤,這是因爲我們雖然在連接的時候寫了-landroid,但是編譯的時候卻沒有找到這個package,自然也就無法鏈接上。一般我們需要在target_link_libraries之前加一句find_package(Threads REQUIRED), 並把-landroid寫在target_link_libraries裏的Threads::Threads之後就可以了。如下代碼所示:
--- a/Source/GmmLib/CMakeLists.txt +++ b/Source/GmmLib/CMakeLists.txt @@ -591,7 +591,7 @@ else() set_target_properties(${GMM_LIB_DLL_NAME} PROPERTIES SOVERSION ${GMMLIB_API_MAJOR_VERSION}) set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) - target_link_libraries(${GMM_LIB_DLL_NAME} Threads::Threads -static-libstdc++ -llog) + target_link_libraries(${GMM_LIB_DLL_NAME} Threads::Threads -static-libstdc++ -llog -landroid) endif() diff --git a/Source/GmmLib/inc/External/Common/GmmDebug.h b/Source/GmmLib/inc/External/Common/GmmDebug.h index 25503254995..8a38d501407 100644 --- a/Source/GmmLib/inc/External/Common/GmmDebug.h +++ b/Source/GmmLib/inc/External/Common/GmmDebug.h @@ -27,6 +27,9 @@ implied warranties, other than those that are expressly stated in the License. #else #include "GmmCommonExt.h" #include "GmmLog/GmmLog.h" +#if defined(__ANDROID__) +#include <android/log.h> +#endif //===================== Debug Message Levels======================== #define GFXDBG_OFF (0x00000000) #define GFXDBG_CRITICAL (0x00000001) @@ -119,6 +122,14 @@ implied warranties, other than those that are expressly stated in the License. #endif // (_DEBUG) //_DEBUG || _RELEASE_INTERNAL #if (_DEBUG || _RELEASE_INTERNAL) +#if defined(__ANDROID__) + #define LOG_TAG "GMMLib" + #define GMMLibDebugMessage(DebugLevel, message, ...) \ + { \ + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, message, ##__VA_ARGS__); \ + } + +#else #define GMMLibDebugMessage(DebugLevel, message, ...) \ { \ if(DebugLevel == GFXDBG_CRITICAL) \ @@ -138,6 +149,7 @@ implied warranties, other than those that are expressly stated in the License. GMM_LOG_INFO(message, ##__VA_ARGS__); \ } \ } +#endif //__ANDROID__ #else #define GMMLibDebugMessage(...)