UNPKG

1.09 kBJavaScriptView Raw
1/*
2 * https://github.com/appscot/debug-logger
3 */
4
5/**
6 * Hooking into Node.js stdout
7 * Based on: https://gist.github.com/pguillory/729616 by @pguillory
8 *
9 * @param {Object} callback
10 */
11function hook_stream(stream, callback) {
12 var old_write = stream.write;
13
14 stream.write = (function(write) {
15 return function(string, encoding, fd) {
16 write.apply(stream, arguments);
17 callback(string, encoding, fd);
18 };
19 })(stream.write);
20
21 return function() {
22 stream.write = old_write;
23 };
24};
25
26
27module.exports.lastCharacter = '\n';
28
29function getLastCharacter(string, encoding, fd) {
30 module.exports.lastCharacter = string.slice(-1);
31}
32
33var unhook;
34var unhookStderr;
35module.exports.enable = function enable() {
36 if (!unhook) {
37 unhook = hook_stream(process.stdout, getLastCharacter);
38 }
39 if (!unhookStderr) {
40 unhookStderr = hook_stream(process.stderr, getLastCharacter);
41 }
42};
43
44module.exports.disable = function disable() {
45 if (unhook) {
46 unhook();
47 unhook = undefined;
48 }
49 if (unhookStderr) {
50 unhookStderr();
51 unhookStderr = undefined;
52 }
53};