1 |
|
2 | var util = require("util")
|
3 | var assert = require("assert")
|
4 | function now() { return new Date().getTime() }
|
5 |
|
6 | var slice = Array.prototype.slice
|
7 | var console
|
8 | var times = {}
|
9 |
|
10 | if (typeof global !== "undefined" && global.console) {
|
11 | console = global.console
|
12 | } else if (typeof window !== "undefined" && window.console) {
|
13 | console = window.console
|
14 | } else {
|
15 | console = {}
|
16 | }
|
17 |
|
18 | var functions = [
|
19 | [log, "log"],
|
20 | [info, "info"],
|
21 | [warn, "warn"],
|
22 | [error, "error"],
|
23 | [time, "time"],
|
24 | [timeEnd, "timeEnd"],
|
25 | [trace, "trace"],
|
26 | [dir, "dir"],
|
27 | [consoleAssert, "assert"]
|
28 | ]
|
29 |
|
30 | for (var i = 0; i < functions.length; i++) {
|
31 | var tuple = functions[i]
|
32 | var f = tuple[0]
|
33 | var name = tuple[1]
|
34 |
|
35 | if (!console[name]) {
|
36 | console[name] = f
|
37 | }
|
38 | }
|
39 |
|
40 | module.exports = console
|
41 |
|
42 | function log() {}
|
43 |
|
44 | function info() {
|
45 | console.log.apply(console, arguments)
|
46 | }
|
47 |
|
48 | function warn() {
|
49 | console.log.apply(console, arguments)
|
50 | }
|
51 |
|
52 | function error() {
|
53 | console.warn.apply(console, arguments)
|
54 | }
|
55 |
|
56 | function time(label) {
|
57 | times[label] = now()
|
58 | }
|
59 |
|
60 | function timeEnd(label) {
|
61 | var time = times[label]
|
62 | if (!time) {
|
63 | throw new Error("No such label: " + label)
|
64 | }
|
65 |
|
66 | delete times[label]
|
67 | var duration = now() - time
|
68 | console.log(label + ": " + duration + "ms")
|
69 | }
|
70 |
|
71 | function trace() {
|
72 | var err = new Error()
|
73 | err.name = "Trace"
|
74 | err.message = util.format.apply(null, arguments)
|
75 | console.error(err.stack)
|
76 | }
|
77 |
|
78 | function dir(object) {
|
79 | console.log(util.inspect(object) + "\n")
|
80 | }
|
81 |
|
82 | function consoleAssert(expression) {
|
83 | if (!expression) {
|
84 | var arr = slice.call(arguments, 1)
|
85 | assert.ok(false, util.format.apply(null, arr))
|
86 | }
|
87 | }
|