307 lines
7.8 KiB
C
307 lines
7.8 KiB
C
/*
|
|
* dtconsole.c -- MCDE: the log viewer (t0123)
|
|
*
|
|
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
|
*
|
|
* The reference's window: the logs on the left (the session's own, the
|
|
* system's under /var/log), the messages on the right with a filter above
|
|
* them and Reload. The tail of a log is read through McdeSys
|
|
* (mcde_sys_log_tail) and a selected log is watched through
|
|
* mcde_sys_log_watch: the descriptor becomes readable when the file grows,
|
|
* XtAppAddInput hands that to this program -- no timer, no polling. A log
|
|
* the user may not read is listed and says so; the administrator's path
|
|
* (t0079) opens it.
|
|
*
|
|
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
|
|
*/
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include <X11/Intrinsic.h>
|
|
#include <Xm/DrawingA.h>
|
|
#include <Xm/Form.h>
|
|
#include <Xm/Label.h>
|
|
#include <Xm/List.h>
|
|
#include <Xm/PushB.h>
|
|
#include <Xm/RowColumn.h>
|
|
#include <Xm/ScrolledW.h>
|
|
#include <Xm/Text.h>
|
|
#include <Xm/TextF.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
static XtAppContext app;
|
|
|
|
/* ---- the small helpers ---- */
|
|
|
|
static void
|
|
SetText(Widget w, const char *text)
|
|
{
|
|
XmString s = XmStringCreateLocalized((char *) text);
|
|
|
|
XtVaSetValues(w, XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
}
|
|
|
|
static Widget
|
|
Text(Widget parent, const char *name, const char *text)
|
|
{
|
|
XmString s = XmStringCreateLocalized((char *) text);
|
|
Widget w = XtVaCreateManagedWidget((char *) name,
|
|
xmLabelWidgetClass, parent, XmNlabelString, s,
|
|
XmNalignment, XmALIGNMENT_BEGINNING, NULL);
|
|
|
|
XmStringFree(s);
|
|
return w;
|
|
}
|
|
|
|
static Widget
|
|
Button(Widget parent, const char *label, XtCallbackProc cb, XtPointer data)
|
|
{
|
|
XmString s = XmStringCreateLocalized((char *) label);
|
|
Widget b = XtVaCreateManagedWidget("button", xmPushButtonWidgetClass,
|
|
parent, XmNlabelString, s, NULL);
|
|
|
|
XmStringFree(s);
|
|
if (cb != NULL)
|
|
XtAddCallback(b, XmNactivateCallback, cb, data);
|
|
return b;
|
|
}
|
|
|
|
#define MAX_LOGS 32
|
|
|
|
static Widget shell, logList, text, filterField, status;
|
|
static int watchFd = -1;
|
|
static int chosen = -1;
|
|
static char currentPath[512];
|
|
|
|
static void
|
|
Show(int at)
|
|
{
|
|
static char buf[200000];
|
|
int n;
|
|
|
|
if (at < 0)
|
|
return;
|
|
if (mcde_sys_log_tail(currentPath, 500, buf, sizeof buf) !=
|
|
MCDE_SYS_OK) {
|
|
XmTextSetString(text, (char *) mcde_sys_error());
|
|
SetText(status, mcde_sys_error());
|
|
return;
|
|
}
|
|
n = (int) strlen(buf);
|
|
(void) n;
|
|
XmTextSetString(text, buf);
|
|
{
|
|
char *f = XmTextFieldGetString(filterField);
|
|
|
|
if (f != NULL && f[0] != '\0') {
|
|
/* a filter shows the lines that carry the text */
|
|
char *copy = strdup(buf), *save = NULL, *line,
|
|
*out = calloc(1, strlen(buf) + 64);
|
|
char low[256];
|
|
size_t k;
|
|
|
|
(void)snprintf(low, sizeof low, "%s", f);
|
|
for (k = 0; low[k] != '\0'; k++)
|
|
low[k] = (char) (low[k] >= 'A' && low[k] <= 'Z'
|
|
? low[k] - 'A' + 'a' : low[k]);
|
|
if (copy != NULL && out != NULL) {
|
|
for (line = strtok_r(copy, "\n", &save);
|
|
line != NULL;
|
|
line = strtok_r(NULL, "\n", &save)) {
|
|
char l2[2048];
|
|
size_t j;
|
|
|
|
(void)snprintf(l2, sizeof l2, "%s",
|
|
line);
|
|
for (j = 0; l2[j] != '\0'; j++)
|
|
l2[j] = (char) (l2[j] >= 'A' &&
|
|
l2[j] <= 'Z' ?
|
|
l2[j] - 'A' + 'a' : l2[j]);
|
|
if (strstr(l2, low) != NULL) {
|
|
strcat(out, line);
|
|
strcat(out, "\n");
|
|
}
|
|
}
|
|
XmTextSetString(text, out);
|
|
}
|
|
free(copy);
|
|
free(out);
|
|
}
|
|
if (f != NULL)
|
|
XtFree(f);
|
|
}
|
|
XmTextShowPosition(text, XmTextGetLastPosition(text));
|
|
{
|
|
char *shown = XmTextGetString(text);
|
|
char s[128];
|
|
int lines = 0;
|
|
const char *p;
|
|
|
|
for (p = shown; p != NULL && *p != '\0'; p++)
|
|
if (*p == '\n')
|
|
lines++;
|
|
if (shown != NULL)
|
|
XtFree(shown);
|
|
(void)snprintf(s, sizeof s, "%d lines", lines);
|
|
SetText(status, s);
|
|
}
|
|
}
|
|
|
|
static void OnGrown(XtPointer data, int *src, XtInputId *id);
|
|
|
|
/* the watched log grew: drain what the kernel queued, show the new tail */
|
|
static void
|
|
OnGrown(XtPointer data, int *src, XtInputId *id)
|
|
{
|
|
char drain[4096];
|
|
|
|
(void) data;
|
|
(void) id;
|
|
while (read(*src, drain, sizeof drain) > 0)
|
|
;
|
|
Show(chosen);
|
|
}
|
|
|
|
static void
|
|
Watch(void)
|
|
{
|
|
if (watchFd >= 0) {
|
|
XtRemoveInput(watchFd);
|
|
(void) close(watchFd);
|
|
watchFd = -1;
|
|
}
|
|
watchFd = mcde_sys_log_watch(currentPath);
|
|
if (watchFd >= 0)
|
|
XtAppAddInput(app, watchFd, (XtPointer) XtInputReadMask,
|
|
OnGrown, NULL);
|
|
}
|
|
|
|
static void
|
|
Chosen(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
XmListCallbackStruct *c = (XmListCallbackStruct *) call;
|
|
McdeSysLog logs[MAX_LOGS];
|
|
int n;
|
|
|
|
(void) w;
|
|
(void) data;
|
|
(void) call;
|
|
n = mcde_sys_logs(logs, MAX_LOGS);
|
|
if (c->item_position < 1 || c->item_position > n)
|
|
return;
|
|
chosen = c->item_position - 1;
|
|
(void)snprintf(currentPath, sizeof currentPath, "%s",
|
|
logs[chosen].path);
|
|
Watch();
|
|
Show(chosen);
|
|
}
|
|
|
|
static void
|
|
Reload(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
(void) w;
|
|
(void) data;
|
|
(void) call;
|
|
Show(chosen);
|
|
}
|
|
|
|
static void
|
|
Fill(Widget w)
|
|
{
|
|
McdeSysLog logs[MAX_LOGS];
|
|
int n, i;
|
|
|
|
(void) w;
|
|
XmListDeleteAllItems(logList);
|
|
n = mcde_sys_logs(logs, MAX_LOGS);
|
|
for (i = 0; i < n; i++) {
|
|
char row[256];
|
|
XmString s;
|
|
|
|
(void)snprintf(row, sizeof row, "%s%s", logs[i].name,
|
|
logs[i].readable ? "" : " (not readable)");
|
|
s = XmStringCreateLocalized(row);
|
|
XmListAddItemUnselected(logList, s, 0);
|
|
XmStringFree(s);
|
|
}
|
|
if (n > 0) {
|
|
XmListSelectPos(logList, 1, False);
|
|
chosen = 0;
|
|
(void)snprintf(currentPath, sizeof currentPath, "%s",
|
|
logs[0].path);
|
|
Watch();
|
|
Show(0);
|
|
} else
|
|
SetText(status, mcde_sys_error());
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
Widget form, bar, sw, pane;
|
|
static String fallback[] = {
|
|
"*renderTable: mcde",
|
|
"*XmText*fontList: fixed",
|
|
"*text.columns: 90",
|
|
"*text.rows: 24",
|
|
NULL
|
|
};
|
|
XmString s;
|
|
|
|
XtSetLanguageProc(NULL, NULL, NULL);
|
|
shell = XtVaAppInitialize(&app, "Dtconsole", NULL, 0, &argc, argv,
|
|
(String *) fallback, XmNtitle, "All Messages", NULL);
|
|
form = XtVaCreateManagedWidget("form", xmFormWidgetClass, shell,
|
|
XmNwidth, 820, XmNheight, 500, NULL);
|
|
bar = XtVaCreateManagedWidget("toolbar", xmRowColumnWidgetClass, form,
|
|
XmNorientation, XmHORIZONTAL, XmNspacing, 8, XmNmarginHeight, 4,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM, NULL);
|
|
(void) Button(bar, "Reload", Reload, NULL);
|
|
s = XmStringCreateLocalized((char *) "Filter:");
|
|
XtVaCreateManagedWidget("filterLabel", xmLabelWidgetClass, bar,
|
|
XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
filterField = XtVaCreateManagedWidget("filter",
|
|
xmTextFieldWidgetClass, bar, XmNcolumns, 24, NULL);
|
|
XtAddCallback(filterField, XmNvalueChangedCallback, Reload, NULL);
|
|
|
|
pane = XtVaCreateManagedWidget("pane", xmRowColumnWidgetClass, form,
|
|
XmNorientation, XmHORIZONTAL, XmNspacing, 10, XmNmarginWidth, 8,
|
|
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, bar,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget,
|
|
status = Text(form, "status", ""), XmNbottomOffset, 8, NULL);
|
|
logList = XmCreateScrolledList(pane, "logs", NULL, 0);
|
|
XtVaSetValues(logList, XmNvisibleItemCount, 16, NULL);
|
|
XtVaSetValues(XtParent(logList), XmNwidth, 240, XmNheight, 400,
|
|
NULL);
|
|
XtManageChild(logList);
|
|
XtAddCallback(logList, XmNbrowseSelectionCallback, Chosen, NULL);
|
|
|
|
sw = XtVaCreateManagedWidget("textBox", xmScrolledWindowWidgetClass,
|
|
pane, XmNscrollingPolicy, XmAUTOMATIC, XmNwidth, 540,
|
|
XmNheight, 400, NULL);
|
|
text = XtVaCreateManagedWidget("text", xmTextWidgetClass, sw,
|
|
XmNeditable, False, XmNcursorPositionVisible, False,
|
|
XmNwordWrap, False, NULL);
|
|
|
|
XtVaSetValues(status, XmNbottomAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment,
|
|
XmATTACH_FORM, NULL);
|
|
XtManageChild(form);
|
|
Fill(shell);
|
|
XtRealizeWidget(shell);
|
|
XtAppMainLoop(app);
|
|
return 0;
|
|
}
|