import React, { useEffect, useContext, useRef, useState, ReactElement } from "react"; import { Select, Typography } from "antd"; import * as lib from "@clusterio/lib"; import { useAccount } from "../model/account"; import useLocalStorage from "../util/useLocalStorage"; import ControlContext from "./ControlContext"; const { Paragraph } = Typography; const consoleHeightKey = "instance-console-height"; const defaultConsoleHeight = 300; type Parsed = { format: string; type: string; level: string; time: string; file: string; action: string; message: string; }; function formatParsedOutput(parsed: Parsed) { let time: ReactElement|string = ""; if (parsed.format === "seconds") { time = {parsed.time.padStart(8)} ; } else if (parsed.format === "date") { time = {parsed.time} ; } let info: ReactElement|string = ""; if (parsed.type === "log") { let level: ReactElement|string = parsed.level; if (level === "Script") { level = {level}; } else if (level === "Verbose") { level = {level}; } else if (level === "Info") { level = {level}; } else if (parsed.level === "Warning") { level = {level}; } else if (parsed.level === "Error") { level = {level}; } info = <>{level} {parsed.file}: ; } else if (parsed.type === "action") { info = <>[{parsed.action}] ; } return <>{time}{info}{parsed.message}; } type Info = lib.LogEntry & { parsed?: Parsed; }; /** * Format an ISO 8601 timestamp as local YYYY-MM-DD HH:MM:SS */ function formatTime(timestamp: string) { const date = new Date(timestamp); if (Number.isNaN(date.getTime())) { return timestamp; } const pad = (value: number) => String(value).padStart(2, "0"); return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` + `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`; } function formatLog(info: Info, key: number): ReactElement { let time = {formatTime(info.timestamp)}; let level = {info.level}; let message = info.level === "server" && info.parsed ? formatParsedOutput(info.parsed) : info.message; return {time} [{level}] {message}
; } /** * Test if a log entry survives the console's actions only filter */ function passesActionsOnly(info: Info) { // Clusterio's own entries are bookkeeping, only keep those reporting a problem. if (info.level !== "server") { return lib.levels[info.level] <= lib.levels.warn; } // Factorio's seconds stamped engine log is verbose diagnostics, only keep warnings and errors. if (info.parsed?.format === "seconds") { return info.parsed.level === "Warning" || info.parsed.level === "Error"; } // What is left is the date stamped console log: the actions done to the server and the replies to them. return true; } type LogConsoleProps = { all?: boolean; controller?: boolean; hosts?: number[]; instances?: number[]; maxLevel?: keyof typeof lib.levels; actionsOnly?: boolean; }; export function SelectMaxLogLevel(props: { value: keyof typeof lib.levels, hidden?: (keyof typeof lib.levels)[], onChange: (option: keyof typeof lib.levels) => void }) { return