com.android.ddmlib.Log Java Examples

The following examples show how to use com.android.ddmlib.Log. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AndroidLogcatFormatter.java    From logviewer with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the result of {@link #parseMessage(String)} or {@code null} if the format of the input
 * text doesn't match.
 */
@Nullable
public static LogCatMessage tryParseMessage(@NotNull String msg) {
    final Matcher matcher = MESSAGE_WITH_HEADER.matcher(msg);
    if (!matcher.matches()) {
        return null;
    }

    @SuppressWarnings("ConstantConditions") // matcher.matches verifies all groups below are non-null
            LogCatHeader header = new LogCatHeader(
            Log.LogLevel.getByLetter(matcher.group(5).charAt(0)),
            Integer.parseInt(matcher.group(2)),
            Integer.parseInt(matcher.group(3)),
            matcher.group(4),
            matcher.group(6),
            LogCatTimestamp.fromString(matcher.group(1)));

    String message = matcher.group(7);

    return new LogCatMessage(header, message);
}
 
Example #2
Source File: InstrumentationResultParser.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parses out a status code result.
 */
private void parseStatusCode(String line) {
    String value = line.substring(Prefixes.STATUS_CODE.length()).trim();
    TestResult testInfo = getCurrentTestInfo();
    testInfo.mCode = StatusCodes.ERROR;
    try {
        testInfo.mCode = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        Log.w(LOG_TAG, "Expected integer status code, received: " + value);
        testInfo.mCode = StatusCodes.ERROR;
    }
    if (testInfo.mCode != StatusCodes.IN_PROGRESS) {
        // this means we're done with current test result bundle
        reportResult(testInfo);
        clearCurrentTestInfo();
    }
}
 
Example #3
Source File: InstrumentationResultParser.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parses out and store the elapsed time.
 */
private void parseTime(String line) {
    final Pattern timePattern = Pattern.compile(String.format("%s\\s*([\\d\\.]+)",
            Prefixes.TIME_REPORT));
    Matcher timeMatcher = timePattern.matcher(line);
    if (timeMatcher.find()) {
        String timeString = timeMatcher.group(1);
        try {
            float timeSeconds = Float.parseFloat(timeString);
            mTestTime = (long) (timeSeconds * 1000);
        } catch (NumberFormatException e) {
            Log.w(LOG_TAG, String.format("Unexpected time format %1$s", line));
        }
    } else {
        Log.w(LOG_TAG, String.format("Unexpected time format %1$s", line));
    }
}
 
Example #4
Source File: DdmLibLogRedirector.java    From buck with Apache License 2.0 6 votes vote down vote up
private static Level convertDdmLevel(Log.LogLevel ddmLevel) {
  switch (ddmLevel) {
    case VERBOSE:
      return Level.FINER;
    case DEBUG:
      return Level.FINE;
    case INFO:
      return Level.INFO;
    case WARN:
      return Level.WARNING;
    case ERROR:
    case ASSERT:
    default:
      return Level.SEVERE;
  }
}
 
Example #5
Source File: XmlTestRunListener.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a report file and populates it with the report data from the completed tests.
 */
private void generateDocument(File reportDir, long elapsedTime) {
    String timestamp = getTimestamp();

    OutputStream stream = null;
    try {
        stream = createOutputResultStream(reportDir);
        KXmlSerializer serializer = new KXmlSerializer();
        serializer.setOutput(stream, SdkConstants.UTF_8);
        serializer.startDocument(SdkConstants.UTF_8, null);
        serializer.setFeature(
                "http://xmlpull.org/v1/doc/features.html#indent-output", true);
        // TODO: insert build info
        printTestResults(serializer, timestamp, elapsedTime);
        serializer.endDocument();
        String msg = String.format("XML test result file generated at %s. %s" ,
                getAbsoluteReportPath(), mRunResult.getTextSummary());
        Log.logAndDisplay(LogLevel.INFO, LOG_TAG, msg);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Failed to generate report data");
        // TODO: consider throwing exception
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException ignored) {
            }
        }
    }
}
 
Example #6
Source File: XmlTestRunListener.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a {@link File} where the report will be created.
 * @param reportDir the root directory of the report.
 * @return a file
 * @throws IOException
 */
protected File getResultFile(File reportDir) throws IOException {
    File reportFile = File.createTempFile(TEST_RESULT_FILE_PREFIX, TEST_RESULT_FILE_SUFFIX,
            reportDir);
    Log.i(LOG_TAG, String.format("Created xml report file at %s",
            reportFile.getAbsolutePath()));

    return reportFile;
}
 
Example #7
Source File: TestRunResult.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private void updateTestResult(TestIdentifier test, TestStatus status, String trace) {
    TestResult r = mTestResults.get(test);
    if (r == null) {
        Log.d(LOG_TAG, String.format("received test event without test start for %s", test));
        r = new TestResult();
    }
    r.setStatus(status);
    r.setStackTrace(trace);
    addTestResult(test, r);
}
 
Example #8
Source File: InstrumentationResultParser.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Processes the instrumentation test output from shell.
 *
 * @see MultiLineReceiver#processNewLines
 */
@Override
public void processNewLines(String[] lines) {
    for (String line : lines) {
        parse(line);
        // in verbose mode, dump all adb output to log
        Log.v(LOG_TAG, line);
    }
}
 
Example #9
Source File: InstrumentationResultParser.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parse an individual output line. Expects a line that is one of:
 * <ul>
 * <li>
 * The start of a new status line (starts with Prefixes.STATUS or Prefixes.STATUS_CODE),
 * and thus there is a new key=value pair to parse, and the previous key-value pair is
 * finished.
 * </li>
 * <li>
 * A continuation of the previous status (the "value" portion of the key has wrapped
 * to the next line).
 * </li>
 * <li> A line reporting a fatal error in the test run (Prefixes.STATUS_FAILED) </li>
 * <li> A line reporting the total elapsed time of the test run. (Prefixes.TIME_REPORT) </li>
 * </ul>
 *
 * @param line  Text output line
 */
private void parse(String line) {
    if (line.startsWith(Prefixes.STATUS_CODE)) {
        // Previous status key-value has been collected. Store it.
        submitCurrentKeyValue();
        mInInstrumentationResultKey = false;
        parseStatusCode(line);
    } else if (line.startsWith(Prefixes.STATUS)) {
        // Previous status key-value has been collected. Store it.
        submitCurrentKeyValue();
        mInInstrumentationResultKey = false;
        parseKey(line, Prefixes.STATUS.length());
    } else if (line.startsWith(Prefixes.RESULT)) {
        // Previous status key-value has been collected. Store it.
        submitCurrentKeyValue();
        mInInstrumentationResultKey = true;
        parseKey(line, Prefixes.RESULT.length());
    } else if (line.startsWith(Prefixes.STATUS_FAILED) ||
               line.startsWith(Prefixes.CODE)) {
        // Previous status key-value has been collected. Store it.
        submitCurrentKeyValue();
        mInInstrumentationResultKey = false;
        // these codes signal the end of the instrumentation run
        mTestRunFinished = true;
        // just ignore the remaining data on this line
    } else if (line.startsWith(Prefixes.TIME_REPORT)) {
        parseTime(line);
    } else {
        if (mCurrentValue != null) {
            // this is a value that has wrapped to next line.
            mCurrentValue.append("\r\n");
            mCurrentValue.append(line);
        } else if (!line.trim().isEmpty()) {
            Log.d(LOG_TAG, "unrecognized line " + line);
        }
    }
}
 
Example #10
Source File: InstrumentationResultParser.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the stack trace of the current failed test, from the provided testInfo.
 */
private String getTrace(TestResult testInfo) {
    if (testInfo.mStackTrace != null) {
        return testInfo.mStackTrace;
    } else {
        Log.e(LOG_TAG, "Could not find stack trace for failed test ");
        return new Throwable("Unknown failure").toString();
    }
}
 
Example #11
Source File: EventLogParser.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private EventValueDescription[] processDescription(String description) {
    String[] descriptions = description.split("\\s*,\\s*"); //$NON-NLS-1$

    ArrayList<EventValueDescription> list = new ArrayList<EventValueDescription>();

    for (String desc : descriptions) {
        Matcher m = PATTERN_DESCRIPTION.matcher(desc);
        if (m.matches()) {
            try {
                String name = m.group(1);

                String typeString = m.group(2);
                int typeValue = Integer.parseInt(typeString);
                EventValueType eventValueType = EventValueType.getEventValueType(typeValue);
                if (eventValueType == null) {
                    // just ignore this description if the value is not recognized.
                    // TODO: log the error.
                }

                typeString = m.group(3);
                if (typeString != null && !typeString.isEmpty()) {
                    //skip the |
                    typeString = typeString.substring(1);

                    typeValue = Integer.parseInt(typeString);
                    ValueType valueType = ValueType.getValueType(typeValue);

                    list.add(new EventValueDescription(name, eventValueType, valueType));
                } else {
                    list.add(new EventValueDescription(name, eventValueType));
                }
            } catch (NumberFormatException nfe) {
                // just ignore this description if one number is malformed.
                // TODO: log the error.
            } catch (InvalidValueTypeException e) {
                // just ignore this description if data type and data unit don't match
                // TODO: log the error.
            }
        } else {
            Log.e("EventLogParser",  //$NON-NLS-1$
                String.format("Can't parse %1$s", description));  //$NON-NLS-1$
        }
    }

    if (list.isEmpty()) {
        return null;
    }

    return list.toArray(new EventValueDescription[list.size()]);

}
 
Example #12
Source File: EventLogParser.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Recursively convert binary log data to printable form.
 *
 * This needs to be recursive because you can have lists of lists.
 *
 * If we run out of room, we stop processing immediately.  It's important
 * for us to check for space on every output element to avoid producing
 * garbled output.
 *
 * Returns the amount read on success, -1 on failure.
 */
private static int parseBinaryEvent(byte[] eventData, int dataOffset, ArrayList<Object> list) {

    if (eventData.length - dataOffset < 1)
        return -1;

    int offset = dataOffset;

    int type = eventData[offset++];

    //fprintf(stderr, "--- type=%d (rem len=%d)\n", type, eventDataLen);

    switch (type) {
    case EVENT_TYPE_INT: { /* 32-bit signed int */
            int ival;

            if (eventData.length - offset < 4)
                return -1;
            ival = ArrayHelper.swap32bitFromArray(eventData, offset);
            offset += 4;

            list.add(ival);
        }
        break;
    case EVENT_TYPE_LONG: { /* 64-bit signed long */
            long lval;

            if (eventData.length - offset < 8)
                return -1;
            lval = ArrayHelper.swap64bitFromArray(eventData, offset);
            offset += 8;

            list.add(lval);
        }
        break;
    case EVENT_TYPE_STRING: { /* UTF-8 chars, not NULL-terminated */
            int strLen;

            if (eventData.length - offset < 4)
                return -1;
            strLen = ArrayHelper.swap32bitFromArray(eventData, offset);
            offset += 4;

            if (eventData.length - offset < strLen)
                return -1;

            // get the string
            String str = new String(eventData, offset, strLen, Charsets.UTF_8);
            list.add(str);
            offset += strLen;
            break;
        }
    case EVENT_TYPE_LIST: { /* N items, all different types */

            if (eventData.length - offset < 1)
                return -1;

            int count = eventData[offset++];

            // make a new temp list
            ArrayList<Object> subList = new ArrayList<Object>();
            for (int i = 0; i < count; i++) {
                int result = parseBinaryEvent(eventData, offset, subList);
                if (result == -1) {
                    return result;
                }

                offset += result;
            }

            list.add(subList.toArray());
        }
        break;
    default:
        Log.e("EventLogParser",  //$NON-NLS-1$
                String.format("Unknown binary event type %1$d", type));  //$NON-NLS-1$
        return -1;
    }

    return offset - dataOffset;
}
 
Example #13
Source File: InstrumentationResultParser.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Stores the currently parsed key-value pair in the appropriate place.
 */
private void submitCurrentKeyValue() {
    if (mCurrentKey != null && mCurrentValue != null) {
        String statusValue = mCurrentValue.toString();
        if (mInInstrumentationResultKey) {
            if (!KNOWN_KEYS.contains(mCurrentKey)) {
                mInstrumentationResultBundle.put(mCurrentKey, statusValue);
            } else if (mCurrentKey.equals(StatusKeys.SHORTMSG)) {
                // test run must have failed
                handleTestRunFailed(String.format("Instrumentation run failed due to '%1$s'",
                        statusValue));
            }
        } else {
            TestResult testInfo = getCurrentTestInfo();

            if (mCurrentKey.equals(StatusKeys.CLASS)) {
                testInfo.mTestClass = statusValue.trim();
            } else if (mCurrentKey.equals(StatusKeys.TEST)) {
                testInfo.mTestName = statusValue.trim();
            } else if (mCurrentKey.equals(StatusKeys.NUMTESTS)) {
                try {
                    testInfo.mNumTests = Integer.parseInt(statusValue);
                } catch (NumberFormatException e) {
                    Log.w(LOG_TAG, "Unexpected integer number of tests, received "
                            + statusValue);
                }
            } else if (mCurrentKey.equals(StatusKeys.ERROR)) {
                // test run must have failed
                handleTestRunFailed(statusValue);
            } else if (mCurrentKey.equals(StatusKeys.STACK)) {
                testInfo.mStackTrace = statusValue;
            } else if (!KNOWN_KEYS.contains(mCurrentKey)) {
                // Not one of the recognized key/value pairs, so dump it in mTestMetrics
                mTestMetrics.put(mCurrentKey, statusValue);
            }
        }

        mCurrentKey = null;
        mCurrentValue = null;
    }
}
 
Example #14
Source File: QuickTesterLoger.java    From AndroidRobot with Apache License 2.0 4 votes vote down vote up
public static void info(String message) {
	Log.i(LOG_TAG, message);
}