Commit ea5b6b81 authored by martin hou's avatar martin hou

fix: 调整日志输出

parent aa728c17
...@@ -124,6 +124,7 @@ function Jensen(log, conn) { ...@@ -124,6 +124,7 @@ function Jensen(log, conn) {
this.onconnect = null; this.onconnect = null;
this.onreceive = null; this.onreceive = null;
this.onerror = null; this.onerror = null;
this.serialNumber = null;
const RECV_BUFF_SIZE = 512000; const RECV_BUFF_SIZE = 512000;
// let cache = new ArrayBuffer(RECV_BUFF_SIZE * 2); // let cache = new ArrayBuffer(RECV_BUFF_SIZE * 2);
...@@ -176,7 +177,7 @@ function Jensen(log, conn) { ...@@ -176,7 +177,7 @@ function Jensen(log, conn) {
await device.selectAlternateInterface(0, 0); await device.selectAlternateInterface(0, 0);
pid = device.productId; pid = device.productId;
self.model = determineModel(device.productId); self.model = determineModel(device.productId);
Logger.info('jensen', 'connect', 'device pid: ' + device.productId); Logger.info(self.identifier(), 'connect', 'device pid: ' + device.productId);
} catch (e) { } catch (e) {
Logger.error('jensen', 'setup', String(e)); Logger.error('jensen', 'setup', String(e));
} }
...@@ -229,6 +230,10 @@ function Jensen(log, conn) { ...@@ -229,6 +230,10 @@ function Jensen(log, conn) {
} }
} }
this.identifier = function () {
return this.serialNumber ? '[' + this.serialNumber + ']' : '[' + this.model + ']';
}
this.connect = async function () { this.connect = async function () {
Logger.debug('jensen', 'connect', 'connect'); Logger.debug('jensen', 'connect', 'connect');
let r = await self.tryconnect(); let r = await self.tryconnect();
...@@ -302,7 +307,7 @@ function Jensen(log, conn) { ...@@ -302,7 +307,7 @@ function Jensen(log, conn) {
}; };
this.disconnect = async function () { this.disconnect = async function () {
Logger.info('jensen', 'disconnect', 'disconnect'); Logger.info(self.identifier(), 'disconnect', 'disconnect');
try { try {
await device?.close(); await device?.close();
} catch (e) {} } catch (e) {}
...@@ -329,7 +334,7 @@ function Jensen(log, conn) { ...@@ -329,7 +334,7 @@ function Jensen(log, conn) {
if (commands.length == 0) return; if (commands.length == 0) return;
cmd = commands.shift(); cmd = commands.shift();
if (cmd.expireTime > 0 && cmd.expireTime < now) { if (cmd.expireTime > 0 && cmd.expireTime < now) {
Logger.info('jensen', 'sendNext', 'expired: cmd-' + cmd.command + '-' + cmd.index + ', ' + cmd.command); // Logger.info('jensen', 'sendNext', 'expired: cmd-' + cmd.command + '-' + cmd.index + ', ' + cmd.command);
continue; continue;
} }
break; break;
...@@ -337,10 +342,10 @@ function Jensen(log, conn) { ...@@ -337,10 +342,10 @@ function Jensen(log, conn) {
let data = cmd.make(); let data = cmd.make();
current = 'cmd-' + cmd.command + '-' + cmd.index; current = 'cmd-' + cmd.command + '-' + cmd.index;
Logger.debug('jensen', 'sendNext', pid + '-command: ' + current + ', data bytes: ' + data.byteLength); // Logger.debug('jensen', 'sendNext', pid + '-command: ' + current + ', data bytes: ' + data.byteLength);
self.timewait = cmd.command == TRANSFER_FILE || cmd.command == GET_FILE_BLOCK || cmd.command == TRANSFER_FILE_PARTIAL ? 1000 : 10; self.timewait = cmd.command == TRANSFER_FILE || cmd.command == GET_FILE_BLOCK || cmd.command == TRANSFER_FILE_PARTIAL ? 1000 : 10;
console.log(pid + '-send', data); // console.log(pid + '-send', data);
if (self.onprogress && cmd.command == TRANSFER_FILE) if (self.onprogress && cmd.command == TRANSFER_FILE)
{ {
try { self.onprogress(0); } catch(e) {} try { self.onprogress(0); } catch(e) {}
...@@ -382,7 +387,7 @@ function Jensen(log, conn) { ...@@ -382,7 +387,7 @@ function Jensen(log, conn) {
timeout(tag); timeout(tag);
}, seconds * 1000) }, seconds * 1000)
: null; : null;
Logger.debug('jensen', 'register', tag); // Logger.debug('jensen', 'register', tag);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
actions[tag] = { actions[tag] = {
tag: tag, tag: tag,
...@@ -400,21 +405,21 @@ function Jensen(log, conn) { ...@@ -400,21 +405,21 @@ function Jensen(log, conn) {
const trigger = function (resp, msgid) { const trigger = function (resp, msgid) {
if (current == null) return; if (current == null) return;
Logger.debug('jensen', 'trigger', pid + ':trigger - ' + current + ' <---> cmd-' + (msgid || 'err')); // Logger.debug('jensen', 'trigger', pid + ':trigger - ' + current + ' <---> cmd-' + (msgid || 'err'));
if (current.substring(0, current.lastIndexOf('-')) != 'cmd-' + msgid) { if (current.substring(0, current.lastIndexOf('-')) != 'cmd-' + msgid) {
// current = null; // current = null;
Logger.info('jensen', 'trigger', 'return cuz cmd mismatched'); // Logger.info('jensen', 'trigger', 'return cuz cmd mismatched');
return; return;
} }
if (current in actions == false) { if (current in actions == false) {
Logger.debug('jensen', 'trigger', pid + ':no action registered'); // Logger.debug('jensen', 'trigger', pid + ':no action registered');
current = null; current = null;
return; return;
} }
let p = actions[current]; let p = actions[current];
if (p.timeout) clearTimeout(p.timeout); if (p.timeout) clearTimeout(p.timeout);
p.resolve(resp); p.resolve(resp);
Logger.info('jensen', 'trigger', p.tag + ', cost: ' + (new Date().getTime() - p.createtime) + 'ms'); // Logger.info('jensen', 'trigger', p.tag + ', cost: ' + (new Date().getTime() - p.createtime) + 'ms');
delete actions[current]; delete actions[current];
current = null; current = null;
}; };
...@@ -476,8 +481,8 @@ function Jensen(log, conn) { ...@@ -476,8 +481,8 @@ function Jensen(log, conn) {
if (device && recvable) if (device && recvable)
{ {
r = await device.transferIn(2, RECV_BUFF_SIZE); r = await device.transferIn(2, RECV_BUFF_SIZE);
Logger.save?.('jensen', 'tryReceive', r?.data); // Logger.save?.('jensen', 'tryReceive', r?.data);
console.log(pid + '-Receive', new Date().toLocaleString(), r?.data); // console.log(pid + '-Receive', new Date().toLocaleString(), r?.data);
} }
return r; return r;
}; };
...@@ -505,7 +510,7 @@ function Jensen(log, conn) { ...@@ -505,7 +510,7 @@ function Jensen(log, conn) {
try { try {
rst = decodeMessage(bview, startIndex, buffLength); rst = decodeMessage(bview, startIndex, buffLength);
} catch (e) { } catch (e) {
Logger.error('jensen', 'decode', String(e)); // Logger.error('jensen', 'decode', String(e));
crash = true; crash = true;
break; break;
} }
...@@ -522,21 +527,23 @@ function Jensen(log, conn) { ...@@ -522,21 +527,23 @@ function Jensen(log, conn) {
for (let x = 0; x < msg.body?.byteLength && x < 32; x++) { for (let x = 0; x < msg.body?.byteLength && x < 32; x++) {
heading.push('0' + (msg.body[x] & 0xff).toString(16).replace(/^0(\w{2})$/gi, '$1')); heading.push('0' + (msg.body[x] & 0xff).toString(16).replace(/^0(\w{2})$/gi, '$1'));
} }
/*
if (msg.id !== TRANSFER_FILE) if (msg.id !== TRANSFER_FILE)
Logger.debug( Logger.debug(
'jensen', 'jensen',
'receive', 'receive',
pid + '-recv: cmd=' + msg.id + ', seq: ' + msg.sequence + ', data bytes: ' + msg.body?.byteLength + ', data: ' + heading.join(' ') pid + '-recv: cmd=' + msg.id + ', seq: ' + msg.sequence + ', data bytes: ' + msg.body?.byteLength + ', data: ' + heading.join(' ')
); );
*/
try { try {
let handler = handlers[msg.id]; let handler = handlers[msg.id];
if (!handler) handler = Jensen.handlers[msg.id]; if (!handler) handler = Jensen.handlers[msg.id];
let r = handler(msg, self); let r = handler(msg, self);
if (r) trigger(r, msg.id); if (r) trigger(r, msg.id);
} catch (e) { } catch (e) {
console.error(e); // console.error(e);
trigger(e); trigger(e);
Logger.error('jensen', 'receive', pid + '-recv: ' + msg.id + ', seq: ' + msg.sequence + ', error: ' + String(e)); // Logger.error('jensen', 'receive', pid + '-recv: ' + msg.id + ', seq: ' + msg.sequence + ', error: ' + String(e));
} }
sendNext(); sendNext();
} }
...@@ -1094,7 +1101,7 @@ Jensen.prototype.readFile = async function (filename, offset, length, seconds) { ...@@ -1094,7 +1101,7 @@ Jensen.prototype.readFile = async function (filename, offset, length, seconds) {
return result; return result;
} }
} else { } else {
Logger.info('jensen', 'readFile', 'file transfer fail.'); Logger.info(this.identifier(), 'readFile', 'file transfer fail.');
return null; return null;
} }
}; };
...@@ -1123,7 +1130,7 @@ Jensen.prototype.streaming = async function (filename, length, ondata, onprogres ...@@ -1123,7 +1130,7 @@ Jensen.prototype.streaming = async function (filename, length, ondata, onprogres
if (typeof length != 'number') throw new Error('parameter `length` required'); if (typeof length != 'number') throw new Error('parameter `length` required');
if (length <= 0) throw new Error('parameter `length` must greater than zero'); if (length <= 0) throw new Error('parameter `length` must greater than zero');
Logger.info('jensen', 'streaming', `file download start. filename: ${filename}, length: ${length} `); Logger.info(this.identifier(), 'streaming', `file download start. filename: ${filename}, length: ${length} `);
let fname = []; let fname = [];
for (let i = 0; i < filename.length; i++) fname.push(filename.charCodeAt(i)); for (let i = 0; i < filename.length; i++) fname.push(filename.charCodeAt(i));
...@@ -1133,13 +1140,13 @@ Jensen.prototype.streaming = async function (filename, length, ondata, onprogres ...@@ -1133,13 +1140,13 @@ Jensen.prototype.streaming = async function (filename, length, ondata, onprogres
if (msg != null) { if (msg != null) {
flen += msg.body.length || msg.body.byteLength; flen += msg.body.length || msg.body.byteLength;
ondata(msg.body); ondata(msg.body);
Logger.info('jensen', 'streaming length', `${length} ${flen}`); Logger.info(this.identifier(), 'streaming length', `${length} ${flen}`);
if (flen >= length) { if (flen >= length) {
Logger.info('jensen', 'streaming', 'file download finish.'); Logger.info(this.identifier(), 'streaming', 'file download finish.');
return 'OK'; return 'OK';
} }
} else { } else {
Logger.info('jensen', 'streaming', 'file download fail.'); Logger.info(this.identifier(), 'streaming', 'file download fail.');
ondata('fail'); ondata('fail');
} }
}; };
...@@ -1152,7 +1159,7 @@ Jensen.prototype.getFilePart = async function (filename, length, ondata, onprogr ...@@ -1152,7 +1159,7 @@ Jensen.prototype.getFilePart = async function (filename, length, ondata, onprogr
if (typeof length != 'number') throw new Error('parameter `length` required'); if (typeof length != 'number') throw new Error('parameter `length` required');
if (length <= 0) throw new Error('parameter `length` must greater than zero'); if (length <= 0) throw new Error('parameter `length` must greater than zero');
Logger.info('jensen', 'getFilePart', `file download start. filename: ${filename}, length: ${length} `); Logger.info(this.identifier(), 'getFilePart', `file download start. filename: ${filename}, length: ${length} `);
let data = []; let data = [];
data.push((length >> 24) & 0xff); data.push((length >> 24) & 0xff);
...@@ -1166,19 +1173,19 @@ Jensen.prototype.getFilePart = async function (filename, length, ondata, onprogr ...@@ -1166,19 +1173,19 @@ Jensen.prototype.getFilePart = async function (filename, length, ondata, onprogr
if (msg != null) { if (msg != null) {
const bodyLen = msg.body ? (msg.body.byteLength ?? msg.body.length ?? 0) : 0; const bodyLen = msg.body ? (msg.body.byteLength ?? msg.body.length ?? 0) : 0;
if (bodyLen === 0 && flen === 0) { if (bodyLen === 0 && flen === 0) {
Logger.info('jensen', 'getFilePart', 'file not found (0-length body).'); Logger.info(this.identifier(), 'getFilePart', 'file not found (0-length body).');
ondata('fail'); ondata('fail');
return 'fail'; return 'fail';
} }
flen += bodyLen; flen += bodyLen;
ondata(msg.body); ondata(msg.body);
Logger.info('jensen', 'getFilePart length', `${length} ${flen}`); Logger.info(this.identifier(), 'getFilePart length', `${length} ${flen}`);
if (flen >= length) { if (flen >= length) {
Logger.info('jensen', 'getFilePart', 'file download finish.'); Logger.info(this.identifier(), 'getFilePart', 'file download finish.');
return 'OK'; return 'OK';
} }
} else { } else {
Logger.info('jensen', 'getFilePart', 'file download fail.'); Logger.info(this.identifier(), 'getFilePart', 'file download fail.');
ondata('fail'); ondata('fail');
return 'fail'; return 'fail';
} }
...@@ -1192,7 +1199,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress) ...@@ -1192,7 +1199,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
if (typeof length != 'number') throw new Error('parameter `length` required'); if (typeof length != 'number') throw new Error('parameter `length` required');
if (length <= 0) throw new Error('parameter `length` must greater than zero'); if (length <= 0) throw new Error('parameter `length` must greater than zero');
Logger.info('jensen', 'getFile', `file download start. filename: ${filename}, length: ${length} `); Logger.info(this.identifier(), 'getFile', `file download start. filename: ${filename}, length: ${length} `);
let fname = []; let fname = [];
for (let i = 0; i < filename.length; i++) fname.push(filename.charCodeAt(i)); for (let i = 0; i < filename.length; i++) fname.push(filename.charCodeAt(i));
...@@ -1245,7 +1252,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress) ...@@ -1245,7 +1252,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
if (msg != null) { if (msg != null) {
const bodyLen = msg.body ? (msg.body.byteLength ?? msg.body.length ?? 0) : 0; const bodyLen = msg.body ? (msg.body.byteLength ?? msg.body.length ?? 0) : 0;
if (bodyLen === 0 && flen === 0) { if (bodyLen === 0 && flen === 0) {
Logger.info('jensen', 'getFile', 'file not found (0-length body).'); Logger.info(this.identifier(), 'getFile', 'file not found (0-length body).');
ondata('fail'); ondata('fail');
return 'fail'; return 'fail';
} }
...@@ -1255,7 +1262,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress) ...@@ -1255,7 +1262,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
if (flen >= length) { if (flen >= length) {
// document.removeEventListener('visibilitychange', visibilitychange); // document.removeEventListener('visibilitychange', visibilitychange);
let cost = new Date().getTime() - stime.getTime(); let cost = new Date().getTime() - stime.getTime();
Logger.info('jensen', 'getFile', 'file transfer finish, cost: ' + cost + ' ms'); Logger.info(this.identifier(), 'getFile', 'file transfer finish, cost: ' + cost + ' ms');
// clearEventAndTask(); // clearEventAndTask();
// return OK indicates all file blocks received // return OK indicates all file blocks received
return 'OK'; return 'OK';
...@@ -1263,7 +1270,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress) ...@@ -1263,7 +1270,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
} else { } else {
// document.removeEventListener('visibilitychange', visibilitychange); // document.removeEventListener('visibilitychange', visibilitychange);
// clearEventAndTask(); // clearEventAndTask();
Logger.info('jensen', 'getFile', 'file transfer fail.'); Logger.info(this.identifier(), 'getFile', 'file transfer fail.');
ondata('fail'); ondata('fail');
return 'fail'; return 'fail';
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment