This is an example of collecting (accumulating) lines of text from a State or Value.
When the state/value changes, the new text is sent to this script. It stores the last few lines and sends them back as a new State. The number of stored lines can be specified as a parameter to the append()
function.
This particular example is tailored to collecting script errors from this plugin itself. It could be used as in the included Touch Portal button.
As described in Status and Logging, every time an unhandled script error is detected the plugin updates the Last script instance error State with the error message. A Touch Portal event handler detects the change and sends that line of text to this script. The script optionally also color-codes some parts of the error message; the error number, time stamp, and instance name.
- Note
- Assets for this example, including the code and sample button shown below, can be found in the project's repository at
https://github.com/mpaperno/DSEP4TP/tree/main/resources/examples/LineLogger/
8var log_data = log_data || [];
15const logLineRx =
new RegExp(/^(\d+) \[([\d:\.]+)\] ([^\s]+) ([^\s]+:)?/,
"g");
19export
function append(line, maxLines = 6, colorize =
false)
23 line = line.replace(logLineRx,
"[c#EB6A6A]$1[/c] [[c#1697E8]$2[/c]] [c#E8CC16]$3[/c] [c#CF62CF]$4[/c]");
25 if (log_data.length >= maxLines)
26 log_data.splice(0, log_data.length - maxLines + 1);
30 return log_data.join(
'\n');
34export
function clear(returnString =
"Script Log Cleared!") {
36 if (typeof returnString !==
'undefined')
void clear(int mode=Mode.Clipboard)
Clears the system clipboard of all values. The opitonal mode argument is used to control which part...
Example usage on a button