Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56fb84e

Browse files
committed
Use fancy formatting in EventLogger
1 parent 56c1959 commit 56fb84e

File tree

1 file changed

+65
-24
lines changed

1 file changed

+65
-24
lines changed

lib/models/event-logger.js

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,95 @@
1-
import util from 'util';
1+
import path from 'path';
2+
import nsfw from 'nsfw';
3+
4+
const actionText = new Map();
5+
actionText.set(nsfw.actions.CREATED, 'created');
6+
actionText.set(nsfw.actions.DELETED, 'deleted');
7+
actionText.set(nsfw.actions.MODIFIED, 'modified');
8+
actionText.set(nsfw.actions.RENAMED, 'renamed');
29

310
export default class EventLogger {
4-
constructor(name) {
5-
this.name = name;
6-
this.path = '<unknown>';
7-
this.enabled = !!process.env.ATOM_GITHUB_FS_EVENT_LOG;
8-
9-
if (this.enabled) {
10-
// eslint-disable-next-line no-console
11-
console.log(`Creating logger for ${this.name}`);
12-
}
11+
constructor(kind) {
12+
this.kind = kind;
13+
this.directory = '<unknown>';
14+
this.shortDirectory = '<unknown>';
1315
}
1416

15-
showStarted(path) {
16-
this.path = path;
17+
showStarted(directory) {
18+
this.directory = directory;
19+
this.shortDirectory = path.basename(directory);
1720

18-
if (!this.enabled) {
21+
if (!this.isEnabled()) {
1922
return;
2023
}
2124

22-
// eslint-disable-next-line no-console
23-
console.log(`${this.name} started watcher at path [${path}]`);
25+
this.shortLog('watcher started');
2426
}
2527

2628
showEvents(events) {
27-
if (!this.enabled) {
29+
if (!this.isEnabled()) {
2830
return;
2931
}
3032

31-
const prettyPrinted = util.inspect(events);
33+
const uniqueRelativeNames = new Set(events.map(event => {
34+
const fullPath = path.join(event.directory, event.file || event.newFile);
35+
return path.relative(this.directory, fullPath);
36+
}));
3237

33-
// eslint-disable-next-line no-console
34-
console.log(`${this.name} @ [${this.path}] received events:\n${prettyPrinted}`);
38+
const fileNames = Array.from(uniqueRelativeNames).slice(0, 3);
39+
const elipses = uniqueRelativeNames.size > 3 ? '...' : '';
40+
const summary = `${this.getShortName()}: ${fileNames.join(', ')}${elipses}`;
41+
42+
/* eslint-disable no-console */
43+
console.groupCollapsed(summary);
44+
console.table(events.map(event => ({
45+
directory: event.directory,
46+
file: event.file,
47+
newFile: event.newFile,
48+
action: actionText.get(event.action) || `(unknown: ${event.action})`,
49+
})), ['directory', 'action', 'file', 'newFile']);
50+
console.groupEnd();
51+
/* eslint-enable no-console */
52+
}
53+
54+
showFocusEvent() {
55+
if (!this.isEnabled()) {
56+
return;
57+
}
58+
59+
this.shortLog('focus triggered');
3560
}
3661

3762
showWorkdirOrHeadEvents() {
38-
if (!this.enabled) {
63+
if (!this.isEnabled()) {
3964
return;
4065
}
4166

42-
// eslint-disable-next-line no-console
43-
console.log(`${this.name} @ [${this.path}] about to broadcast did-change-workdir-or-head`);
67+
this.shortLog('working directory or HEAD change');
4468
}
4569

4670
showStopped() {
47-
if (!this.enabled) {
71+
if (!this.isEnabled()) {
4872
return;
4973
}
5074

75+
this.shortLog('stopped');
76+
}
77+
78+
isEnabled() {
79+
return atom.config.get('github.filesystemEventDiagnostics');
80+
}
81+
82+
getShortName() {
83+
return `${this.kind} @ ${this.shortDirectory}`;
84+
}
85+
86+
shortLog(line) {
5187
// eslint-disable-next-line no-console
52-
console.log(`${this.name} stopped watcher at path [${this.path}]`);
88+
console.log('%c%s%c: %s',
89+
'font-weight: bold; color: blue;',
90+
this.getShortName(),
91+
'font-weight: normal; color: black;',
92+
line,
93+
);
5394
}
5495
}

0 commit comments

Comments
 (0)