File
Blob: src/node/internal/debuglog.ts
| 1 | // Copyright (c) 2017-2023 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | // |
| 5 | // Copyright Joyent, Inc. and other Node contributors. |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | // copy of this software and associated documentation files (the |
| 9 | // "Software"), to deal in the Software without restriction, including |
| 10 | // without limitation the rights to use, copy, modify, merge, publish, |
| 11 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 12 | // persons to whom the Software is furnished to do so, subject to the |
| 13 | // following conditions: |
| 14 | // |
| 15 | // The above copyright notice and this permission notice shall be included |
| 16 | // in all copies or substantial portions of the Software. |
| 17 | // |
| 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 19 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 21 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 22 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 23 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 24 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | /* TODO: the following is adopted code, enabling linting one day */ |
| 26 | /* eslint-disable */ |
| 27 | |
| 28 | import { format, formatWithOptions } from 'node-internal:internal_inspect'; |
| 29 | |
| 30 | let debugImpls: object = {}; |
| 31 | |
| 32 | function debuglogImpl(set: string) { |
| 33 | if ((debugImpls as any)[set] === undefined) { |
| 34 | (debugImpls as any)[set] = function debug(...args: any[]) { |
| 35 | const msg = formatWithOptions({}, ...args); |
| 36 | console.log(format('%s: %s\n', set, msg)); |
| 37 | }; |
| 38 | } |
| 39 | return (debugImpls as any)[set]; |
| 40 | } |
| 41 | |
| 42 | // In Node.js' implementation, debuglog availability is determined by the NODE_DEBUG |
| 43 | // environment variable. However, we don't have access to the environment variables |
| 44 | // in the same way. Instead, we'll just always enable debuglog on the requested sets. |
| 45 | export function debuglog( |
| 46 | set: string, |
| 47 | cb?: (debug: (...args: any[]) => void) => void |
| 48 | ): any { |
| 49 | function init() { |
| 50 | set = set.toUpperCase(); |
| 51 | } |
| 52 | let debug = (...args: any[]): void => { |
| 53 | init(); |
| 54 | debug = debuglogImpl(set); |
| 55 | if (typeof cb === 'function') { |
| 56 | cb(debug); |
| 57 | } |
| 58 | switch (args.length) { |
| 59 | case 1: |
| 60 | return debug(args[0]); |
| 61 | case 2: |
| 62 | return debug(args[0], args[1]); |
| 63 | default: |
| 64 | return debug(...args); |
| 65 | } |
| 66 | }; |
| 67 | const logger = (...args: any[]) => { |
| 68 | switch (args.length) { |
| 69 | case 1: |
| 70 | return debug(args[0]); |
| 71 | case 2: |
| 72 | return debug(args[0], args[1]); |
| 73 | default: |
| 74 | return debug(...args); |
| 75 | } |
| 76 | }; |
| 77 | Object.defineProperty(logger, 'enabled', { |
| 78 | get() { |
| 79 | return true; |
| 80 | }, |
| 81 | configurable: true, |
| 82 | enumerable: true, |
| 83 | }); |
| 84 | return logger; |
| 85 | } |