| 1 |
/* |
| 2 |
* Copyright (C) 2020 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
import { journal } from "journal"; |
| 8 |
import * as timeformat from "timeformat"; |
| 9 |
|
| 10 |
import React from 'react'; |
| 11 |
import { EmptyStatePanel } from "cockpit-components-empty-state.jsx"; |
| 12 |
import { ExclamationCircleIcon } from '@patternfly/react-icons'; |
| 13 |
import { Breadcrumb, BreadcrumbItem } from "@patternfly/react-core/dist/esm/components/Breadcrumb/index.js"; |
| 14 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 15 |
import { Card, CardBody, CardHeader, CardTitle } from '@patternfly/react-core/dist/esm/components/Card/index.js'; |
| 16 |
import { DescriptionList, DescriptionListDescription, DescriptionListGroup, DescriptionListTerm } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js"; |
| 17 |
import { Page, PageBreadcrumb, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js"; |
| 18 |
import { Gallery, GalleryItem } from "@patternfly/react-core/dist/esm/layouts/Gallery/index.js"; |
| 19 |
import { Title, TitleSizes } from "@patternfly/react-core/dist/esm/components/Title"; |
| 20 |
|
| 21 |
const _ = cockpit.gettext; |
| 22 |
|
| 23 |
const LogDetails = ({ entry }) => { |
| 24 |
const general = Object.keys(entry).filter(k => k !== 'MESSAGE'); |
| 25 |
general.sort(); |
| 26 |
|
| 27 |
const id = entry.UNIT || entry.SYSLOG_IDENTIFIER || ""; |
| 28 |
let service = entry.USER_UNIT || entry.COREDUMP_USER_UNIT || entry._SYSTEMD_USER_UNIT || ""; |
| 29 |
const is_user = !!service; |
| 30 |
service = service || entry.UNIT || entry.COREDUMP_UNIT || entry._SYSTEMD_UNIT || ""; |
| 31 |
|
| 32 |
// Only show redirect for unit types we show |
| 33 |
if (["service", "target", "socket", "timer", "path"].indexOf(service.split(".").slice(-1)[0]) === -1) |
| 34 |
service = undefined; |
| 35 |
|
| 36 |
const actions = service && ( |
| 37 |
<Button variant="link" onClick={() => cockpit.jump("/system/services#/" + service + (is_user ? "?owner=user" : "")) }> |
| 38 |
{cockpit.format(_("Go to $0"), service)} |
| 39 |
</Button> |
| 40 |
); |
| 41 |
|
| 42 |
return ( |
| 43 |
<GalleryItem> |
| 44 |
<Card isPlain> |
| 45 |
<CardHeader actions={{ actions }}> |
| 46 |
<Title id="entry-heading" headingLevel="h2" size={TitleSizes['3xl']}> |
| 47 |
{id} |
| 48 |
</Title> |
| 49 |
</CardHeader> |
| 50 |
<CardTitle>{journal.printable(entry.MESSAGE, "MESSAGE")}</CardTitle> |
| 51 |
<CardBody> |
| 52 |
<DescriptionList className="pf-m-horizontal-on-sm"> |
| 53 |
{ general.map(key => |
| 54 |
<DescriptionListGroup key={key}> |
| 55 |
<DescriptionListTerm>{key}</DescriptionListTerm> |
| 56 |
<DescriptionListDescription data-label={key}>{journal.printable(entry[key], key)}</DescriptionListDescription> |
| 57 |
</DescriptionListGroup> |
| 58 |
)} |
| 59 |
</DescriptionList> |
| 60 |
</CardBody> |
| 61 |
</Card> |
| 62 |
</GalleryItem> |
| 63 |
); |
| 64 |
}; |
| 65 |
|
| 66 |
export class LogEntry extends React.Component { |
| 67 |
constructor(props) { |
| 68 |
super(props); |
| 69 |
this.state = { |
| 70 |
error: "", |
| 71 |
entry: null, |
| 72 |
loading: true, |
| 73 |
}; |
| 74 |
|
| 75 |
this.goHome = this.goHome.bind(this); |
| 76 |
} |
| 77 |
|
| 78 |
componentDidMount() { |
| 79 |
const cursor = cockpit.location.path[0]; |
| 80 |
journal.journalctl({ cursor, count: 1, follow: false }) |
| 81 |
.then(entries => { |
| 82 |
if (entries.length >= 1 && entries[0].__CURSOR == cursor) { |
| 83 |
this.setState({ entry: entries[0], loading: false, error: "" }); |
| 84 |
} else |
| 85 |
this.setState({ entry: null, loading: false, error: _("Journal entry not found") }); |
| 86 |
}) |
| 87 |
.catch(error => this.setState({ entry: null, loading: false, error })); |
| 88 |
} |
| 89 |
|
| 90 |
goHome(ev) { |
| 91 |
ev.preventDefault(); |
| 92 |
let parent_options = {}; |
| 93 |
if (cockpit.location.options.parent_options) |
| 94 |
parent_options = JSON.parse(cockpit.location.options.parent_options); |
| 95 |
cockpit.location.go('/', parent_options); |
| 96 |
} |
| 97 |
|
| 98 |
render() { |
| 99 |
let breadcrumb = _("Journal entry"); |
| 100 |
let content = null; |
| 101 |
|
| 102 |
if (this.state.error) |
| 103 |
content = <EmptyStatePanel icon={ExclamationCircleIcon} title={this.state.error} />; |
| 104 |
else if (this.state.loading) |
| 105 |
content = <EmptyStatePanel loading title={ _("Loading...") } />; |
| 106 |
else if (this.state.entry) { |
| 107 |
const entry = this.state.entry; |
| 108 |
const date = timeformat.dateTimeSeconds(entry.__REALTIME_TIMESTAMP / 1000); |
| 109 |
|
| 110 |
breadcrumb = cockpit.format(_("Entry at $0"), date); |
| 111 |
content = <LogDetails entry={entry} />; |
| 112 |
} |
| 113 |
|
| 114 |
return ( |
| 115 |
<Page id="log-details" className="log-details pf-m-no-sidebar"> |
| 116 |
<PageBreadcrumb hasBodyWrapper={false} stickyOnBreakpoint={{ default: "top" }}> |
| 117 |
<Breadcrumb> |
| 118 |
<BreadcrumbItem onClick={this.goHome} className="pf-v6-c-breadcrumb__link">{_("Logs")}</BreadcrumbItem> |
| 119 |
<BreadcrumbItem isActive> |
| 120 |
{breadcrumb} |
| 121 |
</BreadcrumbItem> |
| 122 |
</Breadcrumb> |
| 123 |
</PageBreadcrumb> |
| 124 |
<PageSection hasBodyWrapper={false}> |
| 125 |
<Gallery hasGutter> |
| 126 |
{content} |
| 127 |
</Gallery> |
| 128 |
</PageSection> |
| 129 |
</Page> |
| 130 |
); |
| 131 |
} |
| 132 |
} |