我正在尝试将this coffeescript转换为javascript代码并在node.js上运行。该代码块从Android设备解析logcat日志。
这是我的工作:
var fs = require('fs');
fs.readFile("logcat.log", 'utf8', function (err, data) {
parse(data)
});
var PATTERNS = {
brief: new RegExp("^(?<level>[VDIWEAF])\\/(?<tag>[^)]{0,23}?)\\(\\s*(?<pid>\\d+)\\):\\s+(?<message>.*)$"),
threadtime: new RegExp("^(?<timestamp>\\d\\d-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d\\.\\d+)\\s*(?<pid>\\d+)\\s*(?<tid>\\d+)\\s(?<level>[VDIWEAF])\\s(?<tag>.*?):\\s+(?<message>.*)$"),
time: new RegExp("^(?<timestamp>\\d\\d-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d\\.\\d+):*\\s(?<level>[VDIWEAF])\\/(?<tag>.*?)\\((?<pid>\\s*\\d+)\\):\\s+(?<message>.*)$"),
process: new RegExp("^(?<level>[VDIWEAF])\\(\\s*(?<pid>\\d+)\\)\\s+(?<message>.*)$"),
tag: new RegExp("^(?<level>[VDIWEAF])\\/(?<tag>[^)]{0,23}?):\\s+(?<message>.*)$"),
thread: new RegExp("^(?<level>[VDIWEAF])\\(\\s*(?<pid>\\d+):(?<tid>0x.*?)\\)\\s+(?<message>.*)$"),
ddms_save: new RegExp("^(?<timestamp>\\d\\d-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d\\.\\d+):*\\s(?<level>VERBOSE|DEBUG|ERROR|WARN|INFO|ASSERT)\\/(?<tag>.*?)\\((?<pid>\\s*\\d+)\\):\\s+(?<message>.*)$")
}
var LEVELS = {
V: "verbose",
D: "debug",
I: "info",
W: "warn",
E: "error",
A: "assert",
F: "fatal",
S: "silent"
};
get_type = function (line) {
PATTERNS.forEach(element => {
if (element.test(line)) {
return element.keys(a)[0];
}
return null;
});
}
function parse(contents) {
var type = null;
var messages = [];
var badlines = 0;
contents.split("\n").forEach(line => {
line = line.replace(/\s+$/g, "");
if (!type) {
type = get_type(line)
}
var regex = PATTERNS[type];
if (type && line.length > 0) {
try {
match = regex.exec(line);
if (match.includes('level')) {
message.level = match.level;
}
if (match.includes('timestamp')) {
message.timestamp = match.level;
}
messages.push(message);
} catch (error) {
badlines++;
}
}
});
return {
type: type,
messages: messages,
badlines: badlines
}
}
此正则表达式行出现错误:
^(?<level>[VDIWEAF])\\/(?<tag>[^)]{0,23}?)\\(\\s*(?<pid>\\d+)\\):\\s+(?<message>.*)$
错误消息是:
无效的正则表达式:/^(?[VDIWEAF])/(?[^)]{0,23}?)(\s*(?\d+)):\s+(?.*)$/:无效的组