版权声明:本文为博主经验积累 https://blog.csdn.net/qq_23452385/article/details/87539424
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 都生效 扫描二维码关注公众号,回复: 5885896 查看本文章
- hide:HTML报告中 Kernel log/log 和 Last kmsg/log 都不生效,对比 LogPlugin.java 需要再移除需隐藏 Log