UNPKG

1.07 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';
28function getLastCharacter(string, encoding, fd){
29 module.exports.lastCharacter = string.slice(-1);
30}
31
32var unhook;
33var unhookStderr;
34module.exports.enable = function enable(){
35 if(!unhook){
36 unhook = hook_stream(process.stdout, getLastCharacter);
37 }
38 if(!unhookStderr){
39 unhookStderr = hook_stream(process.stderr, getLastCharacter);
40 }
41};
42
43module.exports.disable = function disable(){
44 if(unhook){
45 unhook();
46 unhook = undefined;
47 }
48 if(unhookStderr){
49 unhookStderr();
50 unhookStderr = undefined;
51 }
52};