Skip to content

Commit

Permalink
Colored logs for debugging (#308)
Browse files Browse the repository at this point in the history
* Colored logs for debugging

* disable colors
  • Loading branch information
iBicha authored Feb 26, 2024
1 parent 4953e44 commit f45ba30
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions tools/bs-plugins/logger-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export class LoggerPlugin implements CompilerPlugin {
}
}

this.generateLoggingFile(program, usedLogFunctions);
// @ts-ignore
const isDebug = !!program.options.debug;
this.generateLoggingFile(program, usedLogFunctions, isDebug);
}

beforeFileTranspile(event: BeforeFileTranspileEvent<BscFile>) {
Expand Down Expand Up @@ -133,20 +135,20 @@ export class LoggerPlugin implements CompilerPlugin {
}
}

generateLoggingFile(program: Program, usedLogFunctions: Map<string, LogFunction>) {
generateLoggingFile(program: Program, usedLogFunctions: Map<string, LogFunction>, isDebug: boolean) {
const file = program.getFile(loggingFilePath);
let content = file.fileContents;

content += '\n\' Start of auto-generated functions\n'
usedLogFunctions.forEach((logFunction, newFunctionName) => {
content += this.generateLoggingFunction(newFunctionName, logFunction.name, logFunction.argCount);
content += this.generateLoggingFunction(newFunctionName, logFunction.name, logFunction.argCount, isDebug);
});
content += '\n\' End of auto-generated functions\n'

program.setFile(loggingFilePath, content)
}

generateLoggingFunction(newFunctionName: string, level: string, argCount: number) {
generateLoggingFunction(newFunctionName: string, level: string, argCount: number, isDebug: boolean) {
const args: string[] = [];
const argsParams: string[] = [];
for (let i = 0; i < argCount; i++) {
Expand All @@ -160,13 +162,49 @@ export class LoggerPlugin implements CompilerPlugin {

const func = logFunctions[level as 'LogError' | 'LogWarn' | 'LogInfo' | 'LogDebug'];

// https://github.com/microsoft/vscode/issues/571
const USE_COLOR = false;

const RED = '[31m';
const YELLOW = '[33m';
const GREEN = '[32m';
const BOLD = '[1m';
const BOLD_RED = '[1;31m';
const BOLD_YELLOW = '[1;33m';
const BOLD_GREEN = '[1;32m';
const CLEAR = '[0m';

let logLine = '';

if (isDebug && USE_COLOR) {
switch (func.stringLevel) {
case 'ERROR':
logLine = `Chr(27) + "${BOLD_RED}" + "[${func.stringLevel}]" + Chr(27) + "${RED}" + ${msg} + Chr(27) + "${CLEAR}"`;
break;
case 'WARN':
logLine = `Chr(27) + "${BOLD_YELLOW}" + "[${func.stringLevel}]" + Chr(27) + "${YELLOW}" + ${msg} + Chr(27) + "${CLEAR}"`;
break;
case 'INFO':
logLine = `Chr(27) + "${BOLD}" + "[${func.stringLevel}]" + Chr(27) + "${CLEAR}" + ${msg}`;
break;
case 'DEBUG':
logLine = `Chr(27) + "${BOLD_GREEN}" + "[${func.stringLevel}]" + Chr(27) + "${GREEN}" + ${msg} + Chr(27) + "${CLEAR}"`;
break;
default:
throw new Error(`Unknown log level: ${func.stringLevel}`);
}
}
else {
logLine = `"[${func.stringLevel}]" + ${msg}`
}

return `
function ${newFunctionName}(${args.join(', ')}) as void
logger = m.global.logger
if logger.logLevel < ${func.level}
return
end if
logger.logLine = "[${func.stringLevel}]" + ${msg}
logger.logLine = ${logLine}
end function
`;
}
Expand Down

0 comments on commit f45ba30

Please sign in to comment.