Skip to content

Proposal: add a console.write method #10119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
samerbuna opened this issue Dec 5, 2016 · 9 comments
Closed

Proposal: add a console.write method #10119

samerbuna opened this issue Dec 5, 2016 · 9 comments
Labels
console Issues and PRs related to the console subsystem. feature request Issues that request new features to be added to Node.js.

Comments

@samerbuna
Copy link

Often, I find myself needing to console.log a string without a new line and I use process.stdout.write for that, but it would be cool if we can just use the global console object for that with all the extra features it supports (like printf, multiple args, etc..), and to make it so that when we have a custom console object, we'll get an output-without-a-new-line method on that as well.

Maybe console.write()? I think that matches the feature on other platforms.

Happy to implement if you think this is a good idea.

@facekapow
Copy link

All the formatting console.log does is available through util.format, so if it's just the formatting with no extra line you want, just do:

const util = require('util');
process.stdout.write(util.format('my specially formatted string with %s', 'stuff'));

If you want the convenience, why not write yourself a function for this?

function writeFormat(...args) {
  process.stdout.write(util.format(...args));
}

Just my 2 cents, but I just don't see why this should be a core thing.

@mscdex
Copy link
Contributor

mscdex commented Dec 5, 2016

Yeah IMHO this is something easily implemented in userland. Besides, I believe the console API is supposed to more or less modeled after the console object in browsers (which do not have a console.write()).

@mscdex mscdex added console Issues and PRs related to the console subsystem. feature request Issues that request new features to be added to Node.js. labels Dec 5, 2016
@brad-decker
Copy link
Contributor

I agree with @mscdex. Extending the console object with a method that doesn't exist in browser seems like something we shouldn't do. Users expect consitant behavior and apis from the console methods.

@Fishrock123
Copy link
Contributor

I believe the console API is supposed to more or less modeled after the console object in browsers (which do not have a console.write()).

Only in a pretty vague way, we don't follow the "spec" and various related behaviours are not actually the same (errors from closed streams etc).

To be clear, this is required if we take #9744 as I stated here: #9470 (comment)

@sam-github
Copy link
Contributor

In a perfect world, console would not be called what it is, so there would be no confusion between browser debug consoles and I/O routines. In that world, node would have some concise I/O routines like other scripting languages do, and distinguish between "newline appending" and "raw" output.

But its not a perfect world, and I'd prefer not to muddy it further, so while I understand the impetus, I don't think we should do this.

-1

@jsinmotion
Copy link

The current implementation of console.log is:

Console.prototype.log = function log(...args) {
  this._stdout.write(`${util.format.apply(null, args)}\n`);
};

Is it safe to assume that console.log will not do any magic in the future? If so, then the userland equivalent would be

function console_log_sans_newline(...args) {
  console._stdout.write(`${util.format.apply(null, args)}`);
};

If that is a bad assumption, where it may be possible for console.log to do something in addition to writing to the underlying _stdout buffer, IMHO there should be an equivalent.

@sam-github
Copy link
Contributor

console.log will not do magic, it is purely a wrapper to do as you show above, format a string, write it to stdout

@tniessen
Copy link
Member

tniessen commented Jun 2, 2017

I think @Fishrock123 has a point, but let's face it, the console API is "a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers" (doc). It was never supposed to be used for serious IO, so maybe we should not try to promote that use. I agree that no console function should cause the process to crash (mainly due to EPIPE), but I disagree that there is a need for a safe write function, at least not within console.

@bnoordhuis
Copy link
Member

I'll close this out. It's clear from the discussion there is no broad support for this feature request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console subsystem. feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

9 participants