index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var chalk = require('chalk');
  2. var util = require('util');
  3. var logTable = require('./log_table');
  4. var countBuffer = {};
  5. function logWithColor(color, args, isError){
  6. var log = util.format.apply(this, args);
  7. if(isError)
  8. console.error(chalk[color](log));
  9. else
  10. console.log(chalk[color](log));
  11. }
  12. module.exports = {
  13. // Writes a message to the console. You may pass as many arguments as
  14. // you'd like, and they will be joined together in a space-delimited line.
  15. // The first argument to log may be a string containing
  16. // printf-like string substitution patterns.
  17. log: function(){
  18. console.log.apply(this, arguments);
  19. },
  20. // Writes a message to the console with blue color
  21. info: function(){
  22. logWithColor('blue', arguments);
  23. },
  24. // Writes a message to the console with yellow color
  25. warn: function(){
  26. logWithColor('yellow', arguments, true);
  27. },
  28. // Writes a message to the console with red color
  29. error: function(){
  30. logWithColor('red', arguments, true);
  31. },
  32. // Writes a message to the console with regular color
  33. debug: function(){
  34. console.log.apply(this, arguments);
  35. },
  36. // Prints an interactive listing of all properties of the object.
  37. dir: function(){
  38. console.dir.apply(this, arguments);
  39. },
  40. // Clears the terminal buffer
  41. clear: function(){
  42. process.stdout.write('\u001B[2J\u001B[0;0f');
  43. },
  44. // Prints a stack trace of JavaScript execution at the point
  45. // where it is called. The stack trace details the functions on the stack,
  46. // as well as the values that were passed as arguments to each function.
  47. trace: function(){
  48. console.trace.apply(this, arguments);
  49. },
  50. // Does nothing if first argument is truly. If first argument is falsy
  51. // it Writes red warning and throws assertion error
  52. assert: function(assertion){
  53. // todo: for now we are cheating, it's just console.erroring and then
  54. // leave console.asset to do it's job. actual todo: print what
  55. // console.assert prints just make first line red
  56. if (!assertion){
  57. logWithColor('red', ['AssertionError: false == true']);
  58. console.assert(assertion);
  59. }
  60. },
  61. // Writes number of times each argument is called with blue color
  62. count: function(toCount){
  63. var toCountString = toCount.toString && toCount.toString(),
  64. log;
  65. if (countBuffer[toCountString] == null){
  66. countBuffer[toCountString] = 0;
  67. }else{
  68. countBuffer[toCountString] += 1;
  69. }
  70. log = toCountString + ': ' + countBuffer[toCountString];
  71. logWithColor('blue', [log]);
  72. },
  73. // Creates a new timer under the given name. Call console.timeEnd(name)
  74. // with the same name to stop the timer and print the time elapsed..
  75. time: function(){
  76. console.time.apply(this, arguments);
  77. },
  78. // Stops a timer created by a call to console.time(name) and writes the time
  79. // elapsed.
  80. timeEnd: function(){
  81. console.timeEnd.apply(this, arguments);
  82. },
  83. // draws a table of elements inside of a 2d array or object
  84. table: function(){
  85. logTable.apply(this, arguments);
  86. }
  87. };