~james-page/ubuntu/precise/nodejs/test-timeout

« back to all changes in this revision

Viewing changes to lib/console.js

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2011-06-30 07:03:44 UTC
  • mfrom: (7.1.13 sid)
  • Revision ID: james.westby@ubuntu.com-20110630070344-5928xvhb3ddw5adb
Tags: 0.4.9-1ubuntu1
* Merge from Debian unstable (LP: #786428). Remaining changes:
  - debian/patches/2007_remove_internet_test.patch: Remove test which requires
    internet connection

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
var writeError = process.binding('stdio').writeError;
 
23
 
 
24
// console object
 
25
var formatRegExp = /%[sdj]/g;
 
26
function format(f) {
 
27
  var util = require('util');
 
28
 
 
29
  if (typeof f !== 'string') {
 
30
    var objects = [];
 
31
    for (var i = 0; i < arguments.length; i++) {
 
32
      objects.push(util.inspect(arguments[i]));
 
33
    }
 
34
    return objects.join(' ');
 
35
  }
 
36
 
 
37
 
 
38
  var i = 1;
 
39
  var args = arguments;
 
40
  var str = String(f).replace(formatRegExp, function(x) {
 
41
    switch (x) {
 
42
      case '%s': return String(args[i++]);
 
43
      case '%d': return Number(args[i++]);
 
44
      case '%j': return JSON.stringify(args[i++]);
 
45
      default:
 
46
        return x;
 
47
    }
 
48
  });
 
49
  for (var len = args.length, x = args[i]; i < len; x = args[++i]) {
 
50
    if (x === null || typeof x !== 'object') {
 
51
      str += ' ' + x;
 
52
    } else {
 
53
      str += ' ' + util.inspect(x);
 
54
    }
 
55
  }
 
56
  return str;
 
57
}
 
58
 
 
59
 
 
60
exports.log = function() {
 
61
  process.stdout.write(format.apply(this, arguments) + '\n');
 
62
};
 
63
 
 
64
 
 
65
exports.info = exports.log;
 
66
 
 
67
 
 
68
exports.warn = function() {
 
69
  writeError(format.apply(this, arguments) + '\n');
 
70
};
 
71
 
 
72
 
 
73
exports.error = exports.warn;
 
74
 
 
75
 
 
76
exports.dir = function(object) {
 
77
  var util = require('util');
 
78
  process.stdout.write(util.inspect(object) + '\n');
 
79
};
 
80
 
 
81
 
 
82
var times = {};
 
83
exports.time = function(label) {
 
84
  times[label] = Date.now();
 
85
};
 
86
 
 
87
 
 
88
exports.timeEnd = function(label) {
 
89
  var duration = Date.now() - times[label];
 
90
  exports.log('%s: %dms', label, duration);
 
91
};
 
92
 
 
93
 
 
94
exports.trace = function(label) {
 
95
  // TODO probably can to do this better with V8's debug object once that is
 
96
  // exposed.
 
97
  var err = new Error;
 
98
  err.name = 'Trace';
 
99
  err.message = label || '';
 
100
  Error.captureStackTrace(err, arguments.callee);
 
101
  console.error(err.stack);
 
102
};
 
103
 
 
104
 
 
105
exports.assert = function(expression) {
 
106
  if (!expression) {
 
107
    var arr = Array.prototype.slice.call(arguments, 1);
 
108
    require('assert').ok(false, format.apply(this, arr));
 
109
  }
 
110
};