| 8 | const padZero = (n, length) => n.toString().padStart(length.toString().length, '0') |
| 9 | |
| 10 | class LogFiles { |
| 11 | // Default to an array so we can buffer |
| 12 | // initial writes before we know the cache location |
| 13 | #logStream = [] |
| 14 | |
| 15 | // We cap log files at a certain number of log events per file. |
| 16 | // Note that each log event can write more than one line to the file. |
| 17 | // Then we rotate log files once this number of events is reached |
| 18 | #MAX_LOGS_PER_FILE = null |
| 19 | |
| 20 | // Now that we write logs continuously we need to have a backstop here for infinite loops that still log. |
| 21 | // This is also partially handled by the config.get('max-files') option, but this is a failsafe to prevent runaway log file creation |
| 22 | #MAX_FILES_PER_PROCESS = null |
| 23 | |
| 24 | #fileLogCount = 0 |
| 25 | #totalLogCount = 0 |
| 26 | #path = null |
| 27 | #logsMax = null |
| 28 | #files = [] |
| 29 | #timing = false |
| 30 | |
| 31 | constructor ({ |
| 32 | maxLogsPerFile = 50_000, |
| 33 | maxFilesPerProcess = 5, |
| 34 | } = {}) { |
| 35 | this.#MAX_LOGS_PER_FILE = maxLogsPerFile |
| 36 | this.#MAX_FILES_PER_PROCESS = maxFilesPerProcess |
| 37 | this.on() |
| 38 | } |
| 39 | |
| 40 | on () { |
| 41 | process.on('log', this.#logHandler) |
| 42 | } |
| 43 | |
| 44 | off () { |
| 45 | process.off('log', this.#logHandler) |
| 46 | this.#endStream() |
| 47 | } |
| 48 | |
| 49 | load ({ command, path, logsMax = Infinity, timing } = {}) { |
| 50 | if (['completion'].includes(command)) { |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | // dir is user configurable and is required to exist so this can error if the dir is missing or not configured correctly |
| 55 | this.#path = path |
| 56 | this.#logsMax = logsMax |
| 57 | this.#timing = timing |
| 58 | |
| 59 | // Log stream has already ended |
| 60 | if (!this.#logStream) { |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | log.verbose('logfile', `logs-max:${logsMax} dir:${this.#path}`) |
| 65 | |
| 66 | // Write the contents of our array buffer to our new file stream and set that as the new log logstream for future writes |
| 67 | // if logs max is 0 then the user does not want a log file |
nothing calls this directly
no test coverage detected
searching dependent graphs…