Skip to content

Commit 8db693a

Browse files
TechNickAIbnoordhuis
authored andcommitted
util: make util.log handle non strings like console.log
util.log will fail on input that does not support .toString(). Have it use console.log() instead. Includes tests for hairy data types. Fixes #5349.
1 parent 69572a3 commit 8db693a

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

lib/util.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,9 @@ function timestamp() {
491491
}
492492

493493

494-
exports.log = function(msg) {
495-
exports.puts(timestamp() + ' - ' + msg.toString());
494+
// log is just a thin wrapper to console.log that prepends a timestamp
495+
exports.log = function() {
496+
console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
496497
};
497498

498499

test/simple/test-util-log.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
23+
var assert = require('assert');
24+
var util = require('util');
25+
26+
assert.ok(process.stdout.writable);
27+
assert.ok(process.stderr.writable);
28+
29+
var stdout_write = global.process.stdout.write;
30+
var strings = [];
31+
global.process.stdout.write = function(string) {
32+
strings.push(string);
33+
};
34+
console._stderr = process.stdout;
35+
36+
var tests = [
37+
{input: 'foo', output: 'foo'},
38+
{input: undefined, output: 'undefined'},
39+
{input: null, output: 'null'},
40+
{input: false, output: 'false'},
41+
{input: 42, output: '42'},
42+
{input: function(){}, output: '[Function]'},
43+
{input: parseInt('not a number', 10), output: 'NaN'},
44+
{input: {answer: 42}, output: '{ answer: 42 }'},
45+
{input: [1,2,3], output: '[ 1, 2, 3 ]'}
46+
];
47+
48+
// test util.log()
49+
tests.forEach(function(test) {
50+
util.log(test.input);
51+
var result = strings.shift().trim(),
52+
re = (/[0-9]{1,2} [A-Z][a-z]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} - (.+)$/),
53+
match = re.exec(result);
54+
assert.ok(match);
55+
assert.equal(match[1], test.output);
56+
});
57+
58+
global.process.stdout.write = stdout_write;

0 commit comments

Comments
 (0)