-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathlog-parser-service.ts
47 lines (39 loc) · 1.49 KB
/
log-parser-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { DEVICE_LOG_EVENT_NAME } from "../common/constants";
import { cache } from "../common/decorators";
import { EventEmitter } from "events";
export class LogParserService extends EventEmitter implements ILogParserService {
private parseRules: IDictionary<ILogParseRule> = {};
constructor(private $deviceLogProvider: Mobile.IDeviceLogProvider,
private $errors: IErrors,
private $previewAppLogProvider: IPreviewAppLogProvider) {
super();
}
public addParseRule(rule: ILogParseRule): void {
if (this.parseRules[rule.name]) {
this.$errors.fail("Log parse rule already exists.");
}
this.parseRules[rule.name] = rule;
this.startParsingLogCore();
}
@cache()
private startParsingLogCore(): void {
this.$deviceLogProvider.on(DEVICE_LOG_EVENT_NAME, this.processDeviceLogResponse.bind(this));
this.$previewAppLogProvider.on(DEVICE_LOG_EVENT_NAME, (deviceId: string, message: string) => {
this.processDeviceLogResponse(message, deviceId);
});
}
private processDeviceLogResponse(message: string, deviceIdentifier: string, devicePlatform?: string) {
const lines = message.split("\n");
_.forEach(lines, line => {
_.forEach(this.parseRules, parseRule => {
if (!devicePlatform || !parseRule.platform || parseRule.platform.toLowerCase() === devicePlatform.toLowerCase()) {
const matches = parseRule.regex.exec(line);
if (matches) {
parseRule.handler(matches, deviceIdentifier);
}
}
});
});
}
}
$injector.register("logParserService", LogParserService);