| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - const { format } = require('internal/util/inspect'); | ||
| 3 | + const { inspect, format, formatWithOptions } = require('internal/util/inspect'); | ||
| 4 | 4 | ||
| 5 | 5 | // `debugs` is deliberately initialized to undefined so any call to | |
| 6 | 6 | // debuglog() before initializeDebugEnv() is called will throw. | |
@@ -38,8 +38,10 @@ function debuglogImpl(set) { | |||
| 38 | 38 | const pid = process.pid; | |
| 39 | 39 | emitWarningIfNeeded(set); | |
| 40 | 40 | debugs[set] = function debug(...args) { | |
| 41 | - const msg = format(...args); | ||
| 42 | - process.stderr.write(format('%s %d: %s\n', set, pid, msg)); | ||
| 41 | + const colors = process.stderr.hasColors && process.stderr.hasColors(); | ||
| 42 | + const msg = formatWithOptions({ colors }, ...args); | ||
| 43 | + const coloredPID = inspect(pid, { colors }); | ||
| 44 | + process.stderr.write(format('%s %s: %s\n', set, coloredPID, msg)); | ||
| 43 | 45 | }; | |
| 44 | 46 | } else { | |
| 45 | 47 | debugs[set] = null; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,7 @@ | |||
| 22 | 22 | 'use strict'; | |
| 23 | 23 | const common = require('../common'); | |
| 24 | 24 | const assert = require('assert'); | |
| 25 | + const util = require('util'); | ||
| 25 | 26 | ||
| 26 | 27 | const [, , modeArgv, sectionArgv] = process.argv; | |
| 27 | 28 | ||
@@ -54,19 +55,36 @@ function parent() { | |||
| 54 | 55 | test('*-test', true, 'abc-test'); | |
| 55 | 56 | } | |
| 56 | 57 | ||
| 57 | - function test(environ, shouldWrite, section) { | ||
| 58 | + function test(environ, shouldWrite, section, forceColors = false) { | ||
| 58 | 59 | let expectErr = ''; | |
| 59 | 60 | const expectOut = 'ok\n'; | |
| 60 | 61 | ||
| 61 | 62 | const spawn = require('child_process').spawn; | |
| 62 | 63 | const child = spawn(process.execPath, [__filename, 'child', section], { | |
| 63 | - env: Object.assign(process.env, { NODE_DEBUG: environ }) | ||
| 64 | + env: Object.assign(process.env, { | ||
| 65 | + NODE_DEBUG: environ, | ||
| 66 | + FORCE_COLOR: forceColors ? 'true' : 'false' | ||
| 67 | + }) | ||
| 64 | 68 | }); | |
| 65 | 69 | ||
| 66 | 70 | if (shouldWrite) { | |
| 67 | - expectErr = | ||
| 68 | - `${section.toUpperCase()} ${child.pid}: this { is: 'a' } /debugging/\n${ | ||
| 69 | - section.toUpperCase()} ${child.pid}: num=1 str=a obj={"foo":"bar"}\n`; | ||
| 71 | + if (forceColors) { | ||
| 72 | + const { colors, styles } = util.inspect; | ||
| 73 | + const addCodes = (arr) => [`\x1B[${arr[0]}m`, `\x1B[${arr[1]}m`]; | ||
| 74 | + const num = addCodes(colors[styles.number]); | ||
| 75 | + const str = addCodes(colors[styles.string]); | ||
| 76 | + const regexp = addCodes(colors[styles.regexp]); | ||
| 77 | + const start = `${section.toUpperCase()} ${num[0]}${child.pid}${num[1]}`; | ||
| 78 | + const debugging = `${regexp[0]}/debugging/${regexp[1]}`; | ||
| 79 | + expectErr = | ||
| 80 | + `${start}: this { is: ${str[0]}'a'${str[1]} } ${debugging}\n` + | ||
| 81 | + `${start}: num=1 str=a obj={"foo":"bar"}\n`; | ||
| 82 | + } else { | ||
| 83 | + const start = `${section.toUpperCase()} ${child.pid}`; | ||
| 84 | + expectErr = | ||
| 85 | + `${start}: this { is: 'a' } /debugging/\n` + | ||
| 86 | + `${start}: num=1 str=a obj={"foo":"bar"}\n`; | ||
| 87 | + } | ||
| 70 | 88 | } | |
| 71 | 89 | ||
| 72 | 90 | let err = ''; | |
@@ -85,12 +103,20 @@ function test(environ, shouldWrite, section) { | |||
| 85 | 103 | assert(!c); | |
| 86 | 104 | assert.strictEqual(err, expectErr); | |
| 87 | 105 | assert.strictEqual(out, expectOut); | |
| 106 | + // Run the test again, this time with colors enabled. | ||
| 107 | + if (!forceColors) { | ||
| 108 | + test(environ, shouldWrite, section, true); | ||
| 109 | + } | ||
| 88 | 110 | })); | |
| 89 | 111 | } | |
| 90 | 112 | ||
| 91 | 113 | ||
| 92 | 114 | function child(section) { | |
| 93 | - const util = require('util'); | ||
| 115 | + const tty = require('tty'); | ||
| 116 | + // Make sure we check for colors, no matter of the stream's default. | ||
| 117 | + Object.defineProperty(process.stderr, 'hasColors', { | ||
| 118 | + value: tty.WriteStream.prototype.hasColors | ||
| 119 | + }); | ||
| 94 | 120 | const debug = util.debuglog(section); | |
| 95 | 121 | debug('this', { is: 'a' }, /debugging/); | |
| 96 | 122 | debug('num=%d str=%s obj=%j', 1, 'a', { foo: 'bar' }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments