Ei kuvausta

index.js 1.6KB

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