Android Open Source - SmartTools Log Cat Message Parser






From Project

Back to project page SmartTools.

License

The source code is released under:

GNU General Public License

If you think the Android project SmartTools listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2011 The Android Open Source Project
 */*from w w w .  j a  va 2  s .c  om*/
 * 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.nj.simba.page.logcat;

import com.android.ddmlib.Log.LogLevel;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class to parse raw output of {@code adb logcat -v long} to {@link LogCatMessage} objects.
 */
public final class LogCatMessageParser {
    private LogLevel mCurLogLevel = LogLevel.WARN;
    private String mCurPid = "?";
    private String mCurTid = "?";
    private String mCurTag = "?";
    private String mCurTime = "?:??";

    /**
     * This pattern is meant to parse the first line of a log message with the option
     * 'logcat -v long'. The first line represents the date, tag, severity, etc.. while the
     * following lines are the message (can be several lines).<br>
     * 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, 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*):\\s*(\\S+)\\s([VDIWEAF])/(.*)\\]$");

    /**
     * Parse a list of strings into {@link LogCatMessage} objects. This method
     * maintains state from previous calls regarding the last seen header of
     * logcat messages.
     * @param lines list of raw strings obtained from logcat -v long
     * @param pidToNameMapper mapper to obtain the app name given a pid
     * @return list of LogMessage objects parsed from the input
     */
    public List<LogCatMessage> processLogLines(String[] lines,
            LogCatPidToNameMapper pidToNameMapper) {
        List<LogCatMessage> messages = new ArrayList<LogCatMessage>(lines.length);

        for (String line : lines) {
            if (line.length() == 0) {
                continue;
            }

            Matcher matcher = sLogHeaderPattern.matcher(line);
            if (matcher.matches()) {
                mCurTime = matcher.group(1);
                mCurPid = matcher.group(2);
                mCurTid = matcher.group(3);
                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, mCurTid,
                        pidToNameMapper.getName(mCurPid),
                        mCurTag, mCurTime, line);
                messages.add(m);
            }
        }

        return messages;
    }
}




Java Source Code List

com.nj.simba.BatteryReceiver.java
com.nj.simba.DaemonController.java
com.nj.simba.DeviceInfo.java
com.nj.simba.DviceInfoCollector.java
com.nj.simba.IDeviceInfo.java
com.nj.simba.PackageReceiver.java
com.nj.simba.SdcardStatuChangeReceiver.java
com.nj.simba.SmartToolService.java
com.nj.simba.app.MainFrame.java
com.nj.simba.app.SmartToolsApp.java
com.nj.simba.app.ToolbarPanel.java
com.nj.simba.base.IDeviceListener.java
com.nj.simba.base.IDeviceReqListener.java
com.nj.simba.connect.DeviceCmdWorker.java
com.nj.simba.connect.DeviceConnectWorker.java
com.nj.simba.connect.DeviceMessage.java
com.nj.simba.connect.DeviceMsgChannel.java
com.nj.simba.ctrls.FilerListRender.java
com.nj.simba.ctrls.ImageBtn.java
com.nj.simba.ctrls.LeftPanel.java
com.nj.simba.ctrls.MetroCoolBtn.java
com.nj.simba.ctrls.MyProgessBar.java
com.nj.simba.ctrls.SubPanel.java
com.nj.simba.ctrls.TabPanel.java
com.nj.simba.ctrls.ThemeButton.java
com.nj.simba.ctrls.ThemeLabel.java
com.nj.simba.ctrls.ToolbarBtn.java
com.nj.simba.cts.CtsResultReset.java
com.nj.simba.page.appmgr.AppTableRender.java
com.nj.simba.page.appmgr.DeviceAppTableModel.java
com.nj.simba.page.appmgr.PageAppMgr.java
com.nj.simba.page.device.DeviceListRender.java
com.nj.simba.page.device.DevicePanel.java
com.nj.simba.page.device.PageMyDevice.java
com.nj.simba.page.filer.FileTableRender.java
com.nj.simba.page.filer.PageFiler.java
com.nj.simba.page.logcat.ILogCatBufferChangeListener.java
com.nj.simba.page.logcat.LogCatMessageList.java
com.nj.simba.page.logcat.LogCatMessageParser.java
com.nj.simba.page.logcat.LogCatMessage.java
com.nj.simba.page.logcat.LogCatPidToNameMapper.java
com.nj.simba.page.logcat.LogCatReceiverFactory.java
com.nj.simba.page.logcat.LogCatReceiver.java
com.nj.simba.page.logcat.PageLogcat.java
com.nj.simba.utils.Config.java
com.nj.simba.utils.FileSyncHelper.java
com.nj.simba.utils.I2CTest.java
com.nj.simba.utils.IProgressMonitor.java
com.nj.simba.utils.SyncProgressHelper.java
com.nj.simba.utils.SyncProgressMonitor.java
com.nj.simba.utils.Utils.java