Debian Codegraph @

pkg/lib/cockpit-components-logs-panel.jsx

raw
1 /*
2 * Copyright (C) 2017 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import React from "react";
8
9 import { Badge } from "@patternfly/react-core/dist/esm/components/Badge/index.js";
10 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
11 import { Card, CardBody, CardHeader, CardTitle } from '@patternfly/react-core/dist/esm/components/Card/index.js';
12 import { ExclamationTriangleIcon } from '@patternfly/react-icons';
13
14 import { journal } from "journal";
15 import "journal.css";
16 import "cockpit-components-logs-panel.scss";
17
18 const _ = cockpit.gettext;
19
20 /* JournalOutput implements the interface expected by
21 journal.renderer, and also collects the output.
22 */
23
24 export class JournalOutput {
25 constructor(search_options) {
26 this.logs = [];
27 this.reboot_key = 0;
28 this.search_options = search_options || {};
29 }
30
31 onEvent(ev, cursor, full_content) {
32 // only consider primary mouse button for clicks
33 if (ev.type === 'click') {
34 if (ev.button !== 0)
35 return;
36
37 // Ignore if text is being selected - less than 3 characters most likely means misclick
38 const selection = window.getSelection().toString();
39 if (selection && selection.length > 2 && full_content.indexOf(selection) >= 0)
40 return;
41 }
42
43 // only consider enter button for keyboard events
44 if (ev.type === 'KeyDown' && ev.key !== "Enter")
45 return;
46
47 cockpit.jump("system/logs#/" + cursor + "?parent_options=" + JSON.stringify(this.search_options));
48 }
49
50 render_line(ident, prio, message, count, time, entry) {
51 let warning = false;
52
53 if (prio < 4) {
54 warning = true;
55 }
56
57 const full_content = [time, message, ident].join("\n");
58
59 return (
60 <div className="cockpit-logline" role="row" tabIndex={0} key={entry.__CURSOR}
61 data-cursor={entry.__CURSOR}
62 onClick={ev => this.onEvent(ev, entry.__CURSOR, full_content)}
63 onKeyDown={ev => this.onEvent(ev, entry.__CURSOR, full_content)}>
64 <div className="cockpit-log-warning" role="cell">
65 { warning
66 ? <ExclamationTriangleIcon className="ct-icon-exclamation-triangle" />
67 : null
68 }
69 </div>
70 <div className="cockpit-log-time" role="cell">{time}</div>
71 <span className="cockpit-log-message" role="cell">{message}</span>
72 {
73 count > 1
74 ? <div className="cockpit-log-service-container" role="cell">
75 <div className="cockpit-log-service-reduced">{ident}</div>
76 <Badge screenReaderText={_("Occurrences")} isRead key={count}>{count}</Badge>
77 </div>
78 : <div className="cockpit-log-service" role="cell">{ident}</div>
79 }
80 </div>
81 );
82 }
83
84 render_day_header(day) {
85 return <div className="panel-heading" key={day}>{day}</div>;
86 }
87
88 render_reboot_separator() {
89 return (
90 <div className="cockpit-logline" role="row" key={"reboot-" + this.reboot_key++}>
91 <div className="cockpit-log-warning" role="cell" />
92 <span className="cockpit-log-message cockpit-logmsg-reboot" role="cell">{_("Reboot")}</span>
93 </div>
94 );
95 }
96
97 prepend(item) {
98 this.logs.unshift(item);
99 }
100
101 append(item) {
102 this.logs.push(item);
103 }
104
105 remove_first() {
106 this.logs.shift();
107 }
108
109 remove_last() {
110 this.logs.pop();
111 }
112
113 limit(max) {
114 if (this.logs.length > max)
115 this.logs = this.logs.slice(0, max);
116 }
117 }
118
119 export class LogsPanel extends React.Component {
120 constructor() {
121 super();
122 this.state = { logs: [] };
123 }
124
125 componentDidMount() {
126 this.journalctl = journal.journalctl(this.props.match, { count: this.props.max });
127
128 const out = new JournalOutput(this.props.search_options);
129 const render = journal.renderer(out);
130
131 this.journalctl.stream((entries) => {
132 for (let i = 0; i < entries.length; i++)
133 render.prepend(entries[i]);
134 render.prepend_flush();
135 // "max + 1" since there is always a date header and we
136 // want to show "max" entries below it.
137 out.limit(this.props.max + 1);
138 this.setState({ logs: out.logs });
139 });
140 }
141
142 componentWillUnmount() {
143 this.journalctl.stop();
144 }
145
146 render() {
147 const actions = (this.state.logs.length > 0 && this.props.goto_url) && <Button variant="secondary" onClick={e => cockpit.jump(this.props.goto_url)}>{_("View all logs")}</Button>;
148
149 return (
150 <Card isPlain className="cockpit-log-panel">
151 <CardHeader actions={{ actions }}>
152 <CardTitle>{this.props.title}</CardTitle>
153 </CardHeader>
154 <CardBody className={(!this.state.logs.length && this.props.emptyMessage.length) ? "empty-message" : "contains-list"}>
155 { this.state.logs.length ? this.state.logs : this.props.emptyMessage }
156 </CardBody>
157 </Card>
158 );
159 }
160 }
161 LogsPanel.defaultProps = {
162 emptyMessage: [],
163 };