方法一.
Exception e = new Exception("this is a log"); //相应的log标记
e.printStackTrace();
//延迟才可以看出效果
//Thread.currentThread().sleep(1000);
//System.out.println("-------------以上是异常详细信息----------------");
方法二 .String fullStackTrace = org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e)
方法三 .Thread.currentThread().getStackTrace()
方法四 .Log.e(“dump_test”,Log.getStackTraceString(new Throwable()));
Native C++ 打印堆栈的方法
1 前面确保包含头文件#include <utils/CallStack.h> //
2.在对应模块Android.mk或者Android.bp的库依赖列表(LOCAL_SHARED_LIBRARIES)里添加libutils eg:
++ b/alps/frameworks/av/media/libaudioclient/Android.bp
@@ -2,6 +2,16 @@ cc_library_headers {
name: "libaudioclient_headers",
vendor_available: true,
export_include_dirs: ["include"],
+ header_libs: [
+ "libbase_headers",
+ "libcutils_headers",
+ "libutils_headers",
+ ],
+ export_header_lib_headers: [
+ "libbase_headers",
+ "libcutils_headers",
+ "libutils_headers",
+ ],
}
cc_library_shared {
@@ -50,6 +60,7 @@ cc_library_shared {
"libmedia_helper",
"libmediametrics",
"libmtkaudio_utils",
+ "libutilscallstack",
],
3 然后在要打印堆栈处加入
CallStack stack;
+ stack.update();
+ stack.log("haha");
“haha”是在logcat输出的TAG,这里可以自己定义。
Native C++的输出log可以在logcat里看到。
eg:
+ b/alps/frameworks/av/media/libaudioclient/AudioTrack.cpp
@@ -25,6 +25,7 @@
#include <sys/prctl.h>
#endif
+#include <utils/CallStack.h>
#include <audio_utils/clock.h>
#include <audio_utils/primitives.h>
#include <binder/IPCThreadState.h>
@@ -405,7 +406,9 @@ status_t AudioTrack::set(
uint32_t channelCount;
pid_t callingPid;
pid_t myPid;
-
+ CallStack stack;
+ stack.update();
+ stack.log("haha");