feat: add Screeps console monitoring with configurable error handling and shard support #84

Merged
Philipp merged 9 commits from feature/console-monitor into main 2026-05-16 19:44:39 +02:00
2 changed files with 34 additions and 9 deletions
Showing only changes of commit c3d595e2e1 - Show all commits
+1 -1
View File
File diff suppressed because one or more lines are too long
+31 -6
View File
@@ -76,12 +76,37 @@ export function detectWarning(logLines) {
* @returns {string} * @returns {string}
*/ */
export function safeDecode(text) { export function safeDecode(text) {
if (typeof text !== "string" || !text.includes("%")) return text; if (typeof text !== "string") return text;
let result = text;
if (result.includes("%")) {
try { try {
return decodeURIComponent(text); result = decodeURIComponent(result);
} catch (err) { } catch (err) {
return text; // Ignore decoding errors
} }
}
// Screeps console often contains HTML entities
return result
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&quot;/g, '"')
.replace(/&amp;/g, "&");
}
/**
* Outputs text to the action log, splitting by newline to ensure proper display.
*
* @param {string} text
* @param {"info"|"warning"|"error"} level
*/
export function outputMultiline(text, level = "info") {
if (!text) return;
const lines = text.split(/\r?\n/);
lines.forEach((line) => {
if (level === "error") core.error(line);
else if (level === "warning") core.warning(line);
else core.info(line);
});
} }
// ──────────────────────────────────────────────────────────────────────────── // ────────────────────────────────────────────────────────────────────────────
@@ -185,7 +210,7 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
const warnPattern = /<font\s+color=['"](?:orange|yellow)['"]/i; const warnPattern = /<font\s+color=['"](?:orange|yellow)['"]/i;
logLines logLines
.filter((l) => warnPattern.test(l)) .filter((l) => warnPattern.test(l))
.forEach((l) => core.warning(safeDecode(l))); .forEach((l) => outputMultiline(safeDecode(l), "warning"));
} }
// ── Traceback detection in log lines (Screeps sometimes sends errors here) ─ // ── Traceback detection in log lines (Screeps sometimes sends errors here) ─
@@ -200,7 +225,7 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
if (logToFile) { if (logToFile) {
stdoutBuffer.push(...allStdout); stdoutBuffer.push(...allStdout);
} else { } else {
allStdout.forEach((l) => core.info(l)); allStdout.forEach((l) => outputMultiline(l, "info"));
} }
} }
@@ -208,7 +233,7 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
if (errorText) { if (errorText) {
state.sawErrorLog = true; state.sawErrorLog = true;
const decodedError = safeDecode(errorText); const decodedError = safeDecode(errorText);
core.error(decodedError); outputMultiline(decodedError, "error");
if (detectTraceback(decodedError)) { if (detectTraceback(decodedError)) {
state.sawTraceback = true; state.sawTraceback = true;
} }