|
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'); |
2 | 9 |
|
3 | 10 | 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>'; |
13 | 15 | }
|
14 | 16 |
|
15 |
| - showStarted(path) { |
16 |
| - this.path = path; |
| 17 | + showStarted(directory) { |
| 18 | + this.directory = directory; |
| 19 | + this.shortDirectory = path.basename(directory); |
17 | 20 |
|
18 |
| - if (!this.enabled) { |
| 21 | + if (!this.isEnabled()) { |
19 | 22 | return;
|
20 | 23 | }
|
21 | 24 |
|
22 |
| - // eslint-disable-next-line no-console |
23 |
| - console.log(`${this.name} started watcher at path [${path}]`); |
| 25 | + this.shortLog('watcher started'); |
24 | 26 | }
|
25 | 27 |
|
26 | 28 | showEvents(events) {
|
27 |
| - if (!this.enabled) { |
| 29 | + if (!this.isEnabled()) { |
28 | 30 | return;
|
29 | 31 | }
|
30 | 32 |
|
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 | + })); |
32 | 37 |
|
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'); |
35 | 60 | }
|
36 | 61 |
|
37 | 62 | showWorkdirOrHeadEvents() {
|
38 |
| - if (!this.enabled) { |
| 63 | + if (!this.isEnabled()) { |
39 | 64 | return;
|
40 | 65 | }
|
41 | 66 |
|
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'); |
44 | 68 | }
|
45 | 69 |
|
46 | 70 | showStopped() {
|
47 |
| - if (!this.enabled) { |
| 71 | + if (!this.isEnabled()) { |
48 | 72 | return;
|
49 | 73 | }
|
50 | 74 |
|
| 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) { |
51 | 87 | // 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 | + ); |
53 | 94 | }
|
54 | 95 | }
|
0 commit comments