Commit ea5b6b81 authored by martin hou's avatar martin hou

fix: 调整日志输出

parent aa728c17
......@@ -124,6 +124,7 @@ function Jensen(log, conn) {
this.onconnect = null;
this.onreceive = null;
this.onerror = null;
this.serialNumber = null;
const RECV_BUFF_SIZE = 512000;
// let cache = new ArrayBuffer(RECV_BUFF_SIZE * 2);
......@@ -176,7 +177,7 @@ function Jensen(log, conn) {
await device.selectAlternateInterface(0, 0);
pid = 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) {
Logger.error('jensen', 'setup', String(e));
}
......@@ -229,6 +230,10 @@ function Jensen(log, conn) {
}
}
this.identifier = function () {
return this.serialNumber ? '[' + this.serialNumber + ']' : '[' + this.model + ']';
}
this.connect = async function () {
Logger.debug('jensen', 'connect', 'connect');
let r = await self.tryconnect();
......@@ -302,7 +307,7 @@ function Jensen(log, conn) {
};
this.disconnect = async function () {
Logger.info('jensen', 'disconnect', 'disconnect');
Logger.info(self.identifier(), 'disconnect', 'disconnect');
try {
await device?.close();
} catch (e) {}
......@@ -329,7 +334,7 @@ function Jensen(log, conn) {
if (commands.length == 0) return;
cmd = commands.shift();
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;
}
break;
......@@ -337,10 +342,10 @@ function Jensen(log, conn) {
let data = cmd.make();
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;
console.log(pid + '-send', data);
// console.log(pid + '-send', data);
if (self.onprogress && cmd.command == TRANSFER_FILE)
{
try { self.onprogress(0); } catch(e) {}
......@@ -382,7 +387,7 @@ function Jensen(log, conn) {
timeout(tag);
}, seconds * 1000)
: null;
Logger.debug('jensen', 'register', tag);
// Logger.debug('jensen', 'register', tag);
return new Promise((resolve, reject) => {
actions[tag] = {
tag: tag,
......@@ -400,21 +405,21 @@ function Jensen(log, conn) {
const trigger = function (resp, msgid) {
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) {
// current = null;
Logger.info('jensen', 'trigger', 'return cuz cmd mismatched');
// Logger.info('jensen', 'trigger', 'return cuz cmd mismatched');
return;
}
if (current in actions == false) {
Logger.debug('jensen', 'trigger', pid + ':no action registered');
// Logger.debug('jensen', 'trigger', pid + ':no action registered');
current = null;
return;
}
let p = actions[current];
if (p.timeout) clearTimeout(p.timeout);
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];
current = null;
};
......@@ -476,8 +481,8 @@ function Jensen(log, conn) {
if (device && recvable)
{
r = await device.transferIn(2, RECV_BUFF_SIZE);
Logger.save?.('jensen', 'tryReceive', r?.data);
console.log(pid + '-Receive', new Date().toLocaleString(), r?.data);
// Logger.save?.('jensen', 'tryReceive', r?.data);
// console.log(pid + '-Receive', new Date().toLocaleString(), r?.data);
}
return r;
};
......@@ -505,7 +510,7 @@ function Jensen(log, conn) {
try {
rst = decodeMessage(bview, startIndex, buffLength);
} catch (e) {
Logger.error('jensen', 'decode', String(e));
// Logger.error('jensen', 'decode', String(e));
crash = true;
break;
}
......@@ -522,21 +527,23 @@ function Jensen(log, conn) {
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'));
}
/*
if (msg.id !== TRANSFER_FILE)
Logger.debug(
'jensen',
'receive',
pid + '-recv: cmd=' + msg.id + ', seq: ' + msg.sequence + ', data bytes: ' + msg.body?.byteLength + ', data: ' + heading.join(' ')
);
*/
try {
let handler = handlers[msg.id];
if (!handler) handler = Jensen.handlers[msg.id];
let r = handler(msg, self);
if (r) trigger(r, msg.id);
} catch (e) {
console.error(e);
// console.error(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();
}
......@@ -1094,7 +1101,7 @@ Jensen.prototype.readFile = async function (filename, offset, length, seconds) {
return result;
}
} else {
Logger.info('jensen', 'readFile', 'file transfer fail.');
Logger.info(this.identifier(), 'readFile', 'file transfer fail.');
return null;
}
};
......@@ -1123,7 +1130,7 @@ Jensen.prototype.streaming = async function (filename, length, ondata, onprogres
if (typeof length != 'number') throw new Error('parameter `length` required');
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 = [];
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
if (msg != null) {
flen += msg.body.length || msg.body.byteLength;
ondata(msg.body);
Logger.info('jensen', 'streaming length', `${length} ${flen}`);
Logger.info(this.identifier(), 'streaming length', `${length} ${flen}`);
if (flen >= length) {
Logger.info('jensen', 'streaming', 'file download finish.');
Logger.info(this.identifier(), 'streaming', 'file download finish.');
return 'OK';
}
} else {
Logger.info('jensen', 'streaming', 'file download fail.');
Logger.info(this.identifier(), 'streaming', 'file download fail.');
ondata('fail');
}
};
......@@ -1152,7 +1159,7 @@ Jensen.prototype.getFilePart = async function (filename, length, ondata, onprogr
if (typeof length != 'number') throw new Error('parameter `length` required');
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 = [];
data.push((length >> 24) & 0xff);
......@@ -1166,19 +1173,19 @@ Jensen.prototype.getFilePart = async function (filename, length, ondata, onprogr
if (msg != null) {
const bodyLen = msg.body ? (msg.body.byteLength ?? msg.body.length ?? 0) : 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');
return 'fail';
}
flen += bodyLen;
ondata(msg.body);
Logger.info('jensen', 'getFilePart length', `${length} ${flen}`);
Logger.info(this.identifier(), 'getFilePart length', `${length} ${flen}`);
if (flen >= length) {
Logger.info('jensen', 'getFilePart', 'file download finish.');
Logger.info(this.identifier(), 'getFilePart', 'file download finish.');
return 'OK';
}
} else {
Logger.info('jensen', 'getFilePart', 'file download fail.');
Logger.info(this.identifier(), 'getFilePart', 'file download fail.');
ondata('fail');
return 'fail';
}
......@@ -1192,7 +1199,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
if (typeof length != 'number') throw new Error('parameter `length` required');
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 = [];
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)
if (msg != null) {
const bodyLen = msg.body ? (msg.body.byteLength ?? msg.body.length ?? 0) : 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');
return 'fail';
}
......@@ -1255,7 +1262,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
if (flen >= length) {
// document.removeEventListener('visibilitychange', visibilitychange);
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();
// return OK indicates all file blocks received
return 'OK';
......@@ -1263,7 +1270,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
} else {
// document.removeEventListener('visibilitychange', visibilitychange);
// clearEventAndTask();
Logger.info('jensen', 'getFile', 'file transfer fail.');
Logger.info(this.identifier(), 'getFile', 'file transfer fail.');
ondata('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