我应该如何从日志消息流中有效地提取堆栈跟踪?

时间:2014-04-25 13:13:51

标签: java

在Android上,我需要从日志消息流中提取堆栈跟踪。这并不像我想象的那么简单。以下是我访问日志消息流的方法:

Process proLogcat = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufLogcat = new BufferedReader(new InputStreamReader(proLogcat.getInputStream()));
String strLine;
while ((strLine = bufLogcat.readLine()) != null) {
    //Do something with the line
}

我只是执行logcat命令并读取行,因为我无法直接访问日志文件。我想从日志消息中解析掉所有错误或警告的堆栈跟踪,并相应地对它们进行分组。

我想知道如何有效地做到这一点。一位朋友建议使用ANTLR,但我没有经验。

以下是我正在解析的流的示例:

E/ImageFetcher(14395): Unable download image from the web
E/ImageFetcher(14395): org.apache.http.client.HttpResponseException: Server responded with code 404
E/ImageFetcher(14395):  at com.mridang.widgets.utils.ImageFetcher.getImage(ImageFetcher.java:64)
E/ImageFetcher(14395):  at com.mridang.cominup.LauncherWidget.fetchContent(LauncherWidget.java:103)
E/ImageFetcher(14395):  at com.mridang.widgets.BaseWidget$1.run(BaseWidget.java:384)
E/ImageFetcher(14395):  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
E/ImageFetcher(14395):  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/ImageFetcher(14395):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/ImageFetcher(14395):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/ImageFetcher(14395):  at java.lang.Thread.run(Thread.java:841)
W/SlideFactory(14395): Unable to download cover for Nymphomaniac Vol II
E/ImageFetcher(14395): Unable download image from the web
E/ImageFetcher(14395): org.apache.http.client.HttpResponseException: Server responded with code 404
E/ImageFetcher(14395):  at com.mridang.widgets.utils.ImageFetcher.getImage(ImageFetcher.java:64)
E/ImageFetcher(14395):  at com.mridang.cominup.LauncherWidget.fetchContent(LauncherWidget.java:103)
E/ImageFetcher(14395):  at com.mridang.widgets.BaseWidget$1.run(BaseWidget.java:384)
E/ImageFetcher(14395):  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
E/ImageFetcher(14395):  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/ImageFetcher(14395):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/ImageFetcher(14395):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/ImageFetcher(14395):  at java.lang.Thread.run(Thread.java:841)
W/SlideFactory(14395): Unable to download cover for Labor Day
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/GCM/DMM ( 3138): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.mridang.computer (has extras) }
W/GCM/DMM ( 3138): Receiver package not found, unregister application com.mridang.computer sender 
W/ContextImpl( 3138): Implicit intents with startService are not safe: Intent { act=com.google.android.c2dm.intent.UNREGISTER (has extras) } android.content.ContextWrapper.startService:494 eeu.a:321 eez.onReceive:354 
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
E/Sensors ( 2349): Acc old sensor_state 0, new sensor_state : 1 en : 1
E/Sensors ( 2349): Gyro old sensor_state 1, new sensor_state : 3 en : 1
E/Sensors ( 2349): Pressure old sensor_state 3, new sensor_state : 11 en : 1
W/Sidekick_LocationOracleImpl( 3405): Best location was null
E/Sensors ( 2349): Acc old sensor_state 11, new sensor_state : 10 en : 0
E/Sensors ( 2349): Gyro old sensor_state 10, new sensor_state : 8 en : 0
E/Sensors ( 2349): Pressure old sensor_state 8, new sensor_state : 0 en : 0
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/GCM/DMM ( 3138): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.mridang.computer (has extras) }
W/GCM/DMM ( 3138): Receiver package not found, unregister application com.mridang.computer sender 
W/ContextImpl( 3138): Implicit intents with startService are not safe: Intent { act=com.google.android.c2dm.intent.UNREGISTER (has extras) } android.content.ContextWrapper.startService:494 eeu.a:321 eez.onReceive:354 
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/GCM/DMM ( 3138): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.mridang.computer (has extras) }
W/GCM/DMM ( 3138): Receiver package not found, unregister application com.mridang.computer sender 
W/ContextImpl( 3138): Implicit intents with startService are not safe: Intent { act=com.google.android.c2dm.intent.UNREGISTER (has extras) } android.content.ContextWrapper.startService:494 eeu.a:321 eez.onReceive:354 
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
E/fb4a(:<default>):AddressResolver(14427): Failed to deserialize to instance com.facebook.mqtt.AddressEntry
E/fb4a(:<default>):AddressResolver(14427):  at [Source: java.io.StringReader@42567cb8; line: 1, column: 34]
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/GCM/DMM ( 3138): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.mridang.computer (has extras) }
W/GCM/DMM ( 3138): Receiver package not found, unregister application com.mridang.computer sender 
W/ContextImpl( 3138): Implicit intents with startService are not safe: Intent { act=com.google.android.c2dm.intent.UNREGISTER (has extras) } android.content.ContextWrapper.startService:494 eeu.a:321 eez.onReceive:354 
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/GCM/DMM ( 3138): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.mridang.computer (has extras) }
W/GCM/DMM ( 3138): Receiver package not found, unregister application com.mridang.computer sender 
W/ContextImpl( 3138): Implicit intents with startService are not safe: Intent { act=com.google.android.c2dm.intent.UNREGISTER (has extras) } android.content.ContextWrapper.startService:494 eeu.a:321 eez.onReceive:354 
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/ContentResolver(14595): Failed to get type for: content://com.android.calendar (Unknown URL content://com.android.calendar)
W/n       (14634): [getWeather] Got exception: java.lang.Exception: Couldn't get current location
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/GCM/DMM ( 3138): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.mridang.computer (has extras) }
W/GCM/DMM ( 3138): Receiver package not found, unregister application com.mridang.computer sender 
W/ContextImpl( 3138): Implicit intents with startService are not safe: Intent { act=com.google.android.c2dm.intent.UNREGISTER (has extras) } android.content.ContextWrapper.startService:494 eeu.a:321 eez.onReceive:354 
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 4865): LOG: Warning Unknown dock level ignored.
W/System.err( 5147): java.lang.IllegalArgumentException: Invalid path: /storage/emulated/0
W/System.err( 5147):    at android.os.StatFs.doStat(StatFs.java:46)
W/System.err( 5147):    at android.os.StatFs.<init>(StatFs.java:39)
W/System.err( 5147):    at com.mridang.storage.StorageWidget.getExternalTotalMemory(StorageWidget.java:45)
W/System.err( 5147):    at com.mridang.storage.StorageWidget.onUpdateData(StorageWidget.java:71)
W/System.err( 5147):    at com.google.android.apps.dashclock.api.DashClockExtension$1$1.run(DashClockExtension.java:312)
W/System.err( 5147):    at android.os.Handler.handleCallback(Handler.java:733)
W/System.err( 5147):    at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err( 5147):    at android.os.Looper.loop(Looper.java:136)
W/System.err( 5147):    at android.os.HandlerThread.run(HandlerThread.java:61)
W/System.err( 5147): Caused by: libcore.io.ErrnoException: statvfs failed: EACCES (Permission denied)
W/Settings( 5360): Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
W/System.err( 5147):    at libcore.io.Posix.statvfs(Native Method)
W/System.err( 5147):    at libcore.io.ForwardingOs.statvfs(ForwardingOs.java:132)
W/System.err( 5147):    at android.os.StatFs.doStat(StatFs.java:44)
W/System.err( 5147):    ... 8 more
W/System.err( 5147): java.lang.IllegalArgumentException: Invalid path: /storage/emulated/0
W/System.err( 5147):    at android.os.StatFs.doStat(StatFs.java:46)
W/System.err( 5147):    at android.os.StatFs.<init>(StatFs.java:39)
W/System.err( 5147):    at com.mridang.storage.StorageWidget.getExternalTotalMemory(StorageWidget.java:45)
W/System.err( 5147):    at com.mridang.storage.StorageWidget.onUpdateData(StorageWidget.java:72)
W/System.err( 5147):    at com.google.android.apps.dashclock.api.DashClockExtension$1$1.run(DashClockExtension.java:312)
W/System.err( 5147):    at android.os.Handler.handleCallback(Handler.java:733)
W/System.err( 5147):    at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err( 5147):    at android.os.Looper.loop(Looper.java:136)
W/System.err( 5147):    at android.os.HandlerThread.run(HandlerThread.java:61)
W/System.err( 5147): Caused by: libcore.io.ErrnoException: statvfs failed: EACCES (Permission denied)
W/CursorWindow( 2873): Window is full: requested allocation 228 bytes, free space 171 bytes, window size 2097152 bytes
W/System.err( 5147):    at libcore.io.Posix.statvfs(Native Method)
W/System.err( 5147):    at libcore.io.ForwardingOs.statvfs(ForwardingOs.java:132)
W/System.err( 5147):    at android.os.StatFs.doStat(StatFs.java:44)
W/System.err( 5147):    ... 8 more
W/System.err( 5147): java.lang.IllegalArgumentException: Invalid path: /storage/emulated/0
W/System.err( 5147):    at android.os.StatFs.doStat(StatFs.java:46)
W/System.err( 5147):    at android.os.StatFs.<init>(StatFs.java:39)
W/System.err( 5147):    at com.mridang.storage.StorageWidget.getExternalFreeMemory(StorageWidget.java:146)
W/System.err( 5147):    at com.mridang.storage.StorageWidget.onUpdateData(StorageWidget.java:85)
W/System.err( 5147):    at com.google.android.apps.dashclock.api.DashClockExtension$1$1.run(DashClockExtension.java:312)
W/System.err( 5147):    at android.os.Handler.handleCallback(Handler.java:733)
W/System.err( 5147):    at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err( 5147):    at android.os.Looper.loop(Looper.java:136)
W/System.err( 5147):    at android.os.HandlerThread.run(HandlerThread.java:61)
W/System.err( 5147): Caused by: libcore.io.ErrnoException: statvfs failed: EACCES (Permission denied)
W/System.err( 5147):    at libcore.io.Posix.statvfs(Native Method)
W/System.err( 5147):    at libcore.io.ForwardingOs.statvfs(ForwardingOs.java:132)
W/System.err( 5147):    at android.os.StatFs.doStat(StatFs.java:44)
W/System.err( 5147):    ... 8 more
W/System.err( 5147): java.lang.IllegalArgumentException: Invalid path: /storage/emulated/0
W/System.err( 5147):    at android.os.StatFs.doStat(StatFs.java:46)
W/System.err( 5147):    at android.os.StatFs.<init>(StatFs.java:39)
W/System.err( 5147):    at com.mridang.storage.StorageWidget.getExternalTotalMemory(StorageWidget.java:45)
W/System.err( 5147):    at com.mridang.storage.StorageWidget.onUpdateData(StorageWidget.java:85)
W/System.err( 5147):    at com.google.android.apps.dashclock.api.DashClockExtension$1$1.run(DashClockExtension.java:312)
W/System.err( 5147):    at android.os.Handler.handleCallback(Handler.java:733)
W/System.err( 5147):    at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err( 5147):    at android.os.Looper.loop(Looper.java:136)
W/System.err( 5147):    at android.os.HandlerThread.run(HandlerThread.java:61)
W/System.err( 5147): Caused by: libcore.io.ErrnoException: statvfs failed: EACCES (Permission denied)
W/System.err( 5147):    at libcore.io.Posix.statvfs(Native Method)
W/System.err( 5147):    at libcore.io.ForwardingOs.statvfs(ForwardingOs.java:132)
W/System.err( 5147):    at android.os.StatFs.doStat(StatFs.java:44)
W/System.err( 5147):    ... 8 more
W/HangoutsWidget( 5299): Unable to access destination database
E/DatabaseUtils( 2873): Writing exception to parcel
E/DatabaseUtils( 2873): java.lang.NullPointerException
E/DatabaseUtils( 2873):     at android.database.sqlite.SQLiteQueryBuilder.buildUnionSubQuery(SQLiteQueryBuilder.java:540)
E/DatabaseUtils( 2873):     at com.android.providers.telephony.MmsSmsProvider.getConversations(MmsSmsProvider.java:815)
E/DatabaseUtils( 2873):     at com.android.providers.telephony.MmsSmsProvider.query(MmsSmsProvider.java:315)
E/DatabaseUtils( 2873):     at android.content.ContentProvider.query(ContentProvider.java:857)
E/DatabaseUtils( 2873):     at android.content.ContentProvider$Transport.query(ContentProvider.java:200)
E/DatabaseUtils( 2873):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
E/DatabaseUtils( 2873):     at android.os.Binder.execTransact(Binder.java:404)
E/DatabaseUtils( 2873):     at dalvik.system.NativeStart.run(Native Method)
E/dashclock_SmsExtension( 4918): Error accessing conversations cursor in SMS/MMS provider
E/dashclock_SmsExtension( 4918): java.lang.NullPointerException
E/dashclock_SmsExtension( 4918):    at android.os.Parcel.readException(Parcel.java:1471)
E/dashclock_SmsExtension( 4918):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
E/dashclock_SmsExtension( 4918):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
E/dashclock_SmsExtension( 4918):    at android.content.ContentProviderProxy.query(ContentProviderNative.java:420)
E/dashclock_SmsExtension( 4918):    at android.content.ContentResolver.query(ContentResolver.java:461)
E/dashclock_SmsExtension( 4918):    at android.content.ContentResolver.query(ContentResolver.java:404)
E/dashclock_SmsExtension( 4918):    at com.google.android.apps.dashclock.phone.SmsExtension.tryOpenMmsSmsCursor(SmsExtension.java:242)
E/dashclock_SmsExtension( 4918):    at com.google.android.apps.dashclock.phone.SmsExtension.onUpdateData(SmsExtension.java:104)
E/dashclock_SmsExtension( 4918):    at com.google.android.apps.dashclock.api.DashClockExtension$1$1.run(DashClockExtension.java:312)
E/dashclock_SmsExtension( 4918):    at android.os.Handler.handleCallback(Handler.java:733)
E/dashclock_SmsExtension( 4918):    at android.os.Handler.dispatchMessage(Handler.java:95)
E/dashclock_SmsExtension( 4918):    at android.os.Looper.loop(Looper.java:136)
E/dashclock_SmsExtension( 4918):    at android.os.HandlerThread.run(HandlerThread.java:61)
W/GCoreFlp( 3247): No location to return for getLastLocation()

1 个答案:

答案 0 :(得分:1)

您可以查找:\tat,删除:之前的文字。 \t是一个标签字符。

如果文本有:\t...

,您还可以在前一行和后一行中添加文本

e.g。

String prev = null;
while ((strLine = bufLogcat.readLine()) != null) {
    String curr = strLine.split(":", 2);
    if (curr.startsWith("\tat ")) {
       if (prev != null)
          System.out.println(prev);
       System.out.println(curr);
       prev = null;
    } else if (curr.startsWith("\t... ") && prev == null) {
       System.out.println(curr);
    } else {
       prev = curr;
    }
}