From 32e4030a4a7ffaed6f5f1e3b0352b806283696f2 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 30 Nov 2023 11:46:25 -0500 Subject: [PATCH] Add logging for various FFI/Termios tasks --- src/common/editor.ts | 8 ++++---- src/common/runtime.ts | 27 +++++++++++++++------------ src/common/termios.ts | 26 +++++++++++++++++++++----- src/deno/ffi.ts | 8 +++++--- 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/common/editor.ts b/src/common/editor.ts index 1a2c8a3..188ce9f 100644 --- a/src/common/editor.ts +++ b/src/common/editor.ts @@ -12,7 +12,7 @@ import { readKey, truncate, } from './fns.ts'; -import { getRuntime, logToFile } from './runtime.ts'; +import { getRuntime, log, LogLevel } from './runtime.ts'; import { ITerminalSize, Position } from './types.ts'; class Editor { @@ -282,11 +282,11 @@ class Editor { const whitelist = ['\t']; if (shouldFilter && !whitelist.includes(input)) { - logToFile({ + log({ 'msg': `Ignoring input: ${input}`, isEscapeSequence, isCtrl, - }); + }, LogLevel.Debug); return true; } @@ -412,7 +412,7 @@ class Editor { private drawFileRow(y: number): void { const row = this.#document.row(y); if (row === null) { - logToFile(`Warning: trying to draw non-existent row '${y}'`); + log(`Trying to draw non-existent row '${y}'`, LogLevel.Warning); return this.drawPlaceholderRow(y); } diff --git a/src/common/runtime.ts b/src/common/runtime.ts index 0299104..172ac74 100644 --- a/src/common/runtime.ts +++ b/src/common/runtime.ts @@ -14,21 +14,29 @@ export enum RunTimeType { Unknown = 'common', } +export enum LogLevel { + Debug = 'Debug', + Info = 'Info', + Notice = 'Notice', + Warning = 'Warning', + Error = 'Error', +} + let scrollRuntime: IRuntime | null = null; // ---------------------------------------------------------------------------- // Misc runtime functions // ---------------------------------------------------------------------------- -/** - * Append information to the scroll.log logfile - */ -export function logToFile(s: unknown): void { +export function log(s: unknown, level: LogLevel = LogLevel.Notice): void { getRuntime().then(({ file }) => { const raw = typeof s === 'string' ? s : JSON.stringify(s, null, 2); - const output = raw + '\n'; + const output = `${level}: ${raw}\n`; - file.appendFile(SCROLL_LOG_FILE, output).then(noop); + const outputFile = (level === LogLevel.Error) + ? SCROLL_ERR_FILE + : SCROLL_LOG_FILE; + file.appendFile(outputFile, output).then(noop); }); } @@ -36,12 +44,7 @@ export function logToFile(s: unknown): void { * Append information to the scroll.err logfile */ export function logError(s: unknown): void { - getRuntime().then(({ file }) => { - const raw = typeof s === 'string' ? s : JSON.stringify(s, null, 2); - const output = raw + '\n'; - - file.appendFile(SCROLL_ERR_FILE, output).then(noop); - }); + log(s, LogLevel.Error); } /** diff --git a/src/common/termios.ts b/src/common/termios.ts index e6329cd..45c5840 100644 --- a/src/common/termios.ts +++ b/src/common/termios.ts @@ -1,4 +1,4 @@ -import { die, IFFI, importForRuntime } from './runtime.ts'; +import { die, IFFI, importForRuntime, log, LogLevel } from './runtime.ts'; export const STDIN_FILENO = 0; export const TCSANOW = 0; @@ -33,6 +33,12 @@ class Termios { */ #termios: Uint8Array; + /** + * Has the nasty ffi stuff been cleaned up? + * @private + */ + #cleaned: boolean = false; + /** * The pointer to the termios struct * @private @@ -52,10 +58,16 @@ class Termios { } cleanup() { - this.#ptr = null; - this.#cookedTermios = new Uint8Array(0); - this.#termios = new Uint8Array(0); - this.#ffi.close(); + if (!this.#cleaned) { + this.#ptr = null; + this.#cookedTermios = new Uint8Array(0); + this.#termios = new Uint8Array(0); + this.#ffi.close(); + + this.#cleaned = true; + } + + log('Attempting to cleanup Termios class again', LogLevel.Warning); } enableRawMode() { @@ -91,6 +103,10 @@ class Termios { // Don't even bother throwing an error if we try to disable raw mode // and aren't in raw mode. It just doesn't really matter. if (!this.#inRawMode) { + log( + 'Attampting to disable raw mode when not in raw mode', + LogLevel.Warning, + ); return; } diff --git a/src/deno/ffi.ts b/src/deno/ffi.ts index c0fe557..e10530b 100644 --- a/src/deno/ffi.ts +++ b/src/deno/ffi.ts @@ -34,17 +34,19 @@ const cStdLib = Deno.dlopen( ); const { tcgetattr, tcsetattr, cfmakeraw } = cStdLib.symbols; +let closed = false; const DenoFFI: IFFI = { tcgetattr, tcsetattr, cfmakeraw, getPointer: Deno.UnsafePointer.of, close: () => { - try { + if (closed === false) { cStdLib.close(); - } catch { - // The error thrown is annoying, but harmless. + closed = true; } + + // Do nothing if FFI library was already closed }, };