如何给Android的CPP代码加ALOG

我们在调试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(...)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章