-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.js
More file actions
193 lines (174 loc) · 5.76 KB
/
snake.js
File metadata and controls
193 lines (174 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var util = require('util'),
StreamBuffer = require('./streambuffer.js').StreamBuffer,
//a variable to hold the stdoutProxy. But I will not perpetuate
//this kind of evil simply because you require this file.
jormungand = false;
function stdoutProxy () {
var old_stdout = process.stdout.write;
var self = this;
StreamBuffer.call(self);
/**
* a function to get at stdout after all the evil that is about to happen
*/
self.stdout = function () {
old_stdout.apply(process.stdout, arguments);
};
/**
* after each event we need to flush everything out to keep the logs clean.
*/
var needLogReset = true;
self.resetLog = function () {
watchingSnakes = [];
needLogReset = true;
self.flush();
};
/**
* adding a snake to the jormungand
* until the next "event" I will log everything to
* this snake as well
*/
var watchingSnakes = [];
self.addSnake = function (snake, noAppend) {
//make sure I don't append a snake twice
if (watchingSnakes.indexOf(snake) < 0) {
//if you really don't want to know about about the back-story
//for this "event", just say so.
if (!noAppend) {
snake.write(self.read());
}
//the "work"
watchingSnakes.push(snake);
//make sure we stop watching so that the logs are clean
if (needLogReset) {
//See the evil below done to process._tickCallback
//I don't need to do anything, _tickCallback will handle
//flushing the log and clearing the watchingSnakes
//I just need to make sure that a tick occurs
process.nextTick(function () {});
needLogReset = false;
}
}
};
/**
* in case you want to stop watching before the end of the event
*/
self.removeSnake = function (snake) {
var i = watchingSnakes.indexOf(snake);
if (i >= 0) {
watchingSnakes.splice(i,1);
}
};
/**
* hook stdout so we can watch what is going on
*/
process.stdout.write = function (chunk) {
old_stdout.apply(process.stdout, arguments);
self.write(chunk);
watchingSnakes.forEach(function (snake) {
snake.write(chunk);
});
if (needLogReset) {
//See the evil below done to process._tickCallback
//I don't need to do anything, _tickCallback will handle
//flushing the log and clearing the watchingSnakes
//I just need to make sure that a tick occurs
process.nextTick(function () {});
needLogReset = false;
}
}
//to undo all the horrible things I have done and
//make all hurt as if it had never been.
var old__tickCallback = process._tickCallback;
self.distroy = function () {
self.resetLog();
self.removeAllListeners();
process._tickCallback = old__tickCallback;
process.stdout.write = old_stdout;
jormungand = false;
};
/**
* do horrible things to process._tickCallback
* this way my log is always clean.
*
* //TODO do I really want to clean before each tick? I am doing this so that
* so that I treat setTimeout, nextTick, and system events the same
* but that may not be the right thing to do.
* //TODO currently I am only cleaning before a group of ticks.
* I think this is adequate. but depending on the answer to the above
* there may be more work involved.
*/
process._tickCallback = function () {
self.resetLog();
old__tickCallback.apply(process, arguments);
};
}
util.inherits(stdoutProxy, StreamBuffer);
/**
* the class to snake logs for a given execution path
*/
function Snake (name, noAppend) {
var self = this;
StreamBuffer.call(self);
self.name = name = name || 'SNAKE';
/**
* to append the log in a callback
*/
self.too = function (noAppend) {
if (jormungand) {
jormungand.addSnake(self, noAppend);
} else {
jormungand = new stdoutProxy();
jormungand.addSnake(self, noAppend);
}
};
/**
* to stop appending the log
*/
self.pause = function (){
jormungand.removeSnake(self);
};
/**
* a function to log out thread for this execution string.
*/
self.out = function out() {
self.stdout('::::::START ' + name + ' LOG\n');
self.stdout(self.read());
self.stdout('::::::END ' + name + ' LOG\n');
};
/**
* maybe you want to write something but not log it?
*/
self.stdout = function () {
jormungand.stdout.apply(this, arguments);
};
/**
* So you now understand what you have wrought? You want out?
* You can stop anytime you want to.
*/
self.killIt = function () {
jormungand.distroy();
};
//well you asked for one, I may as well start logging...
self.too(noAppend);
}
util.inherits(Snake, stdoutProxy);
/**
* export a function so people can use me
*/
exports.snake = function(name, noAppend) {
if (!jormungand) {
jormungand = new stdoutProxy();
}
return new Snake(name, noAppend);
};
/**
* in case you want to output something but don't want it to be snaked
* mostly here in case someone want's to snake the file and not the module
*/
exports.stdout = function () {
if (jormungand) {
jormungand.stdout.apply(this, arguments);
} else {
process.stdout.write.apply(process.stdout.write, arguments);
}
};