Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/better logs #20

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@radix-ui/react-tooltip": "^1.0.6",
"@tailwindcss/line-clamp": "^0.4.4",
"@tanstack/react-table": "^8.9.3",
"@tanstack/react-virtual": "^3.1.3",
"@tauri-apps/api": "2.0.0-beta.0",
"@tauri-apps/plugin-app": "2.0.0-alpha.1",
"@tauri-apps/plugin-autostart": "2.0.0-beta.0",
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

172 changes: 89 additions & 83 deletions src/components/AutowareLaunchDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"use client";

import { memo, useCallback, useEffect, useRef, useState } from "react";
import { useVirtualizer } from "@tanstack/react-virtual";
import { invoke } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
import { useAtom } from "jotai";
import { SetStateAction, useAtom } from "jotai";

import { cn } from "@/lib/utils";
import {
Dialog,
DialogContent,
Expand Down Expand Up @@ -36,6 +38,8 @@ import {
} from "./ui/select";
import { Tabs, TabsList, TabsTrigger } from "./ui/tabs";

type SetAtom<Args extends any[], Result> = (...args: Args) => Result;

interface AutowareLaunchDialog extends React.HTMLAttributes<HTMLDivElement> {}

const tabTitles = ["ALL", "INFO", "WARN", "ERROR", "DEBUG", "COMPONENT"];
Expand Down Expand Up @@ -77,44 +81,46 @@ export function AutowareLaunchDialog(props: AutowareLaunchDialog) {
const logs = data.payload as string;

// Helper function to handle log updates using Sets
const handleLogUpdate = (setLogState: any) => {
setLogState((prevArray: string[]) => {
const handleLogUpdate = (
setLogState: SetAtom<[SetStateAction<string[]>], void>
) => {
setLogState((prevArray) => {
// Deep clone the previous array
const clonedPrevArray = JSON.parse(JSON.stringify(prevArray));

const newSet = new Set(clonedPrevArray);
const newSet = new Set<string>(clonedPrevArray);
newSet.add(logs);
if (newSet.size > 100) {
return [...newSet].slice(-50);
if (newSet.size > 10000) {
return [...newSet].slice(-1000);
}
return [...newSet];
});
};

// Check for "ERROR" logs
if (logs.includes("ERROR")) {
console.log("WE HAVE AN ERROR");
// console.log("WE HAVE AN ERROR");
handleLogUpdate(setLaunchLogsAll);
handleLogUpdate(setLaunchLogsError);
}

// Check for "WARN" logs
if (logs.includes("WARN")) {
console.log("WE HAVE A WARNING");
// console.log("WE HAVE A WARNING");
handleLogUpdate(setLaunchLogsAll);
handleLogUpdate(setLaunchLogsWarn);
}

// Check for "DEBUG" logs
if (logs.includes("DEBUG")) {
console.log("WE HAVE A DEBUG LOG");
// console.log("WE HAVE A DEBUG LOG");
handleLogUpdate(setLaunchLogsAll);
handleLogUpdate(setLaunchLogsDebug);
}

// Check for "INFO" logs
if (logs.includes("INFO")) {
console.log("WE HAVE AN INFO LOG");
// console.log("WE HAVE AN INFO LOG");
handleLogUpdate(setLaunchLogsAll);
handleLogUpdate(setLaunchLogsInfo);
}
Expand All @@ -130,8 +136,8 @@ export function AutowareLaunchDialog(props: AutowareLaunchDialog) {
if (index !== -1) {
const updatedLogs = new Set(newLogs[index].logs);
updatedLogs.add(logs);
if (updatedLogs.size > 100) {
newLogs[index].logs = [...updatedLogs].slice(-50);
if (updatedLogs.size > 10000) {
newLogs[index].logs = [...updatedLogs].slice(-1000);
} else {
newLogs[index].logs = [...updatedLogs];
}
Expand Down Expand Up @@ -177,6 +183,47 @@ export function AutowareLaunchDialog(props: AutowareLaunchDialog) {
);
}

function getLogsForCurrentTab() {
switch (currentTab) {
case "all":
return filterLogs(launchLogsAll);
case "info":
return filterLogs(launchLogsInfo);
case "warn":
return filterLogs(launchLogsWarn);
case "error":
return filterLogs(launchLogsError);
case "debug":
return filterLogs(launchLogsDebug);
case "component":
const componentLogs = launchLogsComponent.find(
(logObj) => logObj.name === selectedPackage
);
return componentLogs ? filterLogs(componentLogs.logs) : [];
default:
return [];
}
}
const logsToDisplay = getLogsForCurrentTab();
useEffect(() => {
console.log(logsToDisplay.length);
}, [logsToDisplay.length]);

const rowVirtualizer = useVirtualizer({
count: logsToDisplay.length,
getScrollElement: () => logDivRef.current!,
estimateSize: useCallback(() => 30, []),
overscan: 5,
});

useEffect(() => {
// scroll to the bottom of the logs
logDivRef.current?.scrollTo({
top: logDivRef.current?.scrollHeight,
behavior: "smooth",
});
}, [logsToDisplay]);

return (
<Dialog>
<DialogTrigger>
Expand All @@ -194,10 +241,7 @@ export function AutowareLaunchDialog(props: AutowareLaunchDialog) {
</DialogTrigger>
<DialogContent className="flex h-[720px] max-w-[600px] flex-col items-center">
<DialogTitle>Autoware Launch Logs</DialogTitle>
<div
ref={logDivRef}
className="relative flex h-full w-full flex-col overflow-y-auto rounded-lg"
>
<div className="relative flex h-full w-full flex-col rounded-lg">
<Tabs
defaultValue="all"
className="flex w-full flex-col gap-2"
Expand Down Expand Up @@ -228,79 +272,41 @@ export function AutowareLaunchDialog(props: AutowareLaunchDialog) {
onChange={(e) => setSearchQuery(e.target.value)}
/>
)}
<div className="fixed left-0 top-28 flex max-h-[calc(100%-8rem)] w-full flex-col gap-4 overflow-y-auto break-words p-4">
{currentTab === "all" &&
filterLogs(launchLogsAll).map((log, index) => (
<div
key={index}
className={`${
log.includes("ERROR")
<div
ref={logDivRef}
className="fixed left-0 top-28 flex h-[calc(100%-8rem)] max-h-[calc(100%-8rem)] w-full flex-col gap-4 overflow-y-auto break-words p-4"
>
{rowVirtualizer.getVirtualItems().map((log) => (
<div
key={log.key}
style={{
position: "relative",
top: 0,
left: 0,
width: "100%",
// Adjust height if your log items have variable sizes
height: rowVirtualizer.getTotalSize(),
transform: `translateY(${log.start}px)`,
}}
className={cn(
"break-words p-2 font-mono ",
`${
logsToDisplay[log.index].includes("ERROR")
? "text-red-500"
: log.includes("WARN")
: logsToDisplay[log.index].includes("WARN")
? "text-yellow-500"
: log.includes("DEBUG")
: logsToDisplay[log.index].includes("DEBUG")
? "text-emerald-500"
: log.includes("INFO")
: logsToDisplay[log.index].includes("INFO")
? "text-violet-300"
: ""
}`}
>
{highlightSearchQuery(log, searchQuery)}
</div>
))}
{currentTab === "info" &&
filterLogs(launchLogsInfo).map((log, index) => (
<div key={index} className="">
{highlightSearchQuery(log, searchQuery)}
</div>
))}
{currentTab === "warn" &&
filterLogs(launchLogsWarn).map((log, index) => (
<div key={index} className="text-yellow-500">
{highlightSearchQuery(log, searchQuery)}
</div>
))}
{currentTab === "error" &&
filterLogs(launchLogsError).map((log, index) => (
<div key={index} className="text-red-500">
{highlightSearchQuery(log, searchQuery)}
</div>
))}
{currentTab === "debug" &&
filterLogs(launchLogsDebug).map((log, index) => (
<div key={index} className="text-emerald-500">
{highlightSearchQuery(log, searchQuery)}
</div>
))}
{currentTab === "component" && (
<div className="flex flex-col overflow-y-auto break-words p-4">
{/* Display logs for the selected package */}
{launchLogsComponent
.filter((logObj) => logObj.name === selectedPackage)
.map((logObj, index) => (
<div key={index}>
{logObj.logs.map((log, logIndex) => {
let logClass = "";
if (log.includes("ERROR")) {
logClass = "text-red-500";
} else if (log.includes("WARN")) {
logClass = "text-yellow-500";
} else if (log.includes("DEBUG")) {
logClass = "text-emerald-500";
} else if (log.includes("INFO")) {
logClass = "";
}

return (
<div key={logIndex} className={logClass}>
{log}
</div>
);
})}
</div>
))}
}`
)}
>
{/* Render your log entry here */}
{highlightSearchQuery(logsToDisplay[log.index], searchQuery)}
</div>
)}
))}
</div>
</Tabs>
</div>
Expand Down
14 changes: 10 additions & 4 deletions src/components/tabComponents/Launch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -472,13 +472,17 @@ const Launch = () => {
}
return;
} else if (mapFileAttributesValuesFound.length === 1) {
if (!mapPathContentFiles.some((file) => file.endsWith(".pcd"))) {
if (
!mapPathContentFiles.some((file) => file && file.endsWith(".pcd"))
) {
toast({
variant: "destructive",
title: "Error",
description: `The "pointcloud_map" file was not found in the map_path directory`,
});
} else if (!mapPathContentFiles.some((file) => file.endsWith(".osm"))) {
} else if (
!mapPathContentFiles.some((file) => file && file.endsWith(".osm"))
) {
toast({
variant: "destructive",
title: "Error",
Expand All @@ -493,8 +497,10 @@ const Launch = () => {
}
} else {
if (
mapFileAttributesValues.some((file) => file.endsWith(".osm")) ||
mapFileAttributesValues.some((file) => file.endsWith(".pcd"))
mapFileAttributesValues.some(
(file) => file && file.endsWith(".osm")
) ||
mapFileAttributesValues.some((file) => file && file.endsWith(".pcd"))
) {
toast({
variant: "destructive",
Expand Down