Add logging for various FFI/Termios tasks
This commit is contained in:
parent
1b748ed63e
commit
32e4030a4a
@ -12,7 +12,7 @@ import {
|
|||||||
readKey,
|
readKey,
|
||||||
truncate,
|
truncate,
|
||||||
} from './fns.ts';
|
} from './fns.ts';
|
||||||
import { getRuntime, logToFile } from './runtime.ts';
|
import { getRuntime, log, LogLevel } from './runtime.ts';
|
||||||
import { ITerminalSize, Position } from './types.ts';
|
import { ITerminalSize, Position } from './types.ts';
|
||||||
|
|
||||||
class Editor {
|
class Editor {
|
||||||
@ -282,11 +282,11 @@ class Editor {
|
|||||||
const whitelist = ['\t'];
|
const whitelist = ['\t'];
|
||||||
|
|
||||||
if (shouldFilter && !whitelist.includes(input)) {
|
if (shouldFilter && !whitelist.includes(input)) {
|
||||||
logToFile({
|
log({
|
||||||
'msg': `Ignoring input: ${input}`,
|
'msg': `Ignoring input: ${input}`,
|
||||||
isEscapeSequence,
|
isEscapeSequence,
|
||||||
isCtrl,
|
isCtrl,
|
||||||
});
|
}, LogLevel.Debug);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -412,7 +412,7 @@ class Editor {
|
|||||||
private drawFileRow(y: number): void {
|
private drawFileRow(y: number): void {
|
||||||
const row = this.#document.row(y);
|
const row = this.#document.row(y);
|
||||||
if (row === null) {
|
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);
|
return this.drawPlaceholderRow(y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,21 +14,29 @@ export enum RunTimeType {
|
|||||||
Unknown = 'common',
|
Unknown = 'common',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum LogLevel {
|
||||||
|
Debug = 'Debug',
|
||||||
|
Info = 'Info',
|
||||||
|
Notice = 'Notice',
|
||||||
|
Warning = 'Warning',
|
||||||
|
Error = 'Error',
|
||||||
|
}
|
||||||
|
|
||||||
let scrollRuntime: IRuntime | null = null;
|
let scrollRuntime: IRuntime | null = null;
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Misc runtime functions
|
// Misc runtime functions
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
export function log(s: unknown, level: LogLevel = LogLevel.Notice): void {
|
||||||
* Append information to the scroll.log logfile
|
|
||||||
*/
|
|
||||||
export function logToFile(s: unknown): void {
|
|
||||||
getRuntime().then(({ file }) => {
|
getRuntime().then(({ file }) => {
|
||||||
const raw = typeof s === 'string' ? s : JSON.stringify(s, null, 2);
|
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
|
* Append information to the scroll.err logfile
|
||||||
*/
|
*/
|
||||||
export function logError(s: unknown): void {
|
export function logError(s: unknown): void {
|
||||||
getRuntime().then(({ file }) => {
|
log(s, LogLevel.Error);
|
||||||
const raw = typeof s === 'string' ? s : JSON.stringify(s, null, 2);
|
|
||||||
const output = raw + '\n';
|
|
||||||
|
|
||||||
file.appendFile(SCROLL_ERR_FILE, output).then(noop);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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 STDIN_FILENO = 0;
|
||||||
export const TCSANOW = 0;
|
export const TCSANOW = 0;
|
||||||
@ -33,6 +33,12 @@ class Termios {
|
|||||||
*/
|
*/
|
||||||
#termios: Uint8Array;
|
#termios: Uint8Array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Has the nasty ffi stuff been cleaned up?
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
#cleaned: boolean = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The pointer to the termios struct
|
* The pointer to the termios struct
|
||||||
* @private
|
* @private
|
||||||
@ -52,10 +58,16 @@ class Termios {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cleanup() {
|
cleanup() {
|
||||||
this.#ptr = null;
|
if (!this.#cleaned) {
|
||||||
this.#cookedTermios = new Uint8Array(0);
|
this.#ptr = null;
|
||||||
this.#termios = new Uint8Array(0);
|
this.#cookedTermios = new Uint8Array(0);
|
||||||
this.#ffi.close();
|
this.#termios = new Uint8Array(0);
|
||||||
|
this.#ffi.close();
|
||||||
|
|
||||||
|
this.#cleaned = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
log('Attempting to cleanup Termios class again', LogLevel.Warning);
|
||||||
}
|
}
|
||||||
|
|
||||||
enableRawMode() {
|
enableRawMode() {
|
||||||
@ -91,6 +103,10 @@ class Termios {
|
|||||||
// Don't even bother throwing an error if we try to disable raw mode
|
// 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.
|
// and aren't in raw mode. It just doesn't really matter.
|
||||||
if (!this.#inRawMode) {
|
if (!this.#inRawMode) {
|
||||||
|
log(
|
||||||
|
'Attampting to disable raw mode when not in raw mode',
|
||||||
|
LogLevel.Warning,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,17 +34,19 @@ const cStdLib = Deno.dlopen(
|
|||||||
);
|
);
|
||||||
|
|
||||||
const { tcgetattr, tcsetattr, cfmakeraw } = cStdLib.symbols;
|
const { tcgetattr, tcsetattr, cfmakeraw } = cStdLib.symbols;
|
||||||
|
let closed = false;
|
||||||
const DenoFFI: IFFI = {
|
const DenoFFI: IFFI = {
|
||||||
tcgetattr,
|
tcgetattr,
|
||||||
tcsetattr,
|
tcsetattr,
|
||||||
cfmakeraw,
|
cfmakeraw,
|
||||||
getPointer: Deno.UnsafePointer.of,
|
getPointer: Deno.UnsafePointer.of,
|
||||||
close: () => {
|
close: () => {
|
||||||
try {
|
if (closed === false) {
|
||||||
cStdLib.close();
|
cStdLib.close();
|
||||||
} catch {
|
closed = true;
|
||||||
// The error thrown is annoying, but harmless.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Do nothing if FFI library was already closed
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user