Skip to content

Commit

Permalink
fix(perf): remove glob dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Apr 7, 2024
1 parent 836b800 commit f04de4c
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions lib/utils/log-file.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
const os = require('os')
const { join, dirname, basename } = require('path')
const { format } = require('util')
const { glob } = require('glob')
const { Minipass } = require('minipass')
const fsMiniPass = require('fs-minipass')
const fs = require('fs/promises')
const log = require('./log-shim')
const Display = require('./display')

const padZero = (n, length) => n.toString().padStart(length.toString().length, '0')
const globify = pattern => pattern.split('\\').join('/')

class LogFiles {
// Default to a plain minipass stream so we can buffer
Expand All @@ -33,7 +31,7 @@ class LogFiles {
#logsMax = null
#files = []

constructor ({
constructor({

Check failure on line 34 in lib/utils/log-file.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before function parentheses
maxLogsPerFile = 50_000,
maxFilesPerProcess = 5,
} = {}) {
Expand All @@ -42,7 +40,7 @@ class LogFiles {
this.on()
}

static format (count, level, title, ...args) {
static format(count, level, title, ...args) {

Check failure on line 43 in lib/utils/log-file.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before function parentheses
let prefix = `${count} ${level}`
if (title) {
prefix += ` ${title}`
Expand All @@ -53,21 +51,21 @@ class LogFiles {
.map(Display.clean)
.reduce((lines, line) =>
lines += prefix + (line ? ' ' : '') + line + os.EOL,
''
''

Check failure on line 54 in lib/utils/log-file.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 6 spaces but found 8
)
}

on () {
on() {

Check failure on line 58 in lib/utils/log-file.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before function parentheses
this.#logStream = new Minipass()
process.on('log', this.#logHandler)
}

off () {
off() {

Check failure on line 63 in lib/utils/log-file.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before function parentheses
process.off('log', this.#logHandler)
this.#endStream()
}

load ({ path, logsMax = Infinity } = {}) {
load({ path, logsMax = Infinity } = {}) {

Check failure on line 68 in lib/utils/log-file.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before function parentheses
// dir is user configurable and is required to exist so
// this can error if the dir is missing or not configured correctly
this.#path = path
Expand Down Expand Up @@ -96,19 +94,19 @@ class LogFiles {
return this.#cleanLogs()
}

log (...args) {
log(...args) {

Check failure on line 97 in lib/utils/log-file.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before function parentheses
this.#logHandler(...args)
}

get files () {
get files() {

Check failure on line 101 in lib/utils/log-file.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before function parentheses
return this.#files
}

get #isBuffered () {
get #isBuffered() {

Check failure on line 105 in lib/utils/log-file.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before function parentheses
return this.#logStream instanceof Minipass
}

#endStream (output) {
#endStream(output) {

Check failure on line 109 in lib/utils/log-file.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before function parentheses
if (this.#logStream) {
this.#logStream.end(output)
this.#logStream = null
Expand Down Expand Up @@ -152,16 +150,16 @@ class LogFiles {
}
}

#formatLogItem (...args) {
#formatLogItem(...args) {
this.#fileLogCount += 1
return LogFiles.format(this.#totalLogCount++, ...args)
}

#getLogFilePath (count = '') {
#getLogFilePath(count = '') {
return `${this.#path}debug-${count}.log`
}

#openLogFile () {
#openLogFile() {
// Count in filename will be 0 indexed
const count = this.#files.length

Expand Down Expand Up @@ -190,7 +188,7 @@ class LogFiles {
}
}

async #cleanLogs () {
async #cleanLogs() {
// module to clean out the old log files
// this is a best-effort attempt. if a rm fails, we just
// log a message about it and move on. We do return a
Expand All @@ -199,25 +197,38 @@ class LogFiles {

try {
const logPath = this.#getLogFilePath()
const logGlob = join(dirname(logPath), basename(logPath)
// tell glob to only match digits
.replace(/\d/g, '[0123456789]')
const patternFileName = basename(logPath)
// tell glob to only match digits
.replace(/\d/g, 'd')
// Handle the old (prior to 8.2.0) log file names which did not have a
// counter suffix
.replace(/-\.log$/, '*.log')
)
.replace('-.log', '')

const files = await fs.readdir(dirname(logPath), { withFileTypes: true, recursive: false, encoding: 'utf-8' })
const logFiles = []

for (const file of files) {
if (!file.isFile())
continue

const genericFileName = file.name.replace(/\d/g, 'd')
const filePath = join(file.path, file.name)

// Always ignore the currently written files
const files = await glob(globify(logGlob), { ignore: this.#files.map(globify), silent: true })
const toDelete = files.length - this.#logsMax
if (genericFileName.includes(patternFileName) && genericFileName.endsWith('.log') && !this.#files.includes(filePath)) {
logFiles.push(filePath)
}
}

const toDelete = logFiles.length - this.#logsMax

if (toDelete <= 0) {
return
}

log.silly('logfile', `start cleaning logs, removing ${toDelete} files`)

for (const file of files.slice(0, toDelete)) {
for (const file of logFiles.slice(0, toDelete)) {
try {
await fs.rm(file, { force: true })
} catch (e) {
Expand Down

0 comments on commit f04de4c

Please sign in to comment.