v1.2.0.1-beta1
Line Logger Example

This is an example of collecting (accumulating) lines of text from a State or Value.

See the published documentation for a properly formatted version of this README.

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/
1// This script stores and returns N last lines of text in a FIFO buffer style,
2// discarding older lines as new ones come in. Lines are added via the append() method.
3//
4// This particular implementation can also color-code an incoming script error log
5// from the DSEP "Last script instance error" state.
6
7// text lines storage
8var log_data = log_data || [];
9
10// regex for splitting up log lines for colorizing the different parts;
11// captures error number, time stamp, instance name, and (optionally) error type.
12// Anatomy & example of an error message:
13// ID TIMESTAMP STATE NAME ERROR TYPE ... rest of the error.
14// 015 [04:12:29.817] DSE_TestTwo ReferenceError: while evaluating 'run()' and script 'test_script.js' at line 33: FileHandl is not defined
15const logLineRx = new RegExp(/^(\d+) \[([\d:\.]+)\] ([^\s]+) ([^\s]+:)?/, "g");
16
17// Call this function from the TP action's expression with the line to append, the number of lines to keep,
18// and whether to try to color-code parts of the log entry.
19export function append(line, maxLines = 6, colorize = false)
20{
21 // optionally wrap special TP color code commands around the error number, time stamp, instance name and error type parts of the new line.
22 if (colorize)
23 line = line.replace(logLineRx, "[c#EB6A6A]$1[/c] [[c#1697E8]$2[/c]] [c#E8CC16]$3[/c] [c#CF62CF]$4[/c]");
24 // clear out old data if needed
25 if (log_data.length >= maxLines)
26 log_data.splice(0, log_data.length - maxLines + 1);
27 // add the new line
28 log_data.push(line);
29 // join the stored lines array and return it; this will get sent back to TP as a state update for this instance's state name.
30 return log_data.join('\n');
31}
32
33// Convenience function to clear the saved lines. Optional message to return as TP state value.
34export function clear(returnString = "Script Log Cleared!") {
35 log_data = [];
36 if (typeof returnString !== 'undefined')
37 return returnString;
38}
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