Skip to content

Instantly share code, notes, and snippets.

@westc
Last active February 13, 2022 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westc/088682af25f978c08aaf303ac8c95f40 to your computer and use it in GitHub Desktop.
Save westc/088682af25f978c08aaf303ac8c95f40 to your computer and use it in GitHub Desktop.
Apex Log Indenter - Add indents to Apex logs files to make them easier to read and navigate.
/**
* Responsible for producing the first half of the page all the way up to but
* not including the output. All of the elements produced by this function
* serve display purposes and input purposes.
* @param data {Object}
* An object mainly representing the input data.
* @returns {Array=}
* An array of objects that represent the elements that will be shown in the
* input section of the page. If nothing is returned, the input section will
* not be updated
*/
function getInputs(data) {
return [
{ type: 'uploadable-text', id: 'txtContents', rows: 20 },
{
type: 'div',
className: 'text-center',
children: [
{
type: 'button',
text: 'Indent',
disabled: !data.valuesById.txtContents,
id: 'btnIndent'
}
]
}
];
}
/**
* Responsible for producing the second half of the page which starts after the
* input section and goes to the end of the page. All of the elements produced
* by this function only serve display purposes.
* @param data {Object}
* An object mainly representing the input data.
* @returns {Array=}
* An array of objects that represent the elements that will be shown in the
* output section of the page. If nothing is returned, the output section
* will not be updated.
*/
function getOutputs(data) {
if (data.clickedButtonId === 'btnIndent') {
return [
{
type: 'downloadable-text',
value: indentApexLog(data.valuesById.txtContents),
rows: 20,
downloadName: (data.filesById.txtContents || {}).name || 'download.txt'
}
];
}
}
// Used by indentApexLog() to figure out when indentation should start and end.
var RGX_HEADER = /^[\d:\.]+\s*\(\w+\)\|(?:(CODE_UNIT_STARTED|\w+_ENTRY)|(CODE_UNIT_FINISHED|\w+_EXIT))\|/;
/**
* Indents the specified contents of an Apex file to make it easier to navigate
* within an IDE that has a tree-view whenever indentation is added.
* @param contents {string}
* The contents of the Apex log file.
* @returns {string}
* The contents with proper indentation whenever code units start and end and
* whenever other things have an ENTRY and an EXIT.
*/
function indentApexLog(contents) {
var indentation = "";
return contents.split(/\r?\n|\r/).map(function(line) {
var m = RGX_HEADER.exec(line) || [];
if (m[2]) {
indentation = indentation.slice(1)
}
line = indentation + line;
if (m[1]) {
indentation += "\t";
}
return line;
}).join('\n');
}
// Every YourJS IO App must be able to either find index.json or index.jsonc in the Gist.
{
"title": "Apex Log Indenter",
"description": "Add indents to Apex logs files to make them easier to read and navigate.",
"js": [
"<io-app> apex-log-indenter.js",
"indentApexLog.js"
],
"css": []
}
{
// Name of the function responsable for transforming the input into the output.
"transform": "indentApexLog",
// Where to find the code that will be eval'd.
"files": ["indentApexLog.js"],
// Input setup
"input": { "label": "Log File Contents" },
// Output setup
"output": { "label": "Formatted Log File Contents" },
// Type of YourJS IO-App
"type": "split"
}
@westc
Copy link
Author

westc commented Feb 8, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment