Skip to content

Commit

Permalink
refactor all logging to go through a logger object
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed Oct 29, 2024
1 parent 0088816 commit b979f2d
Show file tree
Hide file tree
Showing 80 changed files with 394 additions and 273 deletions.
2 changes: 1 addition & 1 deletion pxtblocks/codecardRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function renderCodeCard(card: pxt.CodeCard, options: CodeCardRenderOption
if (card.blocksXml) {
const svg = render(card.blocksXml);
if (!svg) {
console.error("failed to render blocks");
pxt.error("failed to render blocks");
pxt.debug(card.blocksXml);
} else {
let holder = div(img, ''); holder.setAttribute('style', 'width:100%; min-height:10em');
Expand Down
6 changes: 3 additions & 3 deletions pxtblocks/compiler/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export function forEachStatementInput(block: Blockly.Block, cb: (block: Blockly.
export function printScope(scope: Scope, depth = 0) {
const declared = Object.keys(scope.declaredVars).map(k => `${k}(${scope.declaredVars[k].id})`).join(",");
const referenced = scope.referencedVars.join(", ");
console.log(`${mkIndent(depth)}SCOPE: ${scope.firstStatement ? scope.firstStatement.type : "TOP-LEVEL"}`)
pxt.log(`${mkIndent(depth)}SCOPE: ${scope.firstStatement ? scope.firstStatement.type : "TOP-LEVEL"}`)
if (declared.length) {
console.log(`${mkIndent(depth)}DECS: ${declared}`)
pxt.log(`${mkIndent(depth)}DECS: ${declared}`)
}
// console.log(`${mkIndent(depth)}REFS: ${referenced}`)
// pxt.log(`${mkIndent(depth)}REFS: ${referenced}`)
scope.children.forEach(s => printScope(s, depth + 1));
}

Expand Down
2 changes: 1 addition & 1 deletion pxtblocks/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function diffWorkspace(oldWs: Blockly.Workspace, newWs: Blockly.Workspace, optio

function logger() {
const log = pxt.options.debug || (window && /diffdbg=1/.test(window.location.href))
? console.log : (message?: any, ...args: any[]) => { };
? pxt.log : (message?: any, ...args: any[]) => { };
return log;

}
Expand Down
2 changes: 1 addition & 1 deletion pxtblocks/fields/fieldEditorRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function registerFieldEditor(selector: string, field: FieldCustomConstruc

export function createFieldEditor(selector: string, text: string, params: any): FieldCustom {
if (registeredFieldEditors[selector] == undefined) {
console.error(`Field editor ${selector} not registered`);
pxt.error(`Field editor ${selector} not registered`);
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion pxtblocks/fields/field_numberdropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function parseDropdownOptions(options: FieldTextDropdownOptions): [string, any][
return result;
}
else {
console.warn("Could not parse numberdropdown data field");
pxt.warn("Could not parse numberdropdown data field");
}

return [];
Expand Down
8 changes: 4 additions & 4 deletions pxtblocks/fields/field_textdropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function validateOptions(options: Blockly.MenuOption[]) {
const tuple = options[i];
if (!Array.isArray(tuple)) {
foundError = true;
console.error(
pxt.error(
'Invalid option[' +
i +
']: Each FieldDropdown option must be an ' +
Expand All @@ -249,7 +249,7 @@ function validateOptions(options: Blockly.MenuOption[]) {
);
} else if (typeof tuple[1] !== 'string') {
foundError = true;
console.error(
pxt.error(
'Invalid option[' +
i +
']: Each FieldDropdown option id must be ' +
Expand All @@ -264,7 +264,7 @@ function validateOptions(options: Blockly.MenuOption[]) {
typeof tuple[0].src !== 'string'
) {
foundError = true;
console.error(
pxt.error(
'Invalid option[' +
i +
']: Each FieldDropdown option must have a ' +
Expand Down Expand Up @@ -323,7 +323,7 @@ function parseDropdownOptions(options: FieldTextDropdownOptions): [string, strin
return result;
}
else {
console.warn("Could not parse textdropdown data field");
pxt.warn("Could not parse textdropdown data field");
}
}

Expand Down
2 changes: 1 addition & 1 deletion pxtblocks/fields/field_tileset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class FieldTileset extends FieldImages implements FieldCustom {
}

if (this.sourceBlock_) {
console.warn(`Trying to set tile reference to nonexistent tile. Block type: ${this.sourceBlock_.type}, Field name: ${this.name}, Value: ${newValue}`)
pxt.warn(`Trying to set tile reference to nonexistent tile. Block type: ${this.sourceBlock_.type}, Field name: ${this.name}, Value: ${newValue}`)
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion pxtblocks/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ function createBlockFromShadow(shadow: Element) {
export function patchShadows(root: Element, inShadow: boolean) {
if (root.tagName === "shadow") {
if (root.parentElement?.tagName === "xml") {
console.warn(`Shadow block of type '${root.getAttribute("type")}' found at top level. Converting to non-shadow block`);
pxt.warn(`Shadow block of type '${root.getAttribute("type")}' found at top level. Converting to non-shadow block`);
pxt.tickEvent(`blocks.import.topLevelShadow`, { blockId: root.getAttribute("type") });

const newBlock = createBlockFromShadow(root);
Expand Down
2 changes: 1 addition & 1 deletion pxtblocks/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ async function convertIconsToPngAsync(xsg: Document): Promise<void> {
let href = imageIconCache[svgUri];
if (!href) {
href = await pxt.BrowserUtils.encodeToPngAsync(svgUri, { width, height, pixelDensity: 2 });
console.log(`HREF: ${href}`);
pxt.log(`HREF: ${href}`);
}

imageIconCache[svgUri] = href;
Expand Down
12 changes: 6 additions & 6 deletions pxtblocks/legacyMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,22 @@ export function addMutation(b: MutatingBlock, info: pxtc.SymbolInfo, mutationTyp
switch (mutationType) {
case MutatorTypes.ObjectDestructuringMutator:
if (!info.parameters || info.parameters.length < 1) {
console.error("Destructuring mutations require at least one parameter")
pxt.error("Destructuring mutations require at least one parameter")
}
else {
let found = false;
for (const param of info.parameters) {
if (param.type.indexOf("=>") !== -1) {
if (!param.properties || param.properties.length === 0) {
console.error("Destructuring mutations only supported for functions with an event parameter that has multiple properties");
pxt.error("Destructuring mutations only supported for functions with an event parameter that has multiple properties");
return;
}
found = true;
}
}

if (!found) {
console.error("Destructuring mutations must have an event parameter");
pxt.error("Destructuring mutations must have an event parameter");
return;
}
}
Expand All @@ -102,7 +102,7 @@ export function addMutation(b: MutatingBlock, info: pxtc.SymbolInfo, mutationTyp
m = new DefaultInstanceMutator(b, info);
break;
default:
console.warn("Ignoring unknown mutation type: " + mutationType);
pxt.warn("Ignoring unknown mutation type: " + mutationType);
return;
}

Expand All @@ -126,7 +126,7 @@ export function mutateToolboxBlock(block: Node, mutationType: string, mutation:
case MutatorTypes.DefaultInstanceMutator:
mutationElement.setAttribute(DefaultInstanceMutator.attributeName, mutation);
default:
console.warn("Ignoring unknown mutation type: " + mutationType);
pxt.warn("Ignoring unknown mutation type: " + mutationType);
return;
}

Expand Down Expand Up @@ -366,7 +366,7 @@ class DestructuringMutator extends MutatorHelper {
this.parameterRenames = JSON.parse(xmlElement.getAttribute(DestructuringMutator.renameAttributeName));
}
catch (e) {
console.warn("Ignoring invalid rename map in saved block mutation");
pxt.warn("Ignoring invalid rename map in saved block mutation");
}
}

Expand Down
8 changes: 4 additions & 4 deletions pxtblocks/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function injectBlockDefinition(info: pxtc.BlocksInfo, fn: pxtc.SymbolInfo, comp:
}

if (Blockly.Blocks[fn.attributes.blockId]) {
console.error("duplicate block definition: " + id);
pxt.error("duplicate block definition: " + id);
return false;
}

Expand Down Expand Up @@ -342,7 +342,7 @@ function initBlock(block: Blockly.Block, info: pxtc.BlocksInfo, fn: pxtc.SymbolI

if (fn.attributes.shim === "ENUM_GET" || fn.attributes.shim === "KIND_GET") {
if (comp.parameters.length > 1 || comp.thisParameter) {
console.warn(`Enum blocks may only have 1 parameter but ${fn.attributes.blockId} has ${comp.parameters.length}`);
pxt.warn(`Enum blocks may only have 1 parameter but ${fn.attributes.blockId} has ${comp.parameters.length}`);
return;
}
}
Expand Down Expand Up @@ -383,7 +383,7 @@ function initBlock(block: Blockly.Block, info: pxtc.BlocksInfo, fn: pxtc.SymbolI

firstParam = false;
if (!pr) {
console.error("block " + fn.attributes.blockId + ": unknown parameter " + part.name + (part.ref ? ` (${part.ref})` : ""));
pxt.error("block " + fn.attributes.blockId + ": unknown parameter " + part.name + (part.ref ? ` (${part.ref})` : ""));
return;
}

Expand Down Expand Up @@ -424,7 +424,7 @@ function initBlock(block: Blockly.Block, info: pxtc.BlocksInfo, fn: pxtc.SymbolI
}

if (syms.length == 0) {
console.error(`no instances of ${typeInfo.qName} found`)
pxt.error(`no instances of ${typeInfo.qName} found`)
}
const dd: Blockly.MenuOption[] = syms.map(v => {
let k = v.attributes.block || v.attributes.blockId || v.name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ const FUNCTION_DECLARATION_MIXIN: FunctionDeclarationMixin = {
});
break;
default:
console.warn("Unexpected input type on a function mutator root: " + input.type);
pxt.warn("Unexpected input type on a function mutator root: " + input.type);
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion pxtblocks/plugins/functions/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const variableReporterMixin = {
callback: () => {
let variableField = this.getField('VAR') as Blockly.FieldVariable;
if (!variableField) {
console.log("Tried to get a variable field on the wrong type of block.");
pxt.log("Tried to get a variable field on the wrong type of block.");
}
variableField.setValue(variable.getId());
}
Expand Down
2 changes: 1 addition & 1 deletion pxtblocks/plugins/functions/functionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class FunctionManager {
this._editFunctionExternal(mutation, cb);
}
else {
console.warn('External function editor must be overriden: Blockly.Functions.editFunctionExternalHandler', mutation, cb);
pxt.warn('External function editor must be overriden: Blockly.Functions.editFunctionExternalHandler', mutation, cb);
}
}
}
2 changes: 1 addition & 1 deletion pxtblocks/plugins/functions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export function mutateCallersAndDefinition(name: string, ws: Blockly.Workspace,
});
Blockly.Events.setGroup(false);
} else {
console.warn("Attempted to change function " + name + ", but no definition block was found on the workspace");
pxt.warn("Attempted to change function " + name + ", but no definition block was found on the workspace");
}
}

Expand Down
24 changes: 12 additions & 12 deletions pxtcompiler/emitter/backbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ ${baseLabel}_nochk:
if (bi) {
let off = U.lookup(th.stackAtLabel, `__brkp_${bi.id}`)
if (off !== this.proc.debugInfo.localsMark) {
console.log(bi)
console.log(th.stackAtLabel)
pxt.log(bi)
pxt.log(th.stackAtLabel)
U.oops(`offset doesn't match: ${off} != ${this.proc.debugInfo.localsMark}`)
}
}
Expand Down Expand Up @@ -312,17 +312,17 @@ ${baseLabel}_nochk:

for (let i = 0; i < this.proc.body.length; ++i) {
let s = this.proc.body[i]
// console.log("STMT", s.toString())
// pxt.log("STMT", s.toString())
switch (s.stmtKind) {
case ir.SK.Expr:
this.emitExpr(s.expr)
break;
case ir.SK.StackEmpty:
if (this.exprStack.length > 0) {
for (let stmt of this.proc.body.slice(i - 4, i + 1))
console.log(`PREVSTMT ${stmt.toString().trim()}`)
pxt.log(`PREVSTMT ${stmt.toString().trim()}`)
for (let e of this.exprStack)
console.log(`EXPRSTACK ${e.currUses}/${e.totalUses} E: ${e.toString()}`)
pxt.log(`EXPRSTACK ${e.currUses}/${e.totalUses} E: ${e.toString()}`)
oops("stack should be empty")
}
this.write("@stackempty locals")
Expand Down Expand Up @@ -387,7 +387,7 @@ ${baseLabel}_nochk:
private terminate(expr: ir.Expr) {
assert(expr.exprKind == ir.EK.SharedRef)
let arg = expr.args[0]
// console.log("TERM", arg.sharingInfo(), arg.toString(), this.dumpStack())
// pxt.log("TERM", arg.sharingInfo(), arg.toString(), this.dumpStack())
U.assert(arg.currUses != arg.totalUses)
// we should have the terminated expression on top
U.assert(this.exprStack[0] === arg, "term at top")
Expand All @@ -407,14 +407,14 @@ ${baseLabel}_nochk:
}

private validateJmpStack(lbl: ir.Stmt, off = 0) {
// console.log("Validate:", off, lbl.lblName, this.dumpStack())
// pxt.log("Validate:", off, lbl.lblName, this.dumpStack())
let currSize = this.exprStack.length - off
if (lbl.lblStackSize == null) {
lbl.lblStackSize = currSize
} else {
if (lbl.lblStackSize != currSize) {
console.log(lbl.lblStackSize, currSize)
console.log(this.dumpStack())
pxt.log(lbl.lblStackSize, currSize)
pxt.log(this.dumpStack())
U.oops("stack misaligned at: " + lbl.lblName)
}
}
Expand Down Expand Up @@ -542,7 +542,7 @@ ${baseLabel}_nochk:

// result in R0
private emitExpr(e: ir.Expr): void {
//console.log(`EMITEXPR ${e.sharingInfo()} E: ${e.toString()}`)
//pxt.log(`EMITEXPR ${e.sharingInfo()} E: ${e.toString()}`)

switch (e.exprKind) {
case ir.EK.JmpValue:
Expand Down Expand Up @@ -930,8 +930,8 @@ ${baseLabel}_nochk:
let allArgs = nonRefs.concat(refs)
for (let r of allArgs) {
if (r.currUses != 0 || r.totalUses != 1) {
console.log(r.toString())
console.log(allArgs.map(a => a.toString()))
pxt.log(r.toString())
pxt.log(allArgs.map(a => a.toString()))
U.oops(`wrong uses: ${r.currUses} ${r.totalUses}`)
}
r.currUses = 1
Expand Down
4 changes: 2 additions & 2 deletions pxtcompiler/emitter/backjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ function ${id}(s) {

// result in R0
function emitExpr(e: ir.Expr): void {
//console.log(`EMITEXPR ${e.sharingInfo()} E: ${e.toString()}`)
//pxt.log(`EMITEXPR ${e.sharingInfo()} E: ${e.toString()}`)

switch (e.exprKind) {
case EK.JmpValue:
Expand Down Expand Up @@ -607,7 +607,7 @@ function ${id}(s) {
write(`${frameRef} = ${id}(s);`)
}

//console.log("PROCCALL", topExpr.toString())
//pxt.log("PROCCALL", topExpr.toString())
topExpr.args.forEach((a, i) => {
let arg = `arg${i}`
if (isLambda) {
Expand Down
Loading

0 comments on commit b979f2d

Please sign in to comment.