-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: print to stdout/stderr directly instead of using console
This patch adds an internal function that prints to stdout or stderr by directly writing to the known file descriptor, and uses it internally in common cases to avoid the overhead of the console implementation. PR-URL: #27320 Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
76af4f0
commit c2a03d5
Showing
6 changed files
with
103 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
|
||
// This implements a light-weight printer that writes to stdout/stderr | ||
// directly to avoid the overhead in the console abstraction. | ||
|
||
const { formatWithOptions } = require('internal/util/inspect'); | ||
const { writeString } = internalBinding('fs'); | ||
const { handleErrorFromBinding } = require('internal/fs/utils'); | ||
const { guessHandleType } = internalBinding('util'); | ||
const { log } = require('internal/console/global'); | ||
|
||
const kStdout = 1; | ||
const kStderr = 2; | ||
const handleType = [undefined, undefined, undefined]; | ||
function getFdType(fd) { | ||
if (handleType[fd] === undefined) { | ||
handleType[fd] = guessHandleType(fd); | ||
} | ||
return handleType[fd]; | ||
} | ||
|
||
function formatAndWrite(fd, obj, ignoreErrors, colors = false) { | ||
const str = `${formatWithOptions({ colors }, obj)}\n`; | ||
const ctx = {}; | ||
writeString(fd, str, null, undefined, undefined, ctx); | ||
if (!ignoreErrors) { | ||
handleErrorFromBinding(ctx); | ||
} | ||
} | ||
|
||
let colors; | ||
function getColors() { | ||
if (colors === undefined) { | ||
colors = require('internal/tty').getColorDepth() > 2; | ||
} | ||
return colors; | ||
} | ||
|
||
// TODO(joyeecheung): replace more internal process._rawDebug() | ||
// and console.log() usage with this if possible. | ||
function print(fd, obj, ignoreErrors = true) { | ||
switch (getFdType(fd)) { | ||
case 'TTY': | ||
formatAndWrite(fd, obj, ignoreErrors, getColors()); | ||
break; | ||
case 'FILE': | ||
formatAndWrite(fd, obj, ignoreErrors); | ||
break; | ||
case 'PIPE': | ||
case 'TCP': | ||
// Fallback to console.log to handle IPC. | ||
if (process.channel && process.channel.fd === fd) { | ||
log(obj); | ||
} else { | ||
formatAndWrite(fd, obj, ignoreErrors); | ||
} | ||
break; | ||
default: | ||
log(obj); | ||
} | ||
} | ||
|
||
module.exports = { | ||
print, | ||
kStderr, | ||
kStdout | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters