Skip to content

Commit f068788

Browse files
BridgeARcodebytere
authored andcommitted
util: add colors to debuglog()
This adds colors to the passed through arguments in case the stream supports colors. The PID will also be highlighted. PR-URL: #30930 Reviewed-By: James M Snell <[email protected]>
1 parent bc4cbe3 commit f068788

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

‎lib/internal/util/debuglog.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const{ format }=require('internal/util/inspect');
3+
const{inspect,format, formatWithOptions}=require('internal/util/inspect');
44

55
const{ RegExp }=primordials;
66

@@ -40,8 +40,10 @@ function debuglogImpl(set){
4040
constpid=process.pid;
4141
emitWarningIfNeeded(set);
4242
debugs[set]=functiondebug(...args){
43-
constmsg=format(...args);
44-
process.stderr.write(format('%s %d: %s\n',set,pid,msg));
43+
constcolors=process.stderr.hasColors&&process.stderr.hasColors();
44+
constmsg=formatWithOptions({ colors }, ...args);
45+
constcoloredPID=inspect(pid,{ colors });
46+
process.stderr.write(format('%s %s: %s\n',set,coloredPID,msg));
4547
};
4648
}else{
4749
debugs[set]=null;

‎test/sequential/test-util-debug.js‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323
constcommon=require('../common');
2424
constassert=require('assert');
25+
constutil=require('util');
2526

2627
const[,,modeArgv,sectionArgv]=process.argv;
2728

@@ -54,19 +55,36 @@ function parent(){
5455
test('*-test',true,'abc-test');
5556
}
5657

57-
functiontest(environ,shouldWrite,section){
58+
functiontest(environ,shouldWrite,section,forceColors=false){
5859
letexpectErr='';
5960
constexpectOut='ok\n';
6061

6162
constspawn=require('child_process').spawn;
6263
constchild=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+
})
6468
});
6569

6670
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+
constaddCodes=(arr)=>[`\x1B[${arr[0]}m`,`\x1B[${arr[1]}m`];
74+
constnum=addCodes(colors[styles.number]);
75+
conststr=addCodes(colors[styles.string]);
76+
constregexp=addCodes(colors[styles.regexp]);
77+
conststart=`${section.toUpperCase()}${num[0]}${child.pid}${num[1]}`;
78+
constdebugging=`${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+
conststart=`${section.toUpperCase()}${child.pid}`;
84+
expectErr=
85+
`${start}: this{is: 'a'} /debugging/\n`+
86+
`${start}: num=1 str=a obj={"foo":"bar"}\n`;
87+
}
7088
}
7189

7290
leterr='';
@@ -85,12 +103,20 @@ function test(environ, shouldWrite, section){
85103
assert(!c);
86104
assert.strictEqual(err,expectErr);
87105
assert.strictEqual(out,expectOut);
106+
// Run the test again, this time with colors enabled.
107+
if(!forceColors){
108+
test(environ,shouldWrite,section,true);
109+
}
88110
}));
89111
}
90112

91113

92114
functionchild(section){
93-
constutil=require('util');
115+
consttty=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+
});
94120
constdebug=util.debuglog(section);
95121
debug('this',{is: 'a'},/debugging/);
96122
debug('num=%d str=%s obj=%j',1,'a',{foo: 'bar'});

0 commit comments

Comments
(0)