Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 88ad30b

Browse files
committed
Have enabling/disabling debugging not require restarting Atom
1 parent df4dfeb commit 88ad30b

File tree

6 files changed

+154
-24
lines changed

6 files changed

+154
-24
lines changed

.editorconfig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ max_line_length = 80
1111
tab_width = 2
1212
trim_trailing_whitespace = true
1313

14-
[*.{js,ts,coffee}]
14+
[*.{ts,coffee}]
15+
quote_type = single
16+
17+
[*.js]
18+
indent_size = 4
19+
tab_width = 4
1520
quote_type = single

lib/locale-checker.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const spellchecker = require('spellchecker');
22
const pathspec = require('atom-pathspec');
33
const env = require('./checker-env');
4+
const createLog = require('./logging');
45

56
// The locale checker is a checker that takes a locale string (`en-US`) and
67
// optionally a path and then checks it.
@@ -21,12 +22,8 @@ class LocaleChecker {
2122
this.enabled = true;
2223
this.hasSystemChecker = hasSystemChecker;
2324
this.inferredLocale = inferredLocale;
24-
if (atom.config.get('spell-check.enableDebug')) {
25-
debug = require('debug');
26-
this.log = debug('spell-check:locale-checker').extend(locale);
27-
} else {
28-
this.log = (str) => {};
29-
}
25+
this.log = createLog().extend('locale-checker').extend(locale);
26+
3027
this.log(
3128
'enabled',
3229
this.isEnabled(),

lib/logging.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// To avoid increasing the startup time and not loading modules that aren't
2+
// required, we use a thin wrapper around the `debug` logging library that
3+
// allows us to configure it via the Atom settings.
4+
5+
let enabled = atom.config.get('spell-check.enableDebug');
6+
let loggers = {};
7+
8+
function updateLocalStorage() {
9+
// If we aren't enabled, we do nothing.
10+
if (!enabled) {
11+
return;
12+
}
13+
14+
// `debug` requires `localStorage.debug` to contain the prefix for keys.
15+
// Because we have a configuration, we make sure they are there to avoid
16+
// a second step to enabling logging. We only check for the presence of
17+
// *any* spell-check reference so the user can customize it without worrying
18+
// about it being overridden.
19+
if (localStorage.debug === undefined) {
20+
localStorage.debug = '';
21+
}
22+
23+
if (localStorage.debug.indexOf('spell-check') < 0) {
24+
let keys = localStorage.debug.split(',').filter((x) => x !== '');
25+
keys.push('spell-check');
26+
keys.push('spell-check:*');
27+
localStorage.debug = keys.join(',');
28+
}
29+
}
30+
31+
/**
32+
* Updates the registered loggers along with the new one to use `debug` instead
33+
* of the internal, null sink.
34+
*/
35+
function update() {
36+
// If we aren't enabled, then don't do anything.
37+
enabled = atom.config.get('spell-check.enableDebug');
38+
39+
// Go through all the existing loggers and rebind or set them up using the
40+
// new settings.
41+
for (const scope in loggers) {
42+
if (loggers.hasOwnProperty(scope)) {
43+
// Pull out the current logger and make sure it is properly enabled.
44+
let config = loggers[scope];
45+
46+
config.wrapper.enabled = enabled;
47+
48+
// If we are enabled, then load `debug` and use that to create a
49+
// proper log sink using that package.
50+
if (enabled) {
51+
const debug = require('debug');
52+
53+
config.sink = debug(scope);
54+
config.sink.log = console.log.bind(console);
55+
}
56+
}
57+
}
58+
59+
// Make sure the local storage keys.
60+
updateLocalStorage();
61+
}
62+
63+
/**
64+
* Creates a logger based on the atom settings. If the user has enabled logging
65+
* for spell-check, then this also ensures that the spell-check entries will be
66+
* added to localStorage for debugging.
67+
*
68+
* @param {string} scope The name of the scope, such as "spell-check" or
69+
"spell-check:locale-checker:en-US".
70+
* @returns A lambda that is either does nothing or it is the full debug call.
71+
*/
72+
function create(scope) {
73+
// See if we already have a logger defined for this value.
74+
if (loggers[scope] !== undefined) {
75+
return loggers[scope].wrapper;
76+
}
77+
78+
// We use a logger object to contain all the variables and components of
79+
// a log at each level. We do this so we can turn logging on or after based
80+
// on editor settings.
81+
let config = {
82+
scope: scope,
83+
sink: undefined,
84+
};
85+
86+
// If we are enabled, then we've already loaded the `debug` module.
87+
if (enabled) {
88+
const debug = require('debug');
89+
config.sink = debug(scope);
90+
config.sink.log = console.log.bind(console);
91+
}
92+
93+
// Create the function that will actually perform the logging. This function
94+
// uses the `arguments` property to pass values into the inner logger.
95+
config.wrapper = function () {
96+
if (config.sink !== undefined) {
97+
config.sink.apply(config.sink, arguments);
98+
}
99+
};
100+
101+
/**
102+
* The `extend` method is used to create a new logger with a ":" between the
103+
* old scope and the new one.
104+
*
105+
* @param {string} newScope The name of the inner scope to extend.
106+
* @returns The logging function.
107+
*/
108+
config.extend = function (newScope) {
109+
return create([scope, newScope].join(':'));
110+
};
111+
112+
// Wrap everything into the wrapper so it acts like `debug`.
113+
config.wrapper.config = config;
114+
config.wrapper.extend = config.extend;
115+
config.wrapper.update = update;
116+
config.wrapper.enabled = enabled;
117+
118+
// Cache the loggers for updating later and return it.
119+
loggers[scope] = config;
120+
121+
return config.wrapper;
122+
}
123+
124+
/**
125+
* Creates the root logger.
126+
*/
127+
function createRoot() {
128+
return create('spell-check');
129+
}
130+
131+
// Set up the first-time calls.
132+
updateLocalStorage();
133+
134+
module.exports = createRoot;

lib/main.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { CompositeDisposable } = require('atom');
2+
const createLog = require('./logging');
23

34
let SpellCheckView = null;
45
let spellCheckViews = {};
@@ -9,11 +10,7 @@ let log = (str) => {};
910

1011
module.exports = {
1112
activate() {
12-
if (atom.config.get('spell-check.enableDebug')) {
13-
debug = require('debug');
14-
log = debug('spell-check');
15-
}
16-
13+
log = createLog();
1714
log('initializing');
1815

1916
this.subs = new CompositeDisposable();
@@ -115,6 +112,11 @@ module.exports = {
115112
}
116113
)
117114
);
115+
this.subs.add(
116+
atom.config.onDidChange('spell-check.enableDebug', (_) => {
117+
log.update();
118+
})
119+
);
118120

119121
// Hook up the UI and processing.
120122
this.subs.add(

lib/spell-check-manager.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const env = require('./checker-env');
2+
const createLog = require('./logging');
23

34
class SpellCheckerManager {
45
static initClass() {
@@ -423,12 +424,7 @@ class SpellCheckerManager {
423424

424425
init() {
425426
// Set up logging.
426-
if (atom.config.get('spell-check.enableDebug')) {
427-
debug = require('debug');
428-
this.log = debug('spell-check:spell-check-manager');
429-
} else {
430-
this.log = (str) => {};
431-
}
427+
this.log = createLog().extend('spell-check-manager');
432428

433429
// Set up the system checker.
434430
const hasSystemChecker = this.useSystem && env.isSystemSupported();

lib/system-checker.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ let instance;
22
const spellchecker = require('spellchecker');
33
const pathspec = require('atom-pathspec');
44
const env = require('./checker-env');
5+
const createLog = require('./logging');
56

67
// Initialize the global spell checker which can take some time. We also force
78
// the use of the system or operating system library instead of Hunspell.
@@ -23,12 +24,7 @@ if (env.isSystemSupported()) {
2324
// due to some memory bug.
2425
class SystemChecker {
2526
constructor() {
26-
if (atom.config.get('spell-check.enableDebug')) {
27-
debug = require('debug');
28-
this.log = debug('spell-check:system-checker');
29-
} else {
30-
this.log = (str) => {};
31-
}
27+
this.log = createLog().extend('system-checker');
3228
this.log('enabled', this.isEnabled(), this.getStatus());
3329
}
3430

0 commit comments

Comments
 (0)