2 * $Id: trace.c,v 1.33 2020/11/23 23:32:43 tom Exp $
4 * trace.c -- implements screen-dump and keystroke-logging
6 * Copyright 2007-2019,2020 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
35 #define myFP dialog_state.trace_output
38 dlg_trace_time(const char *tag)
40 time_t now = time((time_t *) 0);
41 fprintf(myFP, "%s %s", tag, ctime(&now));
45 dlg_trace_msg(const char *fmt, ...)
50 vfprintf(myFP, fmt, ap);
57 dlg_trace_va_msg(const char *fmt, va_list ap)
60 vfprintf(myFP, fmt, ap);
66 dlg_trace_2s(const char *name, const char *value)
74 while (value[right] != '\0') {
78 if ((next = strchr(value, '\n')) != 0) {
79 left = (int) (next - value);
82 left = (int) strlen(value);
87 dlg_trace_msg("#%14s = %.*s\n", name, left, value);
89 dlg_trace_msg("#+%13s%.*s\n", " ", left, value);
95 dlg_trace_2n(const char *name, int value)
97 dlg_trace_msg("#%14s = %d\n", name, value);
101 dlg_trace_win(WINDOW *win)
104 WINDOW *top = wgetparent(win);
106 while (top != 0 && top != stdscr) {
108 top = wgetparent(win);
112 int rc = getmaxy(win);
113 int cc = getmaxx(win);
118 fprintf(myFP, "window %dx%d at %d,%d\n",
119 rc, cc, getbegy(win), getbegx(win));
122 for (j = 0; j < rc; ++j) {
123 fprintf(myFP, "%3d:", j);
124 for (k = 0; k < cc; ++k) {
125 #ifdef USE_WIDE_CURSES
128 ch = mvwinch(win, j, k) & (A_CHARTEXT | A_ALTCHARSET);
129 if (ch & A_ALTCHARSET) {
130 c2 = dlg_asciibox(ch);
134 buffer[0] = (char) ch;
140 if (win_wch(win, &cch) == ERR
141 || (uc = wunctrl((&cch))) == 0
143 || wcwidth(uc[0]) <= 0) {
148 const wchar_t *ucp = uc;
150 memset(&state, 0, sizeof(state));
151 wcsrtombs(buffer, &ucp, sizeof(buffer), &state);
152 k += wcwidth(uc[0]) - 1;
157 ch = mvwinch(win, j, k) & (A_CHARTEXT | A_ALTCHARSET);
158 c2 = dlg_asciibox(ch);
161 } else if (unctrl(ch) == 0 || strlen(unctrl(ch)) > 1) {
164 fputc((int) (ch & 0xff), myFP);
176 dlg_trace_chr(int ch, int fkey)
178 static int last_err = 0;
181 * Do not bother to trace ERR's indefinitely, since those are usually due
182 * to relatively short polling timeouts.
184 if (last_err && !fkey && ch == ERR) {
186 } else if (myFP != 0) {
187 const char *fkey_name = "?";
190 fprintf(myFP, "skipped %d ERR's\n", last_err);
195 if (fkey > KEY_MAX || (fkey_name = keyname(fkey)) == 0) {
196 #define CASE(name) case name: fkey_name = #name; break
197 switch ((DLG_KEYS_ENUM) fkey) {
204 CASE(DLGK_PAGE_FIRST);
205 CASE(DLGK_PAGE_LAST);
206 CASE(DLGK_PAGE_NEXT);
207 CASE(DLGK_PAGE_PREV);
208 CASE(DLGK_ITEM_FIRST);
209 CASE(DLGK_ITEM_LAST);
210 CASE(DLGK_ITEM_NEXT);
211 CASE(DLGK_ITEM_PREV);
212 CASE(DLGK_FIELD_FIRST);
213 CASE(DLGK_FIELD_LAST);
214 CASE(DLGK_FIELD_NEXT);
215 CASE(DLGK_FIELD_PREV);
216 CASE(DLGK_FORM_FIRST);
217 CASE(DLGK_FORM_LAST);
218 CASE(DLGK_FORM_NEXT);
219 CASE(DLGK_FORM_PREV);
221 CASE(DLGK_GRID_DOWN);
222 CASE(DLGK_GRID_LEFT);
223 CASE(DLGK_GRID_RIGHT);
224 CASE(DLGK_DELETE_LEFT);
225 CASE(DLGK_DELETE_RIGHT);
226 CASE(DLGK_DELETE_ALL);
237 } else if (ch == ERR) {
241 fkey_name = unctrl((chtype) ch);
243 fkey_name = "UNKNOWN";
246 fprintf(myFP, "chr %s (ch=%#x, fkey=%d)\n", fkey_name, ch, fkey);
248 fprintf(myFP, "chr %s (ch=%d, fkey=%d)\n", fkey_name, ch, fkey);
255 dlg_trace(const char *fname)
259 myFP = fopen(fname, "a");
261 dlg_trace_time("## opened at");
262 DLG_TRACE(("## dialog %s\n", dialog_version()));
263 DLG_TRACE(("## vile: confmode\n"));
266 } else if (myFP != 0) {
267 dlg_trace_time("## closed at");
274 extern void dlg_trace(const char *);
276 dlg_trace(const char *fname)