Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Loglevel #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion bin/espruino-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ function getHelp() {
" (bluetooth is not currently supported).",
" Optionally skip N first bytes of the bin file.",
"",
" --loglevel 0 : Set loglevel (0-3). Default is 3",
" 0 = Log errors",
" 1 = Log errors and non-debug messages",
" 2 = Log errors, non-debug messages and warnings",
" 3 = Log errors, non-debug messages, warnings and debug messages",
"",
"If no file, command, or firmware update is specified, this will act",
"as a terminal for communicating directly with Espruino. Press Ctrl-C",
"twice to exit.",
Expand Down Expand Up @@ -167,6 +173,9 @@ for (var i=2;i<process.argv.length;i++) {
} else if (arg=="--sleep") {
i++; args.sleepAfterUpload = parseFloat(next);
if (!isNextValidNumber(next)) throw new Error("Expecting a number argument to --sleep");
} else if (arg=="--loglevel") {
i++; args.logLevel = Number(next);
if (!isNextValidNumber(next)) throw new Error("Expecting a number argument (0-3) to --loglevel");
} else throw new Error("Unknown Argument '"+arg+"', try --help");
} else {
if ("file" in args)
Expand Down Expand Up @@ -848,4 +857,6 @@ function main() {
}

// Start up
require('../index.js').init(main);
require('../index.js').init(args.logLevel !== undefined ? {
logLevel: args.logLevel
} : undefined, main);
2 changes: 1 addition & 1 deletion core/codeWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
for (var i=0;i<code.length;i++) {
var ch = code.charCodeAt(i);
if ((ch<32 || ch>255) && ch!=9/*Tab*/ && ch!=10/*LF*/ && ch!=13/*CR*/) {
console.warn("Funky character code "+ch+" at position "+i+". Replacing with ?");
logger.warn("Funky character code "+ch+" at position "+i+". Replacing with ?");
code = code.substr(0,i)+"?"+code.substr(i+1);
}
}
Expand Down
16 changes: 8 additions & 8 deletions core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
if (typeof chrome !== 'undefined' && chrome.storage) {
chrome.storage.sync.get( "CONFIGS", function (data) {
var value = data["CONFIGS"];
console.log("GET chrome.storage.sync = "+JSON.stringify(value));
logger.debug("GET chrome.storage.sync = "+JSON.stringify(value));
callback(value);
});
} else if (typeof window !== 'undefined' && window.localStorage) {
var data = {};
var value = window.localStorage.getItem("CONFIG");
console.log("GET window.localStorage = "+JSON.stringify(value));
logger.debug("GET window.localStorage = "+JSON.stringify(value));
try {
data = JSON.parse(value);
} catch (e) {
console.log("Invalid config data");
logger.error("Invalid config data");
}
callback(data);
} else if (typeof document != "undefined") {
Expand All @@ -61,7 +61,7 @@
var json = atob(cookie);
data = JSON.parse(json);
} catch (e) {
console.log("Got ", e, " while reading info");
logger.error("Got ", e, " while reading info");
}
}
callback(data);
Expand All @@ -72,10 +72,10 @@

function _set(data) {
if (typeof chrome !== 'undefined' && chrome.storage) {
console.log("SET chrome.storage.sync = "+JSON.stringify(data,null,2));
logger.debug("SET chrome.storage.sync = "+JSON.stringify(data,null,2));
chrome.storage.sync.set({ CONFIGS : data });
} else if (typeof window !== 'undefined' && window.localStorage) {
console.log("SET window.localStorage = "+JSON.stringify(data,null,2));
logger.debug("SET window.localStorage = "+JSON.stringify(data,null,2));
window.localStorage.setItem("CONFIG",JSON.stringify(data));
} else if (typeof document != "undefined") {
document.cookie = "CONFIG="+btoa(JSON.stringify(data));
Expand All @@ -88,7 +88,7 @@
if (key=="set") continue;
Espruino.Config[key] = value[key];
if (Espruino.Core.Config.data[key] !== undefined &&
Espruino.Core.Config.data[key].onChange !== undefined)
Espruino.Core.Config.data[key].onChange !== undefined)
Espruino.Core.Config.data[key].onChange(value[key]);
}
if (callback!==undefined)
Expand Down Expand Up @@ -146,7 +146,7 @@
found = true;

if (!found) {
console.warn("Section named "+c.section+" was not added with Config.addSection");
logger.warn("Section named "+c.section+" was not added with Config.addSection");
sections[c.section] = {
name : c.section,
sortOrder : 0
Expand Down
2 changes: 1 addition & 1 deletion core/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
try {
json = JSON.parse(result);
} catch (e) {
console.log("JSON parse failed - " + e + " in " + JSON.stringify(result));
logger.error("JSON parse failed - " + e + " in " + JSON.stringify(result));
}
}
if (Object.keys(json).length==0) {
Expand Down
42 changes: 21 additions & 21 deletions core/flasher.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@
callback("Can't find STM32 bootloader. Make sure the chip is reset into bootloader mode by holding down BTN1 while pressing RST");
}, (timeout==undefined)?10000:timeout);
var iPoll = setInterval(function() {
console.log("Sending... 0x7F");
logger.debug("Sending... 0x7F");
Espruino.Core.Serial.write("\x7f", false);
}, 70);
dataReceived = function (c) {
console.log("got "+c);
logger.debug("got "+c);
if (c==ACK || c==NACK) {
clearTimeout(iTimeout);
clearInterval(iPoll);
setStatus("Initialised.");
// wait for random extra data...
dataReceived = function(c){
console.log("Already ACKed but got "+c);
logger.debug("Already ACKed but got "+c);
};
setTimeout(function() {
dataReceived = undefined;
Expand Down Expand Up @@ -114,7 +114,7 @@
// Extended erase
sendCommand(0x44, function(err) {
if (err) { callback(err); return; }
console.log("We may be some time...");
logger.debug("We may be some time...");
sendData([0xFF,0xFF], function(err) {
if (err) { callback(err); return; }
callback(undefined);
Expand All @@ -123,25 +123,25 @@
};

var readData = function(callback, addr, readBytes) {
console.log("Reading "+readBytes+" bytes from 0x"+addr.toString(16)+"...");
logger.debug("Reading "+readBytes+" bytes from 0x"+addr.toString(16)+"...");
// send read command
sendCommand(0x11, function(err) {
if (err) {
console.log("Error sending command ("+err+").");
logger.error("Error sending command ("+err+").");
callback(err);
return;
}
// send address
sendData([(addr>>24)&0xFF,(addr>>16)&0xFF,(addr>>8)&0xFF,addr&0xFF], function(err) {
if (err) {
console.log("Error sending address. ("+err+")");
logger.error("Error sending address. ("+err+")");
callback(err);
return;
}
// send amount of bytes we want
sendData([readBytes-1], function(err) {
if (err) {
console.log("Error while reading. ("+err+")");
logger.error("Error while reading. ("+err+")");
callback(err);
return;
}
Expand Down Expand Up @@ -170,25 +170,25 @@
readData(function(err, data) {
if (err) return callback(err);
var word = (data[3]<<24) | (data[2]<<16) | (data[1]<<8) | data[0];
console.log("RCC->CFGR = "+word);
logger.debug("RCC->CFGR = "+word);
var newword = (word&0xFFFFF8FF) | 0x00000400;
if (newword==word) {
console.log("RCC->CFGR is correct");
logger.debug("RCC->CFGR is correct");
callback(undefined);
} else {
console.log("Setting RCC->CFGR to "+newword);
logger.debug("Setting RCC->CFGR to "+newword);
writeData(callback, RCC_CFGR, [newword&0xFF, (newword>>8)&0xFF, (newword>>16)&0xFF, (newword>>24)&0xFF]);
}
}, RCC_CFGR, 4);
};

var writeData = function(callback, addr, data) {
if (data.length>256) callback("Writing too much data");
console.log("Writing "+data.length+" bytes at 0x"+addr.toString(16)+"...");
logger.debug("Writing "+data.length+" bytes at 0x"+addr.toString(16)+"...");
// send write command
sendCommand(0x31, function(err) {
if (err) {
console.log("Error sending command ("+err+"). retrying...");
logger.error("Error sending command ("+err+"). retrying...");
initialiseChip(function (err) {
if (err) callback(err);
else writeData(callback, addr, data);
Expand All @@ -198,7 +198,7 @@
// send address
sendData([(addr>>24)&0xFF,(addr>>16)&0xFF,(addr>>8)&0xFF,addr&0xFF], function(err) {
if (err) {
console.log("Error sending address ("+err+"). retrying...");
logger.error("Error sending address ("+err+"). retrying...");
initialiseChip(function (err) {
if (err) callback(err);
else writeData(callback, addr, data);
Expand All @@ -212,7 +212,7 @@
// send data
sendData(sData, function(err) {
if (err) {
console.log("Error while writing ("+err+"). retrying...");
logger.error("Error while writing ("+err+"). retrying...");
initialiseChip(function (err) {
if (err) callback(err);
else writeData(callback, addr, data);
Expand All @@ -227,7 +227,7 @@

var writeAllData = function(binary, flashOffset, callback) {
var chunkSize = 256;
console.log("Writing "+binary.byteLength+" bytes");
logger.debug("Writing "+binary.byteLength+" bytes");
Espruino.Core.Status.setStatus("Writing flash...", binary.byteLength);
var writer = function(offset) {
if (offset>=binary.byteLength) {
Expand All @@ -250,7 +250,7 @@
var readAllData = function(binaryLength, flashOffset, callback) {
var data = new Uint8Array(flashOffset);
var chunkSize = 256;
console.log("Reading "+binaryLength+" bytes");
logger.debug("Reading "+binaryLength+" bytes");
Espruino.Core.Status.setStatus("Reading flash...", binaryLength);
var reader = function(offset) {
if (offset>=binaryLength) {
Expand Down Expand Up @@ -302,7 +302,7 @@
for (var i=0;i<bufView.length;i++) bytesReceived.push(bufView[i]);
if (dataReceived!==undefined) {
for (var i=0;i<bytesReceived.length;i++) {
if (dataReceived===undefined) console.log("OH NO!");
if (dataReceived===undefined) logger.error("OH NO!");
dataReceived(bytesReceived[i]);
}
bytesReceived = [];
Expand Down Expand Up @@ -359,7 +359,7 @@
function flashDevice(url, flashOffset, callback, statusCallback) {
Espruino.Core.Utils.getBinaryURL(url, function (err, binary) {
if (err) { callback(err); return; }
console.log("Downloaded "+binary.byteLength+" bytes");
logger.debug("Downloaded "+binary.byteLength+" bytes");
flashBinaryToDevice(binary, flashOffset, callback, statusCallback);
});
};
Expand All @@ -374,7 +374,7 @@
for (var i=0;i<bufView.length;i++) bytesReceived.push(bufView[i]);
if (dataReceived!==undefined) {
for (var i=0;i<bytesReceived.length;i++) {
if (dataReceived===undefined) console.log("OH NO!");
if (dataReceived===undefined) logger.error("OH NO!");
dataReceived(bytesReceived[i]);
}
bytesReceived = [];
Expand All @@ -397,7 +397,7 @@
if (err) return finish(err);
var data = new Uint8Array([0x04,0x00,0xFA,0x05]);
var addr = 0xE000ED0C;
console.log("Writing "+data.length+" bytes at 0x"+addr.toString(16)+"...");
logger.debug("Writing "+data.length+" bytes at 0x"+addr.toString(16)+"...");
// send write command
sendCommand(0x31, function(err) {
if (err) return finish(err);
Expand Down
20 changes: 10 additions & 10 deletions core/flasherESP8266.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
var l = uartLine.split("\n");
while(l.length>1) {
var a = l.shift();
console.log(">>>",a);
logger.debug(">>>",a);
try {
if (packetHandler) packetHandler(Espruino.Core.Utils.parseJSONish(a));
} catch(e) { console.log("Unable to decode"); }
} catch(e) { logger.error("Unable to decode"); }
}
uartLine = l[0];
});
Expand All @@ -74,11 +74,11 @@
then(() => cmdFlash(options)).
then(() => unsetupEspruino(options)).
then(function() {
console.log("Complete!");
logger.debug("Complete!");
finish();
if (options.cbDone) options.cbDone();
}).catch(function(error) {
console.log("Error!", error);
logger.log("Error!", error);
finish();
if (options.cbDone) options.cbDone(error);
});
Expand Down Expand Up @@ -117,7 +117,7 @@ function setupEspruino(options) {
return new Promise((resolve,reject)=>{
Espruino.Core.Serial.write('\x03\x10reset()\n', false, function() {
setTimeout(function() {
console.log("Start Wifi, add handler");
logger.debug("Start Wifi, add handler");
Espruino.Core.Serial.write(`\x10${options.serialDevice}.setup(115200, { rx: ${options.serialRx}, tx : ${options.serialTx} });
\x10var packetHandler, packetData="", OUT=eval(process.env.CONSOLE);
\x10${options.serialDevice}.on('data', function(d) {
Expand All @@ -144,7 +144,7 @@ function setupEspruino(options) {
\x10${options.chBoot?`digitalWrite(${options.chBoot}, 0);`:`` /* into of boot mode */}
\x10${options.chPD?`digitalWrite(${options.chPD}, 1);`:`` /* turn on wifi */}
`, false, function() {
console.log("Handler added");
logger.debug("Handler added");
resolve();
});
}, 1000);
Expand All @@ -164,7 +164,7 @@ function unsetupEspruino(options) {
}

function cmdSync(options) {
console.log("Syncing...");
logger.debug("Syncing...");
return new Promise((resolve,reject)=>{
var success = false;
var interval;
Expand All @@ -175,7 +175,7 @@ function cmdSync(options) {
interval = undefined;
}
packetHandler = undefined;
console.log("Sync complete!");
logger.debug("Sync complete!");
setTimeout(function() {
// allow time for other responses
resolve();
Expand All @@ -202,7 +202,7 @@ function cmdSync(options) {
function cmdFlash(options) {
var binary = new Uint8Array(options.binary);
var blockCount = Math.floor((binary.length + (BLOCK_SIZE-1)) / BLOCK_SIZE);
console.log(`Start Flashing, ${blockCount} blocks of ${BLOCK_SIZE} bytes`);
logger.debug(`Start Flashing, ${blockCount} blocks of ${BLOCK_SIZE} bytes`);
var d = new Uint32Array([
blockCount*BLOCK_SIZE, // erase size
blockCount, // # packets
Expand All @@ -211,7 +211,7 @@ function cmdFlash(options) {
]);
var idx = 0;
return sendCmd(options, 2 /* FLASH_BEGIN */, new Uint8Array(d.buffer)).then(function flash() {
console.log("Block "+idx);
logger.debug("Block "+idx);
if (options.cbStatus) options.cbStatus(`Writing Block ${idx} / ${blockCount}`, idx / blockCount);
if (idx>=blockCount) return true;
d = new Uint8Array(16 + BLOCK_SIZE);
Expand Down
8 changes: 4 additions & 4 deletions core/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
var fullModuleName = data.moduleName;

// try and load the module the old way...
console.log("loadModule("+fullModuleName+")");
logger.debug("loadModule("+fullModuleName+")");

var urls = []; // Array of where to look for this module
var modName; // Simple name of the module
Expand Down Expand Up @@ -167,16 +167,16 @@
function moduleLoaded(resolve, requires, modName, data, loadedModuleData, alreadyMinified){
// Check for any modules used from this module that we don't already have
var newRequires = getModulesRequired(data);
console.log(" - "+modName+" requires "+JSON.stringify(newRequires));
logger.debug(" - "+modName+" requires "+JSON.stringify(newRequires));
// if we need new modules, set them to load and get their promises
var newPromises = [];
for (var i in newRequires) {
if (requires.indexOf(newRequires[i])<0) {
console.log(" Queueing "+newRequires[i]);
logger.debug(" Queueing "+newRequires[i]);
requires.push(newRequires[i]);
newPromises.push(loadModule(requires, newRequires[i], loadedModuleData));
} else {
console.log(" Already loading "+newRequires[i]);
logger.debug(" Already loading "+newRequires[i]);
}
}

Expand Down
Loading