ChkBugReport工具 for Android 3

xml 插件扩展 hook子标签支持

对应源码 KernelLogPlugin, 相较于 SystemLogPlugin MainLogPlugin EventLogPlugin  (即 LogPlugin)或者 BatteryInfoPlugin 都有自己对应的Hooks.java 。

参照 LogPlugin 的  com.sonyericsson.chkbugreport.plugins.logs.Hooks.java 添加 com.sonyericsson.chkbugreport.plugins.logs.kernel.Hooks.java:

<span style="color:#000000"><span style="color:#333333"><span style="color:#333333">/**
 * Date 2018/12/1
 */
package com.sonyericsson.chkbugreport.plugins.logs.kernel;

import com.sonyericsson.chkbugreport.BugReportModule;
import com.sonyericsson.chkbugreport.doc.*;
import com.sonyericsson.chkbugreport.plugins.logs.LogLine;
import com.sonyericsson.chkbugreport.plugins.logs.LogLines;
import com.sonyericsson.chkbugreport.plugins.logs.LogMatcher;
import com.sonyericsson.chkbugreport.util.Util;
import com.sonyericsson.chkbugreport.util.XMLNode;

import java.util.Vector;

public class Hooks {

    private Vector<XMLNode> mHooks = new Vector<XMLNode>();
    private KernelLogPlugin mPlugin;

    public Hooks(KernelLogPlugin plugin) {
        mPlugin = plugin;
    }

    public void reset() {
        mHooks.clear();
    }

    public void add(XMLNode hook) {
        mHooks.add(hook);
    }

    public void execute(BugReportModule mod) {
        for (XMLNode hook : mHooks) {
            execute(mod, hook);
        }
    }

    private void execute(BugReportModule mod, XMLNode hook) {
        for (XMLNode item : hook) {
            String tag = item.getName();
            if (tag == null) continue;
            if (tag.equals("filter")) {
                filterLog(mod, item);
            } else {
                mod.printErr(5, "Unknown log hook: " + tag);
            }
        }
    }

    private void filterLog(BugReportModule mod, XMLNode filter) {
        LogMatcher lm = new LogMatcher(mod, filter);
        LogLines kernellog = (LogLines) mod.getInfo(KernelLogPlugin.INFO_ID_KERNEL_LOG);
        LogLines kernellog_lk = (LogLines) mod.getInfo(KernelLogPlugin.INFO_ID_LAST_KMSG);
        for (LogLine ll : kernellog) {
            if (lm.matches(ll)) {
                executeAction(mod, filter, ll);
            }
        }
        for (LogLine ll : kernellog_lk) {
            if (lm.matches(ll)) {
                executeAction(mod, filter, ll);
            }
        }
    }

    private void executeAction(BugReportModule mod, XMLNode filter, LogLine ll) {
        for (XMLNode action : filter) {
            String tag = action.getName();
            if (tag == null) continue;
            if (tag.equals("hide")) {
                ll.setHidden(true);
            } else if (tag.equals("note")) {
                addNote(mod, ll, action);
            } else if (tag.equals("bug")) {
                addBug(mod, ll, action);
            } else {
                mod.printErr(5, "Unknown log filter action: " + tag);
            }
        }
    }

    private void addNote(BugReportModule mod, LogLine ll, XMLNode action) {
        String text = action.getAttr("text");
        String css = "log-float";
        if (Util.parseBoolean(action.getAttr("error"), false)) {
            css = "log-float-err";
        }
        if (text == null) {
            mod.printErr(5, "Missing text attribute from 'note' log filter action!");
        } else {
            ll.addMarker(css, text, null);
        }
    }

    private void addBug(BugReportModule mod, LogLine ll, XMLNode action) {
        String text = action.getAttr("text");
        String title = action.getAttr("title");
        String type = action.getAttr("type");
        Bug.Type bugType = Bug.Type.PHONE_WARN;
        int prio = Util.parseInt(action.getAttr("prio"), 100);
        if (title == null) {
            mod.printErr(5, "Missing title attribute from 'note' log filter action!");
            return;
        }
        if (type != null) {
            if ("phone err".equals(type)) {
                bugType = Bug.Type.PHONE_ERR;
            } else if ("phone warn".equals(type)) {
                bugType = Bug.Type.PHONE_WARN;
            } else if ("tool err".equals(type)) {
                bugType = Bug.Type.TOOL_ERR;
            } else if ("tool warn".equals(type)) {
                bugType = Bug.Type.TOOL_WARN;
            } else {
                mod.printErr(5, "Invalid value for 'type' attribute: " + type + ", must be one of 'phone err', 'phone warn', 'tool err', 'tool warn'!");
            }
        }
        Bug bug = new Bug(bugType, prio, ll.ts, title);
        new Block(bug).add(new Link(ll.getAnchor(), "(link to log)"));
        if (text != null) {
            new Para(bug).add(text);
        }
        DocNode log = new Block(bug).addStyle("log");
        log.add(ll.symlink());
        mod.addBug(bug);
    }
}</span></span></span>

 

参照 LogPlugin.java ,在 KernelLogPlugin.java 调用:

<span style="color:#000000"><span style="color:#333333">    private Hooks mHooks = new Hooks(this);

   
    @Override
    public void load(Module rep) {
        。。。。。。
        // Execute the hooks
        mHooks.execute(br);
    }</span></span>

hook功能扩展使用

在 C:\Users\Administrator\.chkbugreport\example.xml 中:

<span style="color:#000000"><span style="color:#333333"><plugin name="ExamplePlugin">
	<hook into="KernelLogPlugin">
		<filter matchMsg="reboot">
			<!-- Add a yellow side-note to the log -->
			<note text="Reboot" error="true"/>
		</filter>
		<filter matchMsg="lowmemorykiller">
			<!-- Create a link from the "Errors" chapter -->
			<note title="lowmemorykiller" error="true" />
		</filter>
		<filter matchMsg="lowmemorykiller">
			<!-- Create a link from the "Errors" chapter -->
			<bug title="Kernel Reboot" text="Restarting system?!" prio="100" type="phone err" />
		</filter>
		<filter matchMsg="Restarting system">
			<bug title="Kernel Reboot" text="!!!系统重启了!!!\n" prio="100" type="phone err" />
		</filter>
		<filter matchMsg="Restarting system">
			<!-- Delete log lines -->
			<hide />
		</filter>
	</hook>
</plugin></span></span>

HTML报告截图:

目前 hook功能局限性

  • note:HTML报告中 Kernel log/log 不生效;Last kmsg/log 生效


    note

  • bug:HTML报告中 Kernel log/log 和 Last kmsg/log 都生效

  • hide:HTML报告中 Kernel log/log 和 Last kmsg/log 都不生效,对比 LogPlugin.java 需要再移除需隐藏 Log

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xhBruce

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值