123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- var adapters = [];
- var modifiers = [];
- var logger = function devnull() {};
- function use(adapter) {
- if (~adapters.indexOf(adapter)) return false;
- adapters.push(adapter);
- return true;
- }
- function set(custom) {
- logger = custom;
- }
- function enabled(namespace) {
- var async = [];
- for (var i = 0; i < adapters.length; i++) {
- if (adapters[i].async) {
- async.push(adapters[i]);
- continue;
- }
- if (adapters[i](namespace)) return true;
- }
- if (!async.length) return false;
-
-
-
-
-
-
- return new Promise(function pinky(resolve) {
- Promise.all(
- async.map(function prebind(fn) {
- return fn(namespace);
- })
- ).then(function resolved(values) {
- resolve(values.some(Boolean));
- });
- });
- }
- function modify(fn) {
- if (~modifiers.indexOf(fn)) return false;
- modifiers.push(fn);
- return true;
- }
- function write() {
- logger.apply(logger, arguments);
- }
- function process(message) {
- for (var i = 0; i < modifiers.length; i++) {
- message = modifiers[i].apply(modifiers[i], arguments);
- }
- return message;
- }
- function introduce(fn, options) {
- var has = Object.prototype.hasOwnProperty;
- for (var key in options) {
- if (has.call(options, key)) {
- fn[key] = options[key];
- }
- }
- return fn;
- }
- function nope(options) {
- options.enabled = false;
- options.modify = modify;
- options.set = set;
- options.use = use;
- return introduce(function diagnopes() {
- return false;
- }, options);
- }
- function yep(options) {
-
- function diagnostics() {
- var args = Array.prototype.slice.call(arguments, 0);
- write.call(write, options, process(args, options));
- return true;
- }
- options.enabled = true;
- options.modify = modify;
- options.set = set;
- options.use = use;
- return introduce(diagnostics, options);
- }
- /**
- * Simple helper function to introduce various of helper methods to our given
- * diagnostics function.
- *
- * @param {Function} diagnostics The diagnostics function.
- * @returns {Function} diagnostics
- * @public
- */
- module.exports = function create(diagnostics) {
- diagnostics.introduce = introduce;
- diagnostics.enabled = enabled;
- diagnostics.process = process;
- diagnostics.modify = modify;
- diagnostics.write = write;
- diagnostics.nope = nope;
- diagnostics.yep = yep;
- diagnostics.set = set;
- diagnostics.use = use;
- return diagnostics;
- }
|