diff options
author | Siva Velusamy <vsiva@google.com> | 2011-08-11 15:22:29 -0700 |
---|---|---|
committer | Siva Velusamy <vsiva@google.com> | 2011-08-11 15:35:21 -0700 |
commit | c9bd041b91bc4aa621ec82e2babe6a2b7e942bb6 (patch) | |
tree | 33debec23ef71d4b0b9ea56e78545df45acbb17a /ddms | |
parent | 7b7c2aca20906ca5019db6132cbcb22b9067ec76 (diff) | |
download | sdk-c9bd041b91bc4aa621ec82e2babe6a2b7e942bb6.zip sdk-c9bd041b91bc4aa621ec82e2babe6a2b7e942bb6.tar.gz sdk-c9bd041b91bc4aa621ec82e2babe6a2b7e942bb6.tar.bz2 |
Add tests for LogCatMessageParser.
Change-Id: I2fc1f14cbf72117112a217b2f9effb00b3e040a0
Diffstat (limited to 'ddms')
-rw-r--r-- | ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatMessageParser.java | 13 | ||||
-rw-r--r-- | ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatMessageParserTest.java | 94 |
2 files changed, 105 insertions, 2 deletions
diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatMessageParser.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatMessageParser.java index cc90db9..be2be58 100644 --- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatMessageParser.java +++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogCatMessageParser.java @@ -39,13 +39,16 @@ public final class LogCatMessageParser { * This first line looks something like:<br> * {@code "[ 00-00 00:00:00.000 <pid>:0x<???> <severity>/<tag>]"} * <br> - * Note: severity is one of V, D, I, W, E or A<br> + * Note: severity is one of V, D, I, W, E, A? or F. However, there doesn't seem to be + * a way to actually generate an A (assert) message. Log.wtf is supposed to generate + * a message with severity A, however it generates the undocumented F level. In + * such a case, the parser will change the level from F to A.<br> * Note: the fraction of second value can have any number of digit.<br> * Note: the tag should be trimmed as it may have spaces at the end. */ private static Pattern sLogHeaderPattern = Pattern.compile( "^\\[\\s(\\d\\d-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d\\.\\d+)" - + "\\s+(\\d*):(0x[0-9a-fA-F]+)\\s([VDIWEA])/(.*)\\]$"); + + "\\s+(\\d*):(0x[0-9a-fA-F]+)\\s([VDIWEAF])/(.*)\\]$"); /** * Parse a list of strings into {@link LogCatMessage} objects. This method @@ -68,6 +71,12 @@ public final class LogCatMessageParser { mCurPid = matcher.group(2); mCurLogLevel = LogLevel.getByLetterString(matcher.group(4)); mCurTag = matcher.group(5).trim(); + + /* LogLevel doesn't support messages with severity "F". Log.wtf() is supposed + * to generate "A", but generates "F". */ + if (mCurLogLevel == null && matcher.group(4).equals("F")) { + mCurLogLevel = LogLevel.ASSERT; + } } else { LogCatMessage m = new LogCatMessage(mCurLogLevel, mCurPid, mCurTag, mCurTime, line); messages.add(m); diff --git a/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatMessageParserTest.java b/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatMessageParserTest.java new file mode 100644 index 0000000..645e8de --- /dev/null +++ b/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/logcat/LogCatMessageParserTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.ddmuilib.logcat; + +import com.android.ddmlib.Log.LogLevel; + +import java.util.List; + +import junit.framework.TestCase; + +/** + * Unit tests for {@link LogCatMessageParser}. + */ +public final class LogCatMessageParserTest extends TestCase { + private List<LogCatMessage> mParsedMessages; + + /** A list of messages generated with the following code: + * <pre> + * {@code + * Log.d("dtag", "debug message"); + * Log.e("etag", "error message"); + * Log.i("itag", "info message"); + * Log.v("vtag", "verbose message"); + * Log.w("wtag", "warning message"); + * Log.wtf("wtftag", "wtf message"); + * Log.d("dtag", "debug message"); + * } + * </pre> + * Note: On Android 2.3, Log.wtf doesn't really generate the message. + * It only produces the message header, but swallows the message tag. + * This string has been modified to include the message. + */ + private static final String[] MESSAGES = new String[] { + "[ 08-11 19:11:07.132 495:0x1ef D/dtag ]", //$NON-NLS-1$ + "debug message", //$NON-NLS-1$ + "[ 08-11 19:11:07.132 495:0x1ef E/etag ]", //$NON-NLS-1$ + "error message", //$NON-NLS-1$ + "[ 08-11 19:11:07.132 495:0x1ef I/itag ]", //$NON-NLS-1$ + "info message", //$NON-NLS-1$ + "[ 08-11 19:11:07.132 495:0x1ef V/vtag ]", //$NON-NLS-1$ + "verbose message", //$NON-NLS-1$ + "[ 08-11 19:11:07.132 495:0x1ef W/wtag ]", //$NON-NLS-1$ + "warning message", //$NON-NLS-1$ + "[ 08-11 19:11:07.132 495:0x1ef F/wtftag ]", //$NON-NLS-1$ + "wtf message", //$NON-NLS-1$ + "[ 08-11 21:15:35.7524 540:0x21c D/dtag ]", //$NON-NLS-1$ + "debug message", //$NON-NLS-1$ + }; + + @Override + protected void setUp() throws Exception { + LogCatMessageParser parser = new LogCatMessageParser(); + mParsedMessages = parser.processLogLines(MESSAGES); + } + + /** Check that the correct number of messages are received. */ + public void testMessageCount() { + assertEquals(7, mParsedMessages.size()); + } + + /** Check the log level in a few of the parsed messages. */ + public void testLogLevel() { + assertEquals(mParsedMessages.get(0).getLogLevel(), LogLevel.DEBUG); + assertEquals(mParsedMessages.get(5).getLogLevel(), LogLevel.ASSERT); + } + + /** Check the parsed tag. */ + public void testTag() { + assertEquals(mParsedMessages.get(1).getTag(), "etag"); //$NON-NLS-1$ + } + + /** Check the time field. */ + public void testTime() { + assertEquals(mParsedMessages.get(6).getTime(), "08-11 21:15:35.7524"); //$NON-NLS-1$ + } + + /** Check the message field. */ + public void testMessage() { + assertEquals(mParsedMessages.get(2).getMessage(), MESSAGES[5]); + } +} |