Skip to content

Commit

Permalink
add log level to logger
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed Oct 31, 2024
1 parent be52bbc commit 6dc270d
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 12 deletions.
8 changes: 4 additions & 4 deletions cli/buildengine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,12 @@ export async function compileWithLocalCompileService(extinfo: pxtc.ExtensionInfo
const resp = await runDockerCompileAsync(extinfo.compileData);

if (resp.hexfile) {
console.log("Compile successful");
pxt.log("Compile successful");
}
else {
console.log("Compile failed");
console.log(resp.stderr)
console.log(resp.stdout)
pxt.log("Compile failed");
pxt.log(resp.stderr)
pxt.log(resp.stdout)
}

return resp.hexfile && {
Expand Down
4 changes: 3 additions & 1 deletion cli/commandparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ export class CommandParser {
const debugFlag = flagName || match[2];
if (debugFlag == "debug" || debugFlag == "d" || debugFlag == "dbg") {
pxt.options.debug = true;
pxt.debug = console.log;
if (pxt.options.debug) {
pxt.setLogLevel(pxt.LogLevel.Debug);
}
pxt.log(`debug mode`);
if (!flagName)
continue;
Expand Down
4 changes: 3 additions & 1 deletion kiosk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ window.addEventListener("DOMContentLoaded", () => {
const bundle = (window as any).pxtTargetBundle as pxt.TargetBundle;

pxt.options.debug = /dbg=1/i.test(window.location.href);
if (pxt.options.debug) pxt.debug = console.debug;
if (pxt.options.debug) {
pxt.setLogLevel(pxt.LogLevel.Debug)
}

pxt.setupWebConfig((window as any).pxtConfig || pxt.webConfig);
pxt.setAppTarget(bundle);
Expand Down
49 changes: 48 additions & 1 deletion pxtlib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,77 @@ namespace pxt {
debug(...args: any[]): void;
error(...args: any[]): void;
warn(...args: any[]): void;

setLogLevel(level: LogLevel): void;
getLogLevel(): LogLevel;
}

export enum LogLevel {
Debug = 0,
Info = 1,
Log = 1,
Warning = 2,
Error = 3
}

class ConsoleLogger implements Logger {
export class ConsoleLogger implements Logger {
protected logLevel: LogLevel;

constructor() {
this.setLogLevel(LogLevel.Info);
}

setLogLevel(level: LogLevel): void {
this.logLevel = level;
}

getLogLevel(): LogLevel {
return this.logLevel;
}

info(...args: any[]): void {
if (!this.shouldLog(LogLevel.Info)) return;

if (console?.info) {
console.info.call(null, ...args);
}
}

log(...args: any[]): void {
if (!this.shouldLog(LogLevel.Log)) return;

if (console?.log) {
console.log.call(null, ...args);
}
}

debug(...args: any[]): void {
if (!this.shouldLog(LogLevel.Debug)) return;

if (console?.debug) {
console.debug.call(null, ...args);
}
}

error(...args: any[]): void {
if (!this.shouldLog(LogLevel.Error)) return;

if (console?.error) {
console.error.call(null, ...args);
}
}

warn(...args: any[]): void {
if (!this.shouldLog(LogLevel.Warning)) return;

if (console?.warn) {
console.warn.call(null, ...args);
}
}

protected shouldLog(level: LogLevel) {
return level >= this.logLevel;
}
}

let logger: Logger = new ConsoleLogger();
Expand All @@ -62,6 +101,14 @@ namespace pxt {
}

export function setLogger(impl: Logger) {
const level = logger?.getLogLevel();
logger = impl;
if (level !== undefined) {
logger.setLogLevel(level);
}
}

export function setLogLevel(level: LogLevel) {
logger.setLogLevel(level);
}
}
49 changes: 48 additions & 1 deletion pxtsim/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,77 @@ namespace pxt {
debug(...args: any[]): void;
error(...args: any[]): void;
warn(...args: any[]): void;

setLogLevel(level: LogLevel): void;
getLogLevel(): LogLevel;
}

export enum LogLevel {
Debug = 0,
Info = 1,
Log = 1,
Warning = 2,
Error = 3
}

class ConsoleLogger implements Logger {
export class ConsoleLogger implements Logger {
protected logLevel: LogLevel;

constructor() {
this.setLogLevel(LogLevel.Info);
}

setLogLevel(level: LogLevel): void {
this.logLevel = level;
}

getLogLevel(): LogLevel {
return this.logLevel;
}

info(...args: any[]): void {
if (!this.shouldLog(LogLevel.Info)) return;

if (console?.info) {
console.info.call(null, ...args);
}
}

log(...args: any[]): void {
if (!this.shouldLog(LogLevel.Log)) return;

if (console?.log) {
console.log.call(null, ...args);
}
}

debug(...args: any[]): void {
if (!this.shouldLog(LogLevel.Debug)) return;

if (console?.debug) {
console.debug.call(null, ...args);
}
}

error(...args: any[]): void {
if (!this.shouldLog(LogLevel.Error)) return;

if (console?.error) {
console.error.call(null, ...args);
}
}

warn(...args: any[]): void {
if (!this.shouldLog(LogLevel.Warning)) return;

if (console?.warn) {
console.warn.call(null, ...args);
}
}

protected shouldLog(level: LogLevel) {
return level >= this.logLevel;
}
}

let logger: Logger = new ConsoleLogger();
Expand All @@ -62,6 +101,14 @@ namespace pxt {
}

export function setLogger(impl: Logger) {
const level = logger?.getLogLevel();
logger = impl;
if (level !== undefined) {
logger.setLogLevel(level);
}
}

export function setLogLevel(level: LogLevel) {
logger.setLogLevel(level);
}
}
4 changes: 3 additions & 1 deletion teachertool/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ window.addEventListener("DOMContentLoaded", () => {
const optsQuery = pxt.Util.parseQueryString(window.location.href.toLowerCase());

pxt.options.debug = optsQuery["dbg"] == "1";
if (pxt.options.debug) pxt.debug = console.debug;
if (pxt.options.debug) {
pxt.setLogLevel(pxt.LogLevel.Debug);
}

if (optsQuery["consoleticks"] == "1" || optsQuery["consoleticks"] == "verbose") {
pxt.analytics.consoleTicks = pxt.analytics.ConsoleTickOptions.Verbose;
Expand Down
4 changes: 3 additions & 1 deletion tutorialtool/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ window.addEventListener("DOMContentLoaded", () => {
const bundle = (window as any).pxtTargetBundle as pxt.TargetBundle;

pxt.options.debug = /dbg=1/i.test(window.location.href);
if (pxt.options.debug) pxt.debug = console.debug;
if (pxt.options.debug) {
pxt.setLogLevel(pxt.LogLevel.Debug);
}

pxt.setupWebConfig((window as any).pxtConfig || pxt.webConfig);
pxt.setAppTarget(bundle);
Expand Down
5 changes: 3 additions & 2 deletions webapp/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5936,8 +5936,9 @@ document.addEventListener("DOMContentLoaded", async () => {
const config = pxt.webConfig

const optsQuery = Util.parseQueryString(window.location.href.toLowerCase());
if (optsQuery["dbg"] == "1")
pxt.debug = pxt.debug;
if (optsQuery["dbg"] == "1") {
pxt.setLogLevel(pxt.LogLevel.Debug);
}
pxt.options.light = optsQuery["light"] == "1" || pxt.BrowserUtils.isARM() || pxt.BrowserUtils.isIE();
if (pxt.options.light) {
pxsim.U.addClass(document.body, 'light');
Expand Down

0 comments on commit 6dc270d

Please sign in to comment.