|
| 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; |
0 commit comments