Skip to content

Commit 268dd7f

Browse files
committed
Colorize and preserve newlines in log output
1 parent ddcb636 commit 268dd7f

File tree

4 files changed

+84
-53
lines changed

4 files changed

+84
-53
lines changed

css/style.css

+17-1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,22 @@ canvas {
190190
grid-column: 1 / 3;
191191
grid-row: 3;
192192
}
193+
.output {
194+
border: 1px inset #999;
195+
padding: 2px;
196+
width: calc(100% - 6px);
197+
height: calc(100% - 6px);
198+
overflow: auto;
199+
}
200+
.output .info {
201+
color: green;
202+
}
203+
.output .stdout {
204+
color: black;
205+
}
206+
.output .stderr {
207+
color: red;
208+
}
193209

194210
.emulator_screen_container {
195211
width: 512;
@@ -243,4 +259,4 @@ canvas {
243259
grid-column: 1;
244260
grid-row: 5;
245261
}
246-
}
262+
}

index.html

+27-11
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,37 @@
3131
var cpu_step_interval_id;
3232
var emu_view = "";
3333

34-
compiler.setLogCallback(function (str) {
34+
function escapeHTML(str) {
35+
var escapedHTML = document.createElement("div");
36+
escapedHTML.innerText = str;
37+
return escapedHTML.innerHTML;
38+
}
39+
40+
compiler.setLogCallback(function (str, kind) {
41+
console.log(`log_callback ${kind}`, [str]);
3542
var output = document.getElementById("output");
36-
if (str === null) {
37-
output.value = "";
43+
if (str === null && kind == null) {
44+
output.innerHTML = "";
3845
return;
3946
}
40-
output.value += str + "\n";
41-
output.scrollTop = output.clientHeight;
47+
output.innerHTML +=
48+
"<span class=\"" +
49+
kind +
50+
"\">" +
51+
escapeHTML(str) +
52+
"</span>\n";
53+
output.scrollTop = output.scrollHeight;
4254
});
4355
emulator.setSerialCallback(function (value) {
4456
var output = document.getElementById("output");
4557
if ((value >= 32 && value < 128) || value == 13)
46-
output.value += String.fromCharCode(value);
47-
else output.value += "[" + ("00" + value.toString(16)).slice(-2) + "]";
48-
output.scrollTop = output.clientHeight;
58+
output.innerHTML += escapeHTML(String.fromCharCode(value));
59+
else
60+
output.innerHTML +=
61+
"<u>" +
62+
escapeHTML("[" + ("00" + value.toString(16)).slice(-2) + "]") +
63+
"</u>";
64+
output.scrollTop = output.scrollHeight;
4965
});
5066

5167
function compileCode() {
@@ -846,11 +862,11 @@
846862
</table>
847863
</div>
848864
<div class="console">
849-
<textarea
850-
style="height: 100%; width: 100%"
865+
<pre
851866
id="output"
867+
class="output"
852868
readonly
853-
></textarea>
869+
></pre>
854870
</div>
855871

856872
<div id="infodialog" class="modal">

js/compiler.js

+32-40
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,15 @@ var link_options = [];
1616

1717
var line_nr_regex = /([\w\.]+)[\w\.\:~]*\(([0-9]+)\)/gi;
1818

19-
var buffered_log = "";
20-
function logFunction(str) {
21-
if (/^[^:\s]+:/.test(str)) {
22-
// If the log starts with a label, we can emit the previously buffered one.
23-
flushLog();
24-
buffered_log = str.trim();
25-
} else {
26-
// If the log doesn’t start with a label, it’s a follow-up to the previous log,
27-
// so add it to the buffer.
28-
buffered_log += " " + str.trim();
29-
}
30-
}
31-
32-
function flushLog() {
33-
unbufferedLogFunction(buffered_log);
34-
buffered_log = "";
35-
}
36-
37-
function unbufferedLogFunction(str) {
38-
if (log_callback) log_callback(str);
19+
function logFunction(str, kind) {
20+
if (log_callback) log_callback(str, kind);
3921

4022
if (
41-
str.startsWith("error: ") ||
42-
str.startsWith("ERROR: ") ||
43-
str.startsWith("warning: ")
23+
kind == "stderr" && (
24+
str.startsWith("error: ") ||
25+
str.startsWith("ERROR: ") ||
26+
str.startsWith("warning: ")
27+
)
4428
) {
4529
var type = "error";
4630
if (str.startsWith("warning: ")) type = "warning";
@@ -53,6 +37,18 @@ function unbufferedLogFunction(str) {
5337
}
5438
}
5539

40+
function infoFunction(str) {
41+
logFunction(str, "info");
42+
}
43+
44+
function outFunction(str) {
45+
logFunction(str, "stdout");
46+
}
47+
48+
function errFunction(str) {
49+
logFunction(str, "stderr");
50+
}
51+
5652
export function setLogCallback(callback) {
5753
log_callback = callback;
5854
}
@@ -89,7 +85,7 @@ function trigger() {
8985
}
9086

9187
function startCompile() {
92-
if (log_callback) log_callback(null);
88+
if (log_callback) log_callback(null, null);
9389
error_list = [];
9490
rom_symbols = [];
9591
ram_symbols = [];
@@ -102,7 +98,7 @@ function startCompile() {
10298

10399
function runRgbAsm(targets, obj_files) {
104100
var target = targets.pop();
105-
logFunction("Running: rgbasm " + target + " -o " + target + ".o -Wall");
101+
infoFunction("Running: rgbasm " + target + " -o " + target + ".o -Wall");
106102
createRgbAsm({
107103
arguments: [target, "-o", "output.o", "-Wall"],
108104
preRun: function (m) {
@@ -111,10 +107,9 @@ function runRgbAsm(targets, obj_files) {
111107
FS.writeFile(key, value);
112108
}
113109
},
114-
print: logFunction,
115-
printErr: logFunction,
110+
print: outFunction,
111+
printErr: errFunction,
116112
}).then(function (m) {
117-
flushLog();
118113
if (repeat) {
119114
buildFailed();
120115
return;
@@ -135,17 +130,16 @@ function runRgbAsm(targets, obj_files) {
135130
function runRgbLink(obj_files) {
136131
var args = ["-o", "output.gb", "--map", "output.map"].concat(link_options);
137132
for (var name in obj_files) args.push(name + ".o");
138-
logFunction("Running: rgblink " + args.join(" "));
133+
infoFunction("Running: rgblink " + args.join(" "));
139134
createRgbLink({
140135
arguments: args,
141136
preRun: function (m) {
142137
var FS = m.FS;
143138
for (var name in obj_files) FS.writeFile(name + ".o", obj_files[name]);
144139
},
145-
print: logFunction,
146-
printErr: logFunction,
140+
print: outFunction,
141+
printErr: errFunction,
147142
}).then(function (m) {
148-
flushLog();
149143
if (repeat) {
150144
buildFailed();
151145
return;
@@ -169,17 +163,16 @@ function runRgbLink(obj_files) {
169163
}
170164

171165
function runRgbFix(input_rom_file, map_file) {
172-
logFunction("Running: rgbfix -v output.gb -p 0xff");
166+
infoFunction("Running: rgbfix -v output.gb -p 0xff");
173167
createRgbFix({
174168
arguments: ["-v", "output.gb", "-p", "0xff"],
175169
preRun: function (m) {
176170
var FS = m.FS;
177171
FS.writeFile("output.gb", input_rom_file);
178172
},
179-
print: logFunction,
180-
printErr: logFunction,
173+
print: outFunction,
174+
printErr: errFunction,
181175
}).then(function (m) {
182-
flushLog();
183176
var FS = m.FS;
184177
try {
185178
var rom_file = FS.readFile("output.gb");
@@ -193,7 +186,7 @@ function runRgbFix(input_rom_file, map_file) {
193186
}
194187

195188
function buildFailed() {
196-
logFunction("Build failed");
189+
infoFunction("Build failed");
197190
if (repeat) {
198191
repeat = false;
199192
trigger();
@@ -265,7 +258,7 @@ function buildDone(rom_file, map_file) {
265258
var total = 0x4000;
266259
if (section_type.startsWith("WRAM")) total = 0x1000;
267260
else if (section_type.startsWith("HRAM")) total = 127;
268-
logFunction(
261+
infoFunction(
269262
"Space left: " +
270263
section_type +
271264
"[" +
@@ -278,8 +271,7 @@ function buildDone(rom_file, map_file) {
278271
);
279272
}
280273
}
281-
logFunction("Build done");
282-
flushLog();
274+
infoFunction("Build done");
283275
done_callback(rom_file, start_address, addr_to_line);
284276
}
285277
}

tracker/js/player.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ class Player {
77

88
constructor()
99
{
10-
compiler.setLogCallback(console.log);
10+
compiler.setLogCallback(function (str, kind) {
11+
if (kind == "info")
12+
console.info(str);
13+
else if (kind == "stdout")
14+
console.log(str);
15+
else if (kind == "stderr")
16+
console.error(str);
17+
});
1118
compiler.setLinkOptions(['-t', '-w']);
1219

1320
function getFile(url, name)

0 commit comments

Comments
 (0)