hdckit
Version:
A pure Node.js client for the OpenHarmony Device Connector
88 lines (87 loc) • 2.91 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Emitter_1 = __importDefault(require("licia/Emitter"));
const each_1 = __importDefault(require("licia/each"));
const toNum_1 = __importDefault(require("licia/toNum"));
const idxOf_1 = __importDefault(require("licia/idxOf"));
const trim_1 = __importDefault(require("licia/trim"));
class Hilog extends Emitter_1.default {
constructor(command) {
super();
this.command = command;
this.ended = false;
this.data = '';
this.read();
}
end() {
this.ended = true;
this.command.connection.end();
}
async read() {
if (this.ended) {
return;
}
const buf = await this.command.connection.readValue();
this.parse(buf);
this.read();
}
parse(buf) {
let { data } = this;
data += buf.toString();
const rawEntries = [];
regEntryStart.lastIndex = 0;
let match = regEntryStart.exec(data);
let nextMatch = regEntryStart.exec(data);
while (match && nextMatch) {
rawEntries.push(data.substring(match.index, nextMatch.index));
match = nextMatch;
nextMatch = regEntryStart.exec(data);
}
(0, each_1.default)(rawEntries, (rawEntry) => {
const entry = this.parseEntry(rawEntry);
if (entry) {
this.emit('entry', this.parseEntry(rawEntry));
}
});
this.data = match ? data.substring(match.index) : '';
}
parseEntry(rawEntry) {
const entry = new Entry();
const match = rawEntry.match(regEntry);
if (match) {
entry.date = new Date((0, toNum_1.default)(match[1]) * 1000);
entry.pid = (0, toNum_1.default)(match[2]);
entry.tid = (0, toNum_1.default)(match[3]);
entry.level = toLevel(match[4]);
entry.type = toType(match[5]);
entry.domain = match[6];
entry.tag = match[7];
entry.message = (0, trim_1.default)(match[8]);
return entry;
}
}
}
exports.default = Hilog;
class Entry {
constructor() {
this.date = null;
this.pid = -1;
this.tid = -1;
this.level = -1;
this.type = -1;
this.domain = '';
this.tag = '';
this.message = '';
}
}
function toLevel(letter) {
return (0, idxOf_1.default)(['?', '?', 'V', 'D', 'I', 'W', 'E', 'F'], letter);
}
function toType(typePrefix) {
return (0, idxOf_1.default)(['A', 'I', 'C', 'K', 'P'], typePrefix);
}
const regEntryStart = /\d{10}\.\d{3}\s+\d+\s+\d+/g;
const regEntry = /^(\d{10}\.\d{3})\s+(\d+)\s+(\d+)\s+([DIWEF])\s+([AICKP])(.{5})\/([^:]*):(.*)/;