delete log file when exceeding 200mb

pull/330/head
Abdullah Alfaraj 2023-06-28 13:52:48 +03:00
parent 7bddee230f
commit bfa02a00a7
2 changed files with 34 additions and 3 deletions

View File

@ -2,6 +2,10 @@
// helloHelper2 = require('./helper.js')
// for organizational proposes
// let g_sdapi_path = 'sdapi'
const _log = console.log
const _warn = console.warn
const _error = console.error
let g_version = 'v1.2.6'
let g_sd_url = 'http://127.0.0.1:7860'
let g_online_data_url =
@ -73,11 +77,12 @@ const {
} = require('./typescripts/dist/bundle')
const io = require('./utility/io')
const _log = console.log
const _warn = console.warn
const _error = console.error
const should_log = true
if (should_log) {
setInterval(async () => {
await io.deleteFileIfLargerThan('log.txt', 200)
}, 2 * 60 * 1000)
window.addEventListener('error', (event) => {
const [a, b, c, d, e] = [1, 2, 3, 4, 5]
console.log(`message: ${a}`)

View File

@ -1256,6 +1256,31 @@ async function convertGrayscaleToMonochrome(base64) {
console.warn(e)
}
}
async function deleteFileIfLargerThan(file_name, size_mb = 200) {
// const file = await fs.getEntry('path/to/file.txt')
try {
const plugin_folder = await fs.getDataFolder()
try {
var file = await plugin_folder.getEntry(file_name)
} catch (e) {
_warn(e)
}
if (file) {
const contents = await file.read({ format: storage.formats.binary })
// storage.formats.utf8
const fileSizeInBytes = contents.byteLength
const fileSizeInMegabytes = fileSizeInBytes / (1024 * 1024)
if (fileSizeInMegabytes > size_mb) {
await fs.removeEntry(file)
}
}
} catch (e) {
// console.warn(e)
_warn(e)
}
}
module.exports = {
IO,
snapShotLayerExe,
@ -1281,4 +1306,5 @@ module.exports = {
getUniqueDocumentId,
getImageSize,
convertGrayscaleToMonochrome,
deleteFileIfLargerThan,
}