forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoutput_logger.js
108 lines (96 loc) · 2.26 KB
/
output_logger.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Provides output logger.
*/
import {LocalStorage} from '/common/local_storage.js';
import {LogType} from '../../common/log_types.js';
import {LogStore} from '../logging/log_store.js';
import {OutputRuleSpecifier} from './output_rules.js';
export class OutputFormatLogger {
/**
* @param {string} enableKey The key to enable logging in LocalStorage
* @param {!LogType} type
*/
constructor(enableKey, type) {
/** @private {string} */
this.str_ = '';
/** @private {string} */
this.storageEnabledKey_ = enableKey;
/** @private {!LogType} */
this.logType_ = type;
}
/** @return {boolean} */
get loggingDisabled() {
return !LocalStorage.get(this.storageEnabledKey_);
}
/** Sends the queued logs to the LogStore. */
commitLogs() {
if (this.str_) {
LogStore.instance.writeTextLog(this.str_, this.logType_);
}
}
/** @param {string} str */
write(str) {
if (this.loggingDisabled) {
return;
}
this.str_ += str;
}
/**
* @param {string} token
* @param {string|undefined} value
*/
writeTokenWithValue(token, value) {
if (this.loggingDisabled) {
return;
}
this.writeToken(token);
if (value) {
this.str_ += value;
} else {
this.str_ += 'EMPTY';
}
this.str_ += '\n';
}
/** @param {string} token */
writeToken(token) {
if (this.loggingDisabled) {
return;
}
this.str_ += '$' + token + ': ';
}
/**
* @param {OutputRuleSpecifier} rule
*/
writeRule(rule) {
if (this.loggingDisabled) {
return;
}
this.str_ += 'RULE: ';
this.str_ += rule.event + ' ' + rule.role;
if (rule.navigation) {
this.str_ += ' ' + rule.navigation;
}
if (rule.output) {
this.str_ += ' ' + rule.output;
}
this.str_ += '\n';
}
bufferClear() {
if (this.loggingDisabled) {
return;
}
this.str_ += '\nBuffer is cleared.\n';
}
/** @param {string} errorMsg */
writeError(errorMsg) {
if (this.loggingDisabled) {
return;
}
this.str_ += 'ERROR with message: ';
this.str_ += errorMsg;
this.str_ += '\n';
}
}