Add logging for various FFI/Termios tasks

This commit is contained in:
Timothy Warren 2023-11-30 11:46:25 -05:00
parent 1b748ed63e
commit 32e4030a4a
4 changed files with 45 additions and 24 deletions

View File

@ -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);
}

View File

@ -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);
}
/**

View File

@ -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() {
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;
}

View File

@ -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
},
};