Console#
The Console module provides standard console output functionality for debugging and logging, similar to browser console APIs.
Console Object#
console.log(...args)#
Outputs messages to both the serial console and display (depending on configuration). The primary logging function for debugging.
Parameters:
| Parameter | Type | Description |
|---|---|---|
...args |
any |
Any number of values to output |
Returns: None
Example:
Global Print Functions#
print(...args)#
Outputs values without a newline. Useful for building output line by line.
Parameters:
| Parameter | Type | Description |
|---|---|---|
...args |
any |
Any number of values to output |
Returns: None
Example:
println(...args)#
Outputs values followed by a newline. Equivalent to console.log().
Parameters:
| Parameter | Type | Description |
|---|---|---|
...args |
any |
Any number of values to output |
Returns: None
Example:
Console Applications#
System Monitor#
var display = require("display");
var keyboard = require("keyboard");
function systemMonitor() {
var startTime = Date.now();
var loopCount = 0;
console.log("=== System Monitor Started ===");
console.log("Start Time:", Date.now());
while (true) {
if (keyboard.getEscPress()) break;
loopCount++;
var uptime = Date.now() - startTime;
// Display status line
display.println("Uptime: " + formatUptime(uptime));
display.println(" | Loops: " + loopCount);
display.println(" | Avg: " + (uptime / loopCount).toFixed(1) + "ms");
display.println(" | Press Back to exit");
delay(100);
}
console.log("=== System Monitor Stopped ===");
console.log("Total uptime:", formatUptime(Date.now() - startTime));
console.log("Total loops:", loopCount);
}
function formatUptime(ms) {
var seconds = Math.floor(ms / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
if (hours > 0) {
return hours + "h " + (minutes % 60) + "m " + (seconds % 60) + "s";
} else if (minutes > 0) {
return minutes + "m " + (seconds % 60) + "s";
} else {
return seconds + "s";
}
}
// Start monitoring
systemMonitor();
Data Table Formatter#
Log Analysis#
function logAnalyzer(logData) {
var lines = logData.split("\n");
var analysis = {
totalLines: lines.length,
errorCount: 0,
warningCount: 0,
infoCount: 0,
debugCount: 0,
patterns: {}
};
console.log("Analyzing log data...");
var i, j, line, matches, match;
for (i = 0; i < lines.length; i++) {
line = lines[i];
/* Manual trim check (no String.trim) */
if (!line || line.replace(/\s/g, "") === "") {
continue;
}
/* Log level counting (no includes) */
if (line.indexOf("[ERROR]") !== -1) {
analysis.errorCount++;
} else if (line.indexOf("[WARN]") !== -1) {
analysis.warningCount++;
} else if (line.indexOf("[INFO]") !== -1) {
analysis.infoCount++;
} else if (line.indexOf("[DEBUG]") !== -1) {
analysis.debugCount++;
}
/* Pattern matching */
matches = line.match(/\b\w+Exception\b|\b\w+Error\b/g);
if (matches) {
for (j = 0; j < matches.length; j++) {
match = matches[j];
if (analysis.patterns[match]) {
analysis.patterns[match]++;
} else {
analysis.patterns[match] = 1;
}
}
}
}
/* Display results */
console.log("\n=== Log Analysis Results ===");
console.log("Total lines:", analysis.totalLines);
console.log("Errors:", analysis.errorCount);
console.log("Warnings:", analysis.warningCount);
console.log("Info:", analysis.infoCount);
console.log("Debug:", analysis.debugCount);
/* Check if patterns exist (no Object.keys) */
var hasPatterns = false;
for (match in analysis.patterns) {
hasPatterns = true;
break;
}
if (hasPatterns) {
console.log("\nError patterns:");
for (match in analysis.patterns) {
console.log(" " + match + ":", analysis.patterns[match]);
}
}
/* Health score */
var total = analysis.errorCount + analysis.warningCount + analysis.infoCount;
var healthScore = 100;
if (total > 0) {
healthScore = Math.round((1 - (analysis.errorCount / total)) * 100);
}
console.log("\nSystem health score:", healthScore + "%");
return analysis;
}
var sampleLog =
"[2024-01-30T10:00:00Z] [INFO] System started\n" +
"[2024-01-30T10:00:01Z] [DEBUG] Loading modules\n" +
"[2024-01-30T10:00:02Z] [WARN] Low battery detected\n" +
"[2024-01-30T10:00:03Z] [ERROR] ConnectionException: Failed to connect\n" +
"[2024-01-30T10:00:04Z] [INFO] Retrying connection\n" +
"[2024-01-30T10:00:05Z] [ERROR] TimeoutError: Request timed out";
var analysis = logAnalyzer(sampleLog);
Output Formatting#
Formatting Utilities#
/* String formatting helpers */
function pad(str, length, char) {
if (char === undefined) {
char = " ";
}
str = String(str);
while (str.length < length) {
str = char + str;
}
return str;
}
function formatBytes(bytes) {
var units = ["B", "KB", "MB", "GB"];
var size = bytes;
var unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size = size / 1024;
unitIndex++;
}
return size.toFixed(1) + " " + units[unitIndex];
}
function formatDuration(ms) {
if (ms < 1000) {
return ms + "ms";
}
if (ms < 60000) {
return (ms / 1000).toFixed(1) + "s";
}
if (ms < 3600000) {
return Math.floor(ms / 60000) + "m " +
Math.floor((ms % 60000) / 1000) + "s";
}
var hours = Math.floor(ms / 3600000);
var minutes = Math.floor((ms % 3600000) / 60000);
return hours + "h " + minutes + "m";
}
/* Usage in console output */
function systemReport() {
var now = Date.now();
console.log("=== System Report ===");
console.log("Uptime:", formatDuration(now));
console.log("Memory:", formatBytes(512000), "used of", formatBytes(1048576));
console.log("CPU:", pad("85%", 6), "load");
console.log("Temperature:", pad("42°C", 6));
}
systemReport();