Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(log): rework std/log to work without deprecated Deno.Writer #4021

Merged
merged 4 commits into from
Dec 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions log/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { getLevelByName, LevelName, LogLevels } from "./levels.ts";
import type { LogRecord } from "./logger.ts";
import { blue, bold, red, yellow } from "../fmt/colors.ts";
import { existsSync } from "../fs/exists.ts";
import { BufWriterSync } from "../io/buf_writer.ts";
import type { Writer } from "../io/types.d.ts";

const DEFAULT_FORMATTER = "{levelName} {msg}";
const PAGE_SIZE = 4096;
export type FormatterFunction = (logRecord: LogRecord) => string;
export type LogMode = "a" | "w" | "x";

Expand Down Expand Up @@ -108,7 +107,6 @@ export class ConsoleHandler extends BaseHandler {
}

export abstract class WriterHandler extends BaseHandler {
protected _writer!: Writer;
#encoder = new TextEncoder();

abstract override log(msg: string): void;
Expand Down Expand Up @@ -139,7 +137,8 @@ interface FileHandlerOptions extends HandlerOptions {
*/
export class FileHandler extends WriterHandler {
protected _file: Deno.FsFile | undefined;
protected _buf!: BufWriterSync;
protected _buf: Uint8Array = new Uint8Array(PAGE_SIZE);
protected _pointer = 0;
protected _filename: string;
protected _mode: LogMode;
protected _openOptions: Deno.OpenOptions;
Expand All @@ -164,8 +163,7 @@ export class FileHandler extends WriterHandler {

override setup() {
this._file = Deno.openSync(this._filename, this._openOptions);
this._writer = this._file;
this._buf = new BufWriterSync(this._file);
this.#resetBuffer();

addEventListener("unload", this.#unloadCallback);
}
Expand All @@ -180,18 +178,30 @@ export class FileHandler extends WriterHandler {
}

log(msg: string) {
if (this._encoder.encode(msg).byteLength + 1 > this._buf.available()) {
const bytes = this._encoder.encode(msg + "\n");
if (bytes.byteLength > this._buf.byteLength - this._pointer) {
this.flush();
}
this._buf.writeSync(this._encoder.encode(msg + "\n"));
this._buf.set(bytes, this._pointer);
this._pointer += bytes.byteLength;
}

flush() {
if (this._buf?.buffered() > 0) {
this._buf.flush();
if (this._pointer > 0 && this._file) {
let written = 0;
while (written < this._pointer) {
written += this._file.writeSync(
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
this._buf.subarray(written, this._pointer),
);
}
this.#resetBuffer();
}
}

#resetBuffer() {
this._pointer = 0;
}

override destroy() {
this.flush();
this._file?.close();
Expand Down Expand Up @@ -306,7 +316,7 @@ export class RotatingFileHandler extends FileHandler {
}

rotateLogFiles() {
this._buf.flush();
this.flush();
this._file!.close();

for (let i = this.#maxBackupCount - 1; i >= 0; i--) {
Expand All @@ -319,7 +329,5 @@ export class RotatingFileHandler extends FileHandler {
}

this._file = Deno.openSync(this._filename, this._openOptions);
this._writer = this._file;
this._buf = new BufWriterSync(this._file);
}
}