(info: any, callback: () => void)
| 45 | } |
| 46 | |
| 47 | public async log(info: any, callback: () => void) { |
| 48 | const path = getFilePath(this.logDirectory) |
| 49 | |
| 50 | if (this.stream === undefined || this.stream.path !== path) { |
| 51 | if (this.stream !== undefined) { |
| 52 | await promisify(this.stream.end).call(this.stream).catch(error('end')) |
| 53 | await promisify(this.stream.close) |
| 54 | .call(this.stream) |
| 55 | .catch(error('close')) |
| 56 | } |
| 57 | const stream = (this.stream = createWriteStream(path, { flags: 'a' })) |
| 58 | |
| 59 | await new Promise((resolve, reject) => { |
| 60 | stream.on('open', resolve) |
| 61 | stream.on('error', reject) |
| 62 | }).catch(error('stream error')) |
| 63 | |
| 64 | await pruneDirectory(this.logDirectory).catch(error('prune')) |
| 65 | } |
| 66 | |
| 67 | if (this.stream !== undefined) { |
| 68 | await write(this.stream, `${info[MESSAGE]}${EOL}`).catch(error('write')) |
| 69 | this.emit('logged', info) |
| 70 | } |
| 71 | |
| 72 | callback?.() |
| 73 | } |
| 74 | |
| 75 | public close(cb?: () => void) { |
| 76 | this.stream?.end(cb) |
no test coverage detected