debug.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. var assert = require('assert');
  22. var util = require('../../');
  23. if (process.argv[2] === 'child')
  24. child();
  25. else
  26. parent();
  27. function parent() {
  28. test('foo,tud,bar', true);
  29. test('foo,tud', true);
  30. test('tud,bar', true);
  31. test('tud', true);
  32. test('foo,bar', false);
  33. test('', false);
  34. }
  35. function test(environ, shouldWrite) {
  36. var expectErr = '';
  37. if (shouldWrite) {
  38. expectErr = 'TUD %PID%: this { is: \'a\' } /debugging/\n' +
  39. 'TUD %PID%: number=1234 string=asdf obj={"foo":"bar"}\n';
  40. }
  41. var expectOut = 'ok\n';
  42. var didTest = false;
  43. var spawn = require('child_process').spawn;
  44. var child = spawn(process.execPath, [__filename, 'child'], {
  45. env: { NODE_DEBUG: environ }
  46. });
  47. expectErr = expectErr.split('%PID%').join(child.pid);
  48. var err = '';
  49. child.stderr.setEncoding('utf8');
  50. child.stderr.on('data', function(c) {
  51. err += c;
  52. });
  53. var out = '';
  54. child.stdout.setEncoding('utf8');
  55. child.stdout.on('data', function(c) {
  56. out += c;
  57. });
  58. child.on('close', function(c) {
  59. assert(!c);
  60. assert.equal(err, expectErr);
  61. assert.equal(out, expectOut);
  62. didTest = true;
  63. console.log('ok %j %j', environ, shouldWrite);
  64. });
  65. process.on('exit', function() {
  66. assert(didTest);
  67. });
  68. }
  69. function child() {
  70. var debug = util.debuglog('tud');
  71. debug('this', { is: 'a' }, /debugging/);
  72. debug('number=%d string=%s obj=%j', 1234, 'asdf', { foo: 'bar' });
  73. console.log('ok');
  74. }