-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloggingWindow.js
More file actions
executable file
·262 lines (238 loc) · 9.84 KB
/
loggingWindow.js
File metadata and controls
executable file
·262 lines (238 loc) · 9.84 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/gjs
const { Gtk, GObject, Gio, GLib } = imports.gi;
const BORDER_WIDTH = 2;
const SCHEMA = 'org.gnome.shell.extensions.debug';
const EXTENSION_PATH = '/usr/share/gnome-shell/extensions/';
const Gettext = imports.gettext;
const _ = Gettext.domain('debug').gettext;
Gtk.init(null);
const ListBoxWindow = GObject.registerClass({
GTypeName: 'ListBoxWindow',
Properties: {
'test-property': GObject.param_spec_variant(
'test-property',
'nickname',
'GObject property preset for testing',
new GLib.VariantType('as'),
null,
GObject.ParamFlags.READWRITE
),
},
Signals: {
'test-signal': {},
},
}, class ListBoxWindow extends Gtk.Window {
_init(channel, level, type, filename, resolution) {
super._init({ title: _('Debug Log Viewer') });
this.border_width = BORDER_WIDTH;
this._channel = channel;
this._level = level;
this._type = type;
if (!filename)
this._filename = `${GLib.get_home_dir()}/.tos_shell_log.out`;
else
this._filename = filename;
this._resolution = resolution;
this._settings = new Gio.Settings({ schema: SCHEMA });
this._boxOuter = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL, spacing: BORDER_WIDTH });
this.set_default_size(parseInt(resolution[0]), parseInt(resolution[1]));
this.add(this._boxOuter);
this._dconfSetup();
this._updateFileMonitor();
this._updateLogBox();
}
_dconfSetup() {
let channelConfig = this._settings.get_strv('visibility-channel');
if (channelConfig[0] === 'ALL' && channelConfig.length < 2) {
let extensionList = this._readExtensionList(EXTENSION_PATH);
extensionList.push('ALL');
this._settings.set_strv('visibility-channel', extensionList);
}
this._settings.connect('changed::visibility-channel', (_settings, _key) => {
this._channel = this._settings.get_strv(_key);
this._logBox.get_child()._filter.refilter();
});
this._settings.connect('changed::visibility-level', (_settings, _key) => {
this._level = this._settings.get_string(_key);
this._logBox.get_child()._filter.refilter();
});
this._settings.connect('changed::visibility-type', (_settings, _key) => {
this._type = this._settings.get_string(_key);
this._logBox.get_child()._filter.refilter();
});
this._settings.connect('changed::filename', (_settings, _key) => {
this._filename = this._settings.get_string(_key);
this._fileMonitor.cancel();
this._logBox.destroy();
this._updateFileMonitor();
this._updateLogBox();
});
this._settings.connect('changed::resolution', (_settings, _key) => {
this._resolution = this._settings.get_strv(_key);
this.set_default_size(parseInt(this._resolution[0]), parseInt(this._resolution[1]));
});
}
_readExtensionList(path) {
let extensionList = [];
let fileEnum;
try {
fileEnum = Gio.File.new_for_path(path).enumerate_children('standard::name', Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
} catch (e) {
print('Unable to load extension list');
return extensionList;
}
let extensionDir = fileEnum.next_file(null);
while (extensionDir) {
let metadata = this._parseJson(`${path}${extensionDir.get_attribute_as_string('standard::name')}/metadata.json`);
if (metadata)
extensionList.push(metadata);
extensionDir = fileEnum.next_file(null);
}
return extensionList;
}
_parseJson(json) {
try {
let [result, contents] = GLib.file_get_contents(json);
if (result)
return JSON.parse(String.fromCharCode.apply(null, contents))['extension-id'];
} catch (e) {
print(`Failed to read ${json}`);
}
}
_updateFileMonitor() {
this._fileMonitor = Gio.File.new_for_path(this._filename).monitor(Gio.FileMonitorFlags.NONE, null);
this._fileMonitor.connect('changed', () => {
this._logBox.destroy();
this._updateLogBox();
});
}
_updateLogBox() {
let items = this._readLog(this._filename);
this._logBox = new addScroll(new logListBox(items));
this._logBox.get_child()._filter.set_visible_func(this._filterFunc.bind(this));
this._logBox.get_child()._filter.refilter();
this._boxOuter.pack_start(this._logBox, true, true, 0);
this._boxOuter.show_all();
let children = this._logBox.get_child().get_model().iter_n_children(null);
if (children > 0) {
let lastIter = this._logBox.get_child().get_model().get_iter_from_string(String(children - 1));
let lastPath = this._logBox.get_child().get_model().get_path(lastIter[1]);
this._logBox.get_child().scroll_to_cell(lastPath, null, true, 0.0, 1.0);
}
}
_readLog(filename) {
try {
let [__, contents] = GLib.file_get_contents(filename);
return this._parseLog(contents);
} catch (e) {
return [['', '', '', '', _('no log file found')]];
}
}
_parseLog(log) {
let lines = String.fromCharCode.apply(null, log).split('\n').filter(e => {
return e !== '';
});
let result = [];
for (let line of lines) {
let temp = line.split(/(\[|\]\[|\])/);
if (temp.length >= 6)
result.push([temp[2], temp[4], temp[6], temp[8], `${temp.slice(10,).join('')}`]);
else
result.push(['', '', '', '', line]);
}
return result;
}
_filterFunc(row, iter) {
let channelVisible = this._channel.includes(row.get_value(iter, 0)) || this._channel.includes('ALL');
let levelVisible = row.get_value(iter, 1) === this._level || this._level === 'ALL';
let typeVisible = row.get_value(iter, 2) === this._type || this._type === 'All';
return channelVisible && levelVisible && typeVisible;
}
});
const logListBox = GObject.registerClass({
GTypeName: 'logListBox',
}, class logListBox extends Gtk.TreeView {
_init(items) {
super._init({ expand: true });
this._lastClicked = '';
this._clickCount = 0;
this._filter = new Gtk.TreeModelFilter({ 'child-model': new listStoreWithData(items) });
this.set_model(new Gtk.TreeModelSort({ 'model': this._filter }));
this._setColumn(_('Channel'), 0, 'color-channel');
this._setColumn(_('Level'), 1, 'color-level');
this._setColumn(_('Type'), 2, 'color-type');
this._setColumn(_('Date'), 3, 'color-date');
this._setColumn(_('Message'), 4, 'color-message');
}
_setColumn(title, columnId, key) {
this.append_column(new sortableColumn(title, columnId, settings.get_string(key)));
// settings.bind('color-channel',this.get_column(0).get_cells()[0], 'foreground', 0);
settings.connect(`changed::${key}`, (_settings, _key) => {
this._updateFontColor(_settings, _key, columnId);
});
this.get_column(columnId).connect('clicked', () => {
this._clickCount += 1;
if (this._lastClicked === '')
this._lastClicked = this.get_column(columnId).title;
if (this._lastClicked !== this.get_column(columnId).title) {
this.set_model(new Gtk.TreeModelSort({ 'model': this._filter }));
this._lastClicked = '';
this._clickCount = 0;
this.get_column(columnId).clicked();
} else if (this._clickCount >= 3) {
this.set_model(new Gtk.TreeModelSort({ 'model': this._filter }));
this._lastClicked = '';
this._clickCount = 0;
}
});
}
_updateFontColor(settings, key, index) {
this.get_column(index).clear();
this.get_column(index)._color = new Gtk.CellRendererText({ foreground: settings.get_string(key) });
this.get_column(index).pack_start(this.get_column(index)._color, true);
this.get_column(index).add_attribute(this.get_column(index)._color, 'text', index);
}
});
const listStoreWithData = GObject.registerClass({
GTypeName: 'listStoreWithData',
}, class listStoreWithData extends Gtk.ListStore {
_init(items) {
super._init();
this.set_column_types([GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_STRING]);
items.forEach(
item => this.set(this.append(), [0, 1, 2, 3, 4], item)
);
}
});
const sortableColumn = GObject.registerClass({
GTypeName: 'sortableColumn',
}, class sortableColumn extends Gtk.TreeViewColumn {
_init(title, index, color = 'black') {
super._init({ title });
this._color = new Gtk.CellRendererText({ foreground: color });
this.set_sort_column_id(index);
this.set_resizable(true);
this.pack_start(this._color, true);
this.add_attribute(this._color, 'text', index);
}
});
const addScroll = GObject.registerClass({
GTypeName: 'addScroll',
}, class addScroll extends Gtk.ScrolledWindow {
_init(content) {
super._init();
this.add(content);
}
});
let settings = new Gio.Settings({ schema: SCHEMA });
let arg1 = settings.get_strv('visibility-channel');
let arg2 = settings.get_string('visibility-level');
let arg3 = settings.get_string('visibility-type');
let arg4 = settings.get_string('filename');
let arg5 = settings.get_strv('resolution');
let win = new ListBoxWindow(arg1, arg2, arg3, arg4, arg5);
win.connect('delete-event', () => {
Gtk.main_quit();
});
win.show_all();
Gtk.main();