-
Notifications
You must be signed in to change notification settings - Fork 54
/
logtail.js
192 lines (155 loc) · 5.15 KB
/
logtail.js
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
/* Copyright (c) 2012: Daniel Richman. License: GNU GPL 3 */
/* Additional features: Priyesh Patel */
(function () {
var dataelem = "#data";
var pausetoggle = "#pause";
var scrollelems = ["html", "body"];
var url = "log";
var fix_rn = true;
var load = 30 * 1024; /* 30KB */
var poll = 1000; /* 1s */
var kill = false;
var loading = false;
var pause = false;
var reverse = true;
var log_data = "";
var log_file_size = 0;
/* :-( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt */
function parseInt2(value) {
if(!(/^[0-9]+$/.test(value))) throw "Invalid integer " + value;
var v = Number(value);
if (isNaN(v)) throw "Invalid integer " + value;
return v;
}
function get_log() {
if (kill | loading) return;
loading = true;
var range;
var first_load;
var must_get_206;
if (log_file_size === 0) {
/* Get the last 'load' bytes */
range = "-" + load.toString();
first_load = true;
must_get_206 = false;
} else {
/* Get the (log_file_size - 1)th byte, onwards. */
range = (log_file_size - 1).toString() + "-";
first_load = false;
must_get_206 = log_file_size > 1;
}
/* The "log_file_size - 1" deliberately reloads the last byte, which we already
* have. This is to prevent a 416 "Range unsatisfiable" error: a response
* of length 1 tells us that the file hasn't changed yet. A 416 shows that
* the file has been trucnated */
$.ajax(url, {
dataType: "text",
cache: false,
headers: {Range: "bytes=" + range},
success: function (data, s, xhr) {
loading = false;
var content_size;
if (xhr.status === 206) {
var c_r = xhr.getResponseHeader("Content-Range");
if (!c_r)
throw "Server did not respond with a Content-Range";
log_file_size = parseInt2(c_r.split("/")[1]);
content_size = parseInt2(xhr.getResponseHeader("Content-Length"));
} else if (xhr.status === 200) {
if (must_get_206)
throw "Expected 206 Partial Content";
content_size = log_file_size =
parseInt2(xhr.getResponseHeader("Content-Length"));
} else {
throw "Unexpected status " + xhr.status;
}
if (first_load && data.length > load)
throw "Server's response was too long";
var added = false;
if (first_load) {
/* Clip leading part-line if not the whole file */
if (content_size < log_file_size) {
var start = data.indexOf("\n");
log_data = data.substring(start + 1);
} else {
log_data = data;
}
added = true;
} else {
/* Drop the first byte (see above) */
log_data += data.substring(1);
if (log_data.length > load) {
var start = log_data.indexOf("\n", log_data.length - load);
log_data = log_data.substring(start + 1);
}
if (data.length > 1)
added = true;
}
if (added)
show_log(added);
setTimeout(get_log, poll);
},
error: function (xhr, s, t) {
loading = false;
if (xhr.status === 416 || xhr.status == 404) {
/* 416: Requested range not satisfiable: log was truncated. */
/* 404: Retry soon, I guess */
log_file_size = 0;
log_data = "";
show_log();
setTimeout(get_log, poll);
} else {
throw "Unknown AJAX Error (status " + xhr.status + ")";
}
}
});
}
function scroll(where) {
for (var i = 0; i < scrollelems.length; i++) {
var s = $(scrollelems[i]);
if (where === -1)
s.scrollTop(s.height());
else
s.scrollTop(where);
}
}
function show_log() {
if (pause) return;
var t = log_data;
if (reverse) {
var t_a = t.split(/\n/g);
t_a.reverse();
if (t_a[0] == "")
t_a.shift();
t = t_a.join("\n");
}
if (fix_rn)
t = t.replace(/\n/g, "\r\n");
$(dataelem).text(t);
if (!reverse)
scroll(-1);
}
function error(what) {
kill = true;
$(dataelem).text("An error occured :-(.\r\n" +
"Reloading may help; no promises.\r\n" +
what);
scroll(0);
return false;
}
$(document).ready(function () {
window.onerror = error;
/* If URL is /logtail/?noreverse display in chronological order */
var hash = location.search.replace(/^\?/, "");
if (hash == "noreverse")
reverse = false;
/* Add pause toggle */
$(pausetoggle).click(function (e) {
pause = !pause;
$(pausetoggle).text(pause ? "Unpause" : "Pause");
show_log();
e.preventDefault();
});
get_log();
});
})();