UNPKG

logkitten

Version:

Stream Android and iOS logs without Android Studio or Console.app, with programmatic Node.js API for log analysis.

67 lines (66 loc) 2.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AndroidParser = void 0; var _utils = require("../utils"); var _utils2 = require("./utils"); function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } class AndroidParser { constructor() { _defineProperty(this, "_buffer", new _utils.LineBuffer()); // In fact, this is: [] | [AndroidEntry] _defineProperty(this, "_pendingEntry", []); } static create() { const parser = new AndroidParser(); return parser.parse.bind(parser); } parse(raw, stderr) { if (stderr) { return []; } const str = typeof raw === 'string' ? raw : raw.toString('utf-8'); const lines = this._buffer.processChunk(str); return this._parseMessages(lines); } _parseMessages(messages) { const newEntries = messages.map(rawMessage => { const match = rawMessage.match(AndroidParser.logcatRegex); if (match) { const [, epoch, uid, pid, tid, level, tag, message] = match; return { ts: Math.floor(parseFloat(epoch) * 1000), pid: Number.parseInt(pid, 10), tid: Number.parseInt(tid, 10), level: _utils2.PriorityUtils.fromLetter(level), tag: tag.trim() || 'unknown', msg: message.trim(), uid }; } return null; }); const result = newEntries.reduce((acc, entry) => { if (!entry) { return acc; } const last = acc.at(-1); if (last && (0, _utils2.areEntriesComingFromTheSameSource)(last, entry)) { last.msg += '\n' + entry.msg; return acc; } acc.push(entry); return acc; }, this._pendingEntry); this._pendingEntry = result.splice(-1); return result; } } exports.AndroidParser = AndroidParser; _defineProperty(AndroidParser, "logcatRegex", // epoch uid pid tid level tag message /^\s*(\d+\.\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\w)\s+(.+?)\s*:\s*(.*)$/m); //# sourceMappingURL=parser.js.map