import React, { useState } from "react"; import { Form, Col } from 'react-bootstrap'; import * as GQL from "src/core/generated-graphql"; import { StashService } from "src/core/StashService"; function convertTime(logEntry: GQL.LogEntryDataFragment) { function pad(val : number) { var ret = val.toString(); if (val <= 9) { ret = "0" + ret; } return ret; } var date = new Date(logEntry.time); var month = date.getMonth() + 1; var day = date.getDate(); var dateStr = date.getFullYear() + "-" + pad(month) + "-" + pad(day); dateStr += " " + pad(date.getHours()) + ":" + pad(date.getMinutes()) + ":" + pad(date.getSeconds()); return dateStr; } function levelClass(level : string) { return level.toLowerCase().trim(); } interface ILogElementProps { logEntry : LogEntry } const LogElement: React.FC = ({ logEntry }) => { // pad to maximum length of level enum var level = logEntry.level.padEnd(GQL.LogLevel.Progress.length); return ( <> {logEntry.time}  {level}  {logEntry.message}
); } class LogEntry { public time: string; public level: string; public message: string; public id: string; private static nextId: number = 0; public constructor(logEntry: GQL.LogEntryDataFragment) { this.time = convertTime(logEntry); this.level = logEntry.level; this.message = logEntry.message; var id = LogEntry.nextId++; this.id = id.toString(); } } // maximum number of log entries to display. Subsequent entries will truncate // the list, dropping off the oldest entries first. const MAX_LOG_ENTRIES = 200; const logLevels = ["Debug", "Info", "Warning", "Error"]; export const SettingsLogsPanel: React.FC = () => { const { data, error } = StashService.useLoggingSubscribe(); const { data: existingData } = StashService.useLogs(); const [logLevel, setLogLevel] = useState("Info"); const oldData = (existingData?.logs ?? []).map(e => new LogEntry(e)); const newData = (data?.loggingSubscribe ?? []).map(e => new LogEntry(e)); const filteredLogEntries = [...newData.reverse(), ...oldData] .filter(filterByLogLevel).slice(0, MAX_LOG_ENTRIES); const maybeRenderError = error ?
Error connecting to log server: {error.message}
: ''; function filterByLogLevel(logEntry : LogEntry) { if (logLevel === "Debug") return true; var logLevelIndex = logLevels.indexOf(logLevel); var levelIndex = logLevels.indexOf(logEntry.level); return levelIndex >= logLevelIndex; } return ( <>

Logs

Log Level setLogLevel(event.currentTarget.value)} > { logLevels.map(level => ()) }
{maybeRenderError} {filteredLogEntries.map((logEntry) => )}
); };