add logger
This commit is contained in:
@ -18,6 +18,7 @@
|
||||
"scripts": [
|
||||
"scripts/shared/tools.js",
|
||||
"scripts/shared/common.js",
|
||||
"scripts/background/logger.js",
|
||||
"scripts/background/messaging.js",
|
||||
"scripts/background/result.js",
|
||||
"scripts/background/signiture.js",
|
||||
@ -38,6 +39,7 @@
|
||||
"run_at": "document_idle"
|
||||
}],
|
||||
"permissions": [
|
||||
"activeTab"
|
||||
"activeTab",
|
||||
"notifications"
|
||||
]
|
||||
}
|
||||
@ -17,7 +17,7 @@ class Extractor {
|
||||
*/
|
||||
load() {
|
||||
if (!__EXTRACTOR_STATE__) {
|
||||
console.log('No state found. \nPlease upload a saved state from the popup window first.');
|
||||
logger.info('No state found. Please upload a saved state from the popup window first.');
|
||||
return;
|
||||
}
|
||||
let state = JSON.parse(__EXTRACTOR_STATE__);
|
||||
@ -63,11 +63,11 @@ class Extractor {
|
||||
}
|
||||
async _startTasks(from) {
|
||||
if (this._running) {
|
||||
console.log('The Extractor is running. Please wait..');
|
||||
logger.info('The Extractor is running. Please wait..');
|
||||
return;
|
||||
}
|
||||
if (!this._tasks.length) {
|
||||
console.log('No task to run.');
|
||||
logger.info('No task to run.');
|
||||
return;
|
||||
}
|
||||
|
||||
@ -80,7 +80,7 @@ class Extractor {
|
||||
tab = await getActiveTab(true) || await getActiveTab(false);
|
||||
let succ = await ping(tab);
|
||||
if (!succ) {
|
||||
console.log('Cannot contact with active tab.');
|
||||
logger.error('Cannot contact with active tab.');
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -102,7 +102,7 @@ class Extractor {
|
||||
}
|
||||
).catch(err => {
|
||||
this._running = false;
|
||||
console.log(err);
|
||||
logger.error(err);
|
||||
});
|
||||
}
|
||||
/**
|
||||
@ -118,7 +118,7 @@ class Extractor {
|
||||
let exResults = new ExtractResult(results);
|
||||
|
||||
if (!results.length) {
|
||||
console.log(`No result for task #${id}. Forget to call ".start()"?`);
|
||||
logger.info(`No result for task #${id}. Forget to call ".start()"?`);
|
||||
return;
|
||||
}
|
||||
let msg = `
|
||||
@ -132,12 +132,12 @@ ${exResults.toString(50) || "- Empty -"}
|
||||
}
|
||||
_checkTaskId(id, defaultId) {
|
||||
if (!this._tasks.length) {
|
||||
console.log("No task found.");
|
||||
logger.info("No task found.");
|
||||
return -1;
|
||||
}
|
||||
if (!isNaN(defaultId) && id === undefined) id = defaultId;
|
||||
if (isNaN(id) || id < 0 || id >= this._tasks.length) {
|
||||
console.log(`Invalid task id. Rang(0-${this._tasks.length - 1})`);
|
||||
logger.info(`Invalid task id. Rang(0-${this._tasks.length - 1})`);
|
||||
return -1;
|
||||
}
|
||||
return id
|
||||
|
||||
81
scripts/background/logger.js
Normal file
81
scripts/background/logger.js
Normal file
@ -0,0 +1,81 @@
|
||||
const LOGGER_LEVEL = {
|
||||
DEBUG: 1,
|
||||
INFO: 2,
|
||||
WARNING: 3,
|
||||
ERROR: 4,
|
||||
DISABLED: 100,
|
||||
properties: {
|
||||
1: { name: "debug", value: 1, prefix: "DEBUG" },
|
||||
2: { name: "info", value: 2, prefix: "INFO" },
|
||||
3: { name: "warning", value: 3, prefix: "WARN" },
|
||||
4: { name: "error", value: 3, prefix: "ERROR" }
|
||||
}
|
||||
};
|
||||
|
||||
class Logger {
|
||||
_notificationId = undefined;
|
||||
_log_level = LOGGER_LEVEL.INFO;
|
||||
_notify_level = LOGGER_LEVEL.ERROR;
|
||||
constructor(logLevel, notifyLevel) {
|
||||
if (logLevel) this._log_level = logLevel;
|
||||
if (notifyLevel) this._notify_level = notifyLevel;
|
||||
chrome.notifications.onClosed.addListener((id, byUser) => { this._notify_level = undefined });
|
||||
}
|
||||
get logLevel() {
|
||||
return this._log_level;
|
||||
}
|
||||
set logLevel(val) {
|
||||
this._log_level = val;
|
||||
}
|
||||
get notifyLevel() {
|
||||
return this._notify_level;
|
||||
}
|
||||
set notifyLevel(val) {
|
||||
this._notify_level = val;
|
||||
}
|
||||
log(level, loggerFn, ...msgs) {
|
||||
if (level < this._log_level) return;
|
||||
let time = new Date().toLocaleString();
|
||||
loggerFn(`${time} [${LOGGER_LEVEL.properties[level].prefix}]`, ...msgs);
|
||||
if (level < this._notify_level) return;
|
||||
this.notify(...msgs);
|
||||
}
|
||||
debug(...msgs) {
|
||||
this.log(LOGGER_LEVEL.DEBUG, console.debug, ...msgs);
|
||||
}
|
||||
info(...msgs) {
|
||||
this.log(LOGGER_LEVEL.INFO, console.info, ...msgs);
|
||||
}
|
||||
warn(...msgs) {
|
||||
this.log(LOGGER_LEVEL.WARNING, console.info, ...msgs);
|
||||
}
|
||||
error(...msgs) {
|
||||
this.log(LOGGER_LEVEL.ERROR, console.info, ...msgs);
|
||||
}
|
||||
notify(...msgs) {
|
||||
let msg = msgs.join(' ');
|
||||
if (!this._notificationId) {
|
||||
chrome.notifications.create(
|
||||
null,
|
||||
{
|
||||
"type": "basic",
|
||||
"iconUrl": chrome.extension.getURL('icon.png'),
|
||||
"title": "Data Extractor",
|
||||
"message": msg,
|
||||
"priority": 0,
|
||||
"requireInteraction": true,
|
||||
},
|
||||
notificationId => {
|
||||
this._notificationId = notificationId;
|
||||
}
|
||||
);
|
||||
return;
|
||||
}
|
||||
chrome.notifications.update(
|
||||
this._notificationId,
|
||||
{ "message": msg }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const logger = new Logger(LOGGER_LEVEL.DEBUG, LOGGER_LEVEL.DISABLED);
|
||||
@ -18,7 +18,7 @@ function sendMessage(tab, req, log, cond, interval, limit = 0) {
|
||||
loop();
|
||||
|
||||
async function loop() {
|
||||
// console.log("request for", req.action);
|
||||
logger.debug("Request for", req.action);
|
||||
let tabAvailable = await getTabByID(tab.id);
|
||||
if (!tabAvailable) {
|
||||
reject("Task interrupted due to the target tab is closed.");
|
||||
@ -37,7 +37,7 @@ function sendMessage(tab, req, log, cond, interval, limit = 0) {
|
||||
chrome.runtime.lastError;
|
||||
|
||||
let flag = !cond || cond(r);
|
||||
if (log) console.log(log, flag ? '(OK)' : '(failed)');
|
||||
if (log) logger.info(log, flag ? '(OK)' : '(failed)');
|
||||
if (flag) {
|
||||
resolve(r);
|
||||
} else {
|
||||
@ -58,7 +58,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
|
||||
case ACTION_UPLOAD_STATE:
|
||||
sendResponse('recieved!');
|
||||
__EXTRACTOR_STATE__ = request.state;
|
||||
console.log(`State (${request.name}) recieved. Use following to load it: \nsome_var = new Extractor().load()`);
|
||||
logger.info(`State (${request.name}) recieved. To load it: some_var = new Extractor().load()`);
|
||||
break;
|
||||
default:
|
||||
sendResponse("Request not supported.");
|
||||
|
||||
Reference in New Issue
Block a user