index.js
4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
var fs = require('fs');
var path = require('path');
var os = require('os');
var util = require('util');
var transform = require('../../transform');
var FileRegistry = require('./file').FileRegistry;
var variables = require('./variables');
module.exports = fileTransportFactory;
// Shared between multiple file transport instances
var globalRegistry = new FileRegistry();
function fileTransportFactory(electronLog, customRegistry) {
var pathVariables = variables.getPathVariables(process.platform);
var registry = customRegistry || globalRegistry;
if (registry.listenerCount('error') < 1) {
registry.on('error', function (e, file) {
logConsole('Can\'t write to ' + file, e);
});
}
/* eslint-disable no-multi-spaces */
transport.archiveLog = archiveLog;
transport.depth = 5;
transport.fileName = getDefaultFileName();
transport
.format = '[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}]{scope} {text}';
transport.getFile = getFile;
transport.level = 'silly';
transport.maxSize = 1024 * 1024;
transport.readAllLogs = readAllLogs;
transport.resolvePath = resolvePath;
transport.sync = true;
transport.writeOptions = {
flag: 'a',
mode: 438, // 0666
encoding: 'utf8',
};
transport.inspectOptions = {};
initDeprecated();
return transport;
function transport(message) {
var file = getFile(message);
var needLogRotation = transport.maxSize > 0
&& file.size > transport.maxSize;
if (needLogRotation) {
transport.archiveLog(file);
file.reset();
}
var scopeOptions = electronLog.scope.getOptions();
var inspectOptions = Object.assign(
{ depth: transport.depth },
transport.inspectOptions
);
var content = transform.transform(message, [
transform.removeStyles,
transform.customFormatterFactory(transport.format, false, scopeOptions),
transform.concatFirstStringElements,
transform.toStringFactory(inspectOptions),
]);
file.writeLine(content);
}
function archiveLog(file) {
var oldPath = file.toString();
var inf = path.parse(oldPath);
try {
fs.renameSync(oldPath, path.join(inf.dir, inf.name + '.old' + inf.ext));
} catch (e) {
logConsole('Could not rotate log', e);
var quarterOfMaxSize = Math.round(transport.maxSize / 4);
file.crop(Math.min(quarterOfMaxSize, 256 * 1024));
}
}
function logConsole(message, error) {
var data = ['electron-log.transports.file: ' + message];
if (error) {
data.push(error);
}
electronLog.transports.console({
data: data,
date: new Date(),
level: 'warn',
});
}
function getFile(msg) {
var vars = Object.assign({}, pathVariables, {
fileName: transport.fileName,
});
var filePath = transport.resolvePath(vars, msg);
return registry.provide(filePath, transport.writeOptions, !transport.sync);
}
/**
* @param {PathVariables} vars
*/
function resolvePath(vars) {
return path.join(vars.libraryDefaultDir, vars.fileName);
}
function readAllLogs() {
var vars = Object.assign({}, pathVariables, {
fileName: transport.fileName,
});
var logsPath = path.dirname(transport.resolvePath(vars));
return fs.readdirSync(logsPath)
.map(function (fileName) {
var logPath = path.join(logsPath, fileName);
try {
return {
path: logPath,
lines: fs.readFileSync(logPath, 'utf8').split(os.EOL),
};
} catch (e) {
return null;
}
})
.filter(Boolean);
}
function initDeprecated() {
var isDeprecatedText = ' is deprecated and will be removed in v5.';
var isDeprecatedProp = ' property' + isDeprecatedText;
Object.defineProperties(transport, {
bytesWritten: {
get: util.deprecate(getBytesWritten, 'bytesWritten' + isDeprecatedProp),
},
file: {
get: util.deprecate(getLogFile, 'file' + isDeprecatedProp),
set: util.deprecate(setLogFile, 'file' + isDeprecatedProp),
},
fileSize: {
get: util.deprecate(getFileSize, 'file' + isDeprecatedProp),
},
});
transport.clear = util.deprecate(clear, 'clear()' + isDeprecatedText);
transport.findLogPath = util.deprecate(
getLogFile,
'findLogPath()' + isDeprecatedText
);
transport.init = util.deprecate(init, 'init()' + isDeprecatedText);
function getBytesWritten() {
return getFile().bytesWritten;
}
function getLogFile() {
return getFile().path;
}
function setLogFile(filePath) {
transport.resolvePath = function () {
return filePath;
};
}
function getFileSize() {
return getFile().size;
}
function clear() {
getFile().clear();
}
function init() {}
}
}
function getDefaultFileName() {
switch (process.type) {
case 'renderer': return 'renderer.log';
case 'worker': return 'worker.log';
default: return 'main.log';
}
}