Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dmZhan committed May 9, 2024
1 parent c196229 commit a38c2dd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"afterColon": true,
"mode": "minimum"
}],
"max-len": [2, { "code": 80 }],
"max-len": [2, { "code": 150 }],
"no-continue": 0,
"no-param-reassign": 0,
"no-restricted-syntax": 0,
Expand Down
8 changes: 8 additions & 0 deletions src/transform/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module.exports = {
concatFirstStringElements: concatFirstStringElements,
formatDate: formatDate,
generateFileNameFromTemp: generateFileNameFromTemp,
formatTimeZone: formatTimeZone,
pad: pad,
padString: padString,
Expand Down Expand Up @@ -48,6 +49,13 @@ function formatDate(template, date) {
.replace('{iso}', date.toISOString());
}

function generateFileNameFromTemp(template, date) {
return template
.replace('{y}', String(date.getFullYear()))
.replace('{m}', pad(date.getMonth() + 1))
.replace('{d}', pad(date.getDate()));
}

function formatTimeZone(minutesOffset) {
var m = Math.abs(minutesOffset);
return (minutesOffset >= 0 ? '-' : '+')
Expand Down
32 changes: 23 additions & 9 deletions src/transports/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ var transform = require('../../transform');
var FileRegistry = require('./file').FileRegistry;
var variables = require('./variables');
var electronApi = require('../../electronApi');
var template = require('../../transform/template');

module.exports = fileTransportFactory;

// Shared between multiple file transport instances
var globalRegistry = new FileRegistry();
// 支持的配置文件名
var configFileNames = ['.logrc', '.logrc.json']
var configFileNames = ['.logrc', '.logrc.json'];

function fileTransportFactory(electronLog, customRegistry) {
var pathVariables = variables.getPathVariables(process.platform);
Expand Down Expand Up @@ -114,7 +115,7 @@ function fileTransportFactory(electronLog, customRegistry) {
* @param {PathVariables} vars
*/
function resolvePath(vars) {
getConfigs()
getConfigs();
return path.join(vars.libraryDefaultDir, vars.fileName);
}

Expand Down Expand Up @@ -204,17 +205,30 @@ function getDefaultFileName() {
}
}

function getConfigs() {
const p = configFileNames.find(i => fs.existsSync(path.join(electronApi.getAppPath(), '/', i)))
if(p) {
function getConfigs(message) {
const p = configFileNames.find(i => fs.existsSync(path.join(electronApi.getAppPath(), '/', i)));
if (p) {
const {
segmentation = false,
maxSize = 1024 * 1024,
filePath = null,
fileName = null
} = JSON.parse(path.join(electronApi.getAppPath(), '/', p))
fileName = null,
} = JSON.parse(path.join(electronApi.getAppPath(), '/', p));
// fileName: SMSC-{y}-{m}-{d}
console.log(segmentation, maxSize, filePath, fileName);
if (segmentation && filePath && fileName) {
const existP = path.join(filePath, template.generateFileNameFromTemp(fileName, message.date));
// existP as D:/log/SMSC-2024-1-21
const files = Math.max(...fs.readdirSync(filePath, { encoding: 'utf8' }).filter(i => {
const pp = path.join(filePath, i);
return fs.lstatSync(pp).isFile() && pp.startsWith(existP + '-') && pp.endsWith('.log');
}).map(j => {
const pp = path.join(filePath, j);
return Number(pp.replace(existP + '-', '').replace('.log', ''));
}).filter(i => !Number.isNaN(i)));
console.log(files);
}
} else {
console.log('read package.json')
console.log('read package.json');
}
}
}

0 comments on commit a38c2dd

Please sign in to comment.