From 3a4fc11948e6a53ba77ec30577f9802253ee9475 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Sat, 27 Jan 2024 16:12:52 +0800 Subject: [PATCH 1/2] chore: upgrade log code style --- log/file_handler.ts | 52 ++++++++++++++++++------------------ log/rotating_file_handler.ts | 22 +++++++-------- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/log/file_handler.ts b/log/file_handler.ts index d34be19b71f2..275c35749e30 100644 --- a/log/file_handler.ts +++ b/log/file_handler.ts @@ -30,33 +30,33 @@ export interface FileHandlerOptions extends BaseHandlerOptions { * This handler requires `--allow-write` permission on the log file. */ export class FileHandler extends BaseHandler { - protected _file: Deno.FsFile | undefined; - protected _buf: Uint8Array = new Uint8Array(PAGE_SIZE); - protected _pointer = 0; - protected _filename: string; - protected _mode: LogMode; - protected _openOptions: Deno.OpenOptions; - protected _encoder: TextEncoder = new TextEncoder(); + protected file: Deno.FsFile | undefined; + protected buf: Uint8Array = new Uint8Array(PAGE_SIZE); + protected pointer = 0; + protected filename: string; + protected mode: LogMode; + protected openOptions: Deno.OpenOptions; + protected encoder: TextEncoder = new TextEncoder(); #unloadCallback = (() => { this.destroy(); }).bind(this); constructor(levelName: LevelName, options: FileHandlerOptions) { super(levelName, options); - this._filename = options.filename; + this.filename = options.filename; // default to append mode, write only - this._mode = options.mode ? options.mode : "a"; - this._openOptions = { - createNew: this._mode === "x", - create: this._mode !== "x", - append: this._mode === "a", - truncate: this._mode !== "a", + this.mode = options.mode ? options.mode : "a"; + this.openOptions = { + createNew: this.mode === "x", + create: this.mode !== "x", + append: this.mode === "a", + truncate: this.mode !== "a", write: true, }; } override setup() { - this._file = Deno.openSync(this._filename, this._openOptions); + this.file = Deno.openSync(this.filename, this.openOptions); this.#resetBuffer(); addEventListener("unload", this.#unloadCallback); @@ -72,20 +72,20 @@ export class FileHandler extends BaseHandler { } override log(msg: string) { - const bytes = this._encoder.encode(msg + "\n"); - if (bytes.byteLength > this._buf.byteLength - this._pointer) { + const bytes = this.encoder.encode(msg + "\n"); + if (bytes.byteLength > this.buf.byteLength - this.pointer) { this.flush(); } - this._buf.set(bytes, this._pointer); - this._pointer += bytes.byteLength; + this.buf.set(bytes, this.pointer); + this.pointer += bytes.byteLength; } flush() { - if (this._pointer > 0 && this._file) { + if (this.pointer > 0 && this.file) { let written = 0; - while (written < this._pointer) { - written += this._file.writeSync( - this._buf.subarray(written, this._pointer), + while (written < this.pointer) { + written += this.file.writeSync( + this.buf.subarray(written, this.pointer), ); } this.#resetBuffer(); @@ -93,13 +93,13 @@ export class FileHandler extends BaseHandler { } #resetBuffer() { - this._pointer = 0; + this.pointer = 0; } override destroy() { this.flush(); - this._file?.close(); - this._file = undefined; + this.file?.close(); + this.file = undefined; removeEventListener("unload", this.#unloadCallback); } } diff --git a/log/rotating_file_handler.ts b/log/rotating_file_handler.ts index 5954604a24aa..83cb446ad6ad 100644 --- a/log/rotating_file_handler.ts +++ b/log/rotating_file_handler.ts @@ -68,35 +68,35 @@ export class RotatingFileHandler extends FileHandler { } super.setup(); - if (this._mode === "w") { + if (this.mode === "w") { // Remove old backups too as it doesn't make sense to start with a clean // log file, but old backups for (let i = 1; i <= this.#maxBackupCount; i++) { try { - Deno.removeSync(this._filename + "." + i); + Deno.removeSync(this.filename + "." + i); } catch (error) { if (!(error instanceof Deno.errors.NotFound)) { throw error; } } } - } else if (this._mode === "x") { + } else if (this.mode === "x") { // Throw if any backups also exist for (let i = 1; i <= this.#maxBackupCount; i++) { - if (existsSync(this._filename + "." + i)) { + if (existsSync(this.filename + "." + i)) { this.destroy(); throw new Deno.errors.AlreadyExists( - "Backup log file " + this._filename + "." + i + " already exists", + "Backup log file " + this.filename + "." + i + " already exists", ); } } } else { - this.#currentFileSize = (Deno.statSync(this._filename)).size; + this.#currentFileSize = (Deno.statSync(this.filename)).size; } } override log(msg: string) { - const msgByteLength = this._encoder.encode(msg).byteLength + 1; + const msgByteLength = this.encoder.encode(msg).byteLength + 1; if (this.#currentFileSize + msgByteLength > this.#maxBytes) { this.rotateLogFiles(); @@ -110,17 +110,17 @@ export class RotatingFileHandler extends FileHandler { rotateLogFiles() { this.flush(); - this._file!.close(); + this.file!.close(); for (let i = this.#maxBackupCount - 1; i >= 0; i--) { - const source = this._filename + (i === 0 ? "" : "." + i); - const dest = this._filename + "." + (i + 1); + const source = this.filename + (i === 0 ? "" : "." + i); + const dest = this.filename + "." + (i + 1); if (existsSync(source)) { Deno.renameSync(source, dest); } } - this._file = Deno.openSync(this._filename, this._openOptions); + this.file = Deno.openSync(this.filename, this.openOptions); } } From 56fcfb1c2572d5e5c55168cf5a341f1823688c0f Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Sat, 27 Jan 2024 16:28:20 +0800 Subject: [PATCH 2/2] chore: upgrade log code style --- log/file_handler.ts | 52 ++++++++++++++++++------------------ log/logger.ts | 12 ++++----- log/rotating_file_handler.ts | 22 +++++++-------- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/log/file_handler.ts b/log/file_handler.ts index 275c35749e30..d34be19b71f2 100644 --- a/log/file_handler.ts +++ b/log/file_handler.ts @@ -30,33 +30,33 @@ export interface FileHandlerOptions extends BaseHandlerOptions { * This handler requires `--allow-write` permission on the log file. */ export class FileHandler extends BaseHandler { - protected file: Deno.FsFile | undefined; - protected buf: Uint8Array = new Uint8Array(PAGE_SIZE); - protected pointer = 0; - protected filename: string; - protected mode: LogMode; - protected openOptions: Deno.OpenOptions; - protected encoder: TextEncoder = new TextEncoder(); + protected _file: Deno.FsFile | undefined; + protected _buf: Uint8Array = new Uint8Array(PAGE_SIZE); + protected _pointer = 0; + protected _filename: string; + protected _mode: LogMode; + protected _openOptions: Deno.OpenOptions; + protected _encoder: TextEncoder = new TextEncoder(); #unloadCallback = (() => { this.destroy(); }).bind(this); constructor(levelName: LevelName, options: FileHandlerOptions) { super(levelName, options); - this.filename = options.filename; + this._filename = options.filename; // default to append mode, write only - this.mode = options.mode ? options.mode : "a"; - this.openOptions = { - createNew: this.mode === "x", - create: this.mode !== "x", - append: this.mode === "a", - truncate: this.mode !== "a", + this._mode = options.mode ? options.mode : "a"; + this._openOptions = { + createNew: this._mode === "x", + create: this._mode !== "x", + append: this._mode === "a", + truncate: this._mode !== "a", write: true, }; } override setup() { - this.file = Deno.openSync(this.filename, this.openOptions); + this._file = Deno.openSync(this._filename, this._openOptions); this.#resetBuffer(); addEventListener("unload", this.#unloadCallback); @@ -72,20 +72,20 @@ export class FileHandler extends BaseHandler { } override log(msg: string) { - const bytes = this.encoder.encode(msg + "\n"); - if (bytes.byteLength > this.buf.byteLength - this.pointer) { + const bytes = this._encoder.encode(msg + "\n"); + if (bytes.byteLength > this._buf.byteLength - this._pointer) { this.flush(); } - this.buf.set(bytes, this.pointer); - this.pointer += bytes.byteLength; + this._buf.set(bytes, this._pointer); + this._pointer += bytes.byteLength; } flush() { - if (this.pointer > 0 && this.file) { + if (this._pointer > 0 && this._file) { let written = 0; - while (written < this.pointer) { - written += this.file.writeSync( - this.buf.subarray(written, this.pointer), + while (written < this._pointer) { + written += this._file.writeSync( + this._buf.subarray(written, this._pointer), ); } this.#resetBuffer(); @@ -93,13 +93,13 @@ export class FileHandler extends BaseHandler { } #resetBuffer() { - this.pointer = 0; + this._pointer = 0; } override destroy() { this.flush(); - this.file?.close(); - this.file = undefined; + this._file?.close(); + this._file = undefined; removeEventListener("unload", this.#unloadCallback); } } diff --git a/log/logger.ts b/log/logger.ts index b6abe61db58d..4ce2c3614fc1 100644 --- a/log/logger.ts +++ b/log/logger.ts @@ -107,7 +107,7 @@ export class Logger { * function, not the function itself, unless the function isn't called, in which * case undefined is returned. All types are coerced to strings for logging. */ - #_log( + #log( level: LogLevel, msg: (T extends GenericFunction ? never : T) | (() => T), ...args: unknown[] @@ -169,7 +169,7 @@ export class Logger { msg: (T extends GenericFunction ? never : T) | (() => T), ...args: unknown[] ): T | undefined { - return this.#_log(LogLevels.DEBUG, msg, ...args); + return this.#log(LogLevels.DEBUG, msg, ...args); } info(msg: () => T, ...args: unknown[]): T | undefined; @@ -178,7 +178,7 @@ export class Logger { msg: (T extends GenericFunction ? never : T) | (() => T), ...args: unknown[] ): T | undefined { - return this.#_log(LogLevels.INFO, msg, ...args); + return this.#log(LogLevels.INFO, msg, ...args); } /** @deprecated (will be removed after 0.214.0) Use {@linkcode warn} instead. */ @@ -198,7 +198,7 @@ export class Logger { msg: (T extends GenericFunction ? never : T) | (() => T), ...args: unknown[] ): T | undefined { - return this.#_log(LogLevels.WARNING, msg, ...args); + return this.#log(LogLevels.WARNING, msg, ...args); } error(msg: () => T, ...args: unknown[]): T | undefined; @@ -207,7 +207,7 @@ export class Logger { msg: (T extends GenericFunction ? never : T) | (() => T), ...args: unknown[] ): T | undefined { - return this.#_log(LogLevels.ERROR, msg, ...args); + return this.#log(LogLevels.ERROR, msg, ...args); } critical(msg: () => T, ...args: unknown[]): T | undefined; @@ -219,6 +219,6 @@ export class Logger { msg: (T extends GenericFunction ? never : T) | (() => T), ...args: unknown[] ): T | undefined { - return this.#_log(LogLevels.CRITICAL, msg, ...args); + return this.#log(LogLevels.CRITICAL, msg, ...args); } } diff --git a/log/rotating_file_handler.ts b/log/rotating_file_handler.ts index 83cb446ad6ad..5954604a24aa 100644 --- a/log/rotating_file_handler.ts +++ b/log/rotating_file_handler.ts @@ -68,35 +68,35 @@ export class RotatingFileHandler extends FileHandler { } super.setup(); - if (this.mode === "w") { + if (this._mode === "w") { // Remove old backups too as it doesn't make sense to start with a clean // log file, but old backups for (let i = 1; i <= this.#maxBackupCount; i++) { try { - Deno.removeSync(this.filename + "." + i); + Deno.removeSync(this._filename + "." + i); } catch (error) { if (!(error instanceof Deno.errors.NotFound)) { throw error; } } } - } else if (this.mode === "x") { + } else if (this._mode === "x") { // Throw if any backups also exist for (let i = 1; i <= this.#maxBackupCount; i++) { - if (existsSync(this.filename + "." + i)) { + if (existsSync(this._filename + "." + i)) { this.destroy(); throw new Deno.errors.AlreadyExists( - "Backup log file " + this.filename + "." + i + " already exists", + "Backup log file " + this._filename + "." + i + " already exists", ); } } } else { - this.#currentFileSize = (Deno.statSync(this.filename)).size; + this.#currentFileSize = (Deno.statSync(this._filename)).size; } } override log(msg: string) { - const msgByteLength = this.encoder.encode(msg).byteLength + 1; + const msgByteLength = this._encoder.encode(msg).byteLength + 1; if (this.#currentFileSize + msgByteLength > this.#maxBytes) { this.rotateLogFiles(); @@ -110,17 +110,17 @@ export class RotatingFileHandler extends FileHandler { rotateLogFiles() { this.flush(); - this.file!.close(); + this._file!.close(); for (let i = this.#maxBackupCount - 1; i >= 0; i--) { - const source = this.filename + (i === 0 ? "" : "." + i); - const dest = this.filename + "." + (i + 1); + const source = this._filename + (i === 0 ? "" : "." + i); + const dest = this._filename + "." + (i + 1); if (existsSync(source)) { Deno.renameSync(source, dest); } } - this.file = Deno.openSync(this.filename, this.openOptions); + this._file = Deno.openSync(this._filename, this._openOptions); } }