tslog icon indicating copy to clipboard operation
tslog copied to clipboard

Feature Request: PrintPrettyLog should have option to send entire string as one write call to IStd

Open JustinGrote opened this issue 4 years ago • 0 comments

Description / Use Case for a Feature

Currently when implementing a custom output via IStd, write gets called 5 times for a simple message, which makes processing more complicated than it needs to be (buffering the output and then looking maybe for a \n to decide when a log entry is complete, and if I want a multiline output from prettyprint, gets even more complicated especially if the log message has newlines in it)

Instead, a not-breaking simplification would be to add a boolean option to PrintPrettyLog called singleWrite or something that will only call the IStd write method once with the entire prettyprint result string, newlines and all.

Workaround

Use a custom delimiter as a sort of flag to indicate there is more to come. This is a bad code smell.

/** ASCII Section Break character */
const delimiter = String.fromCharCode(31)

class DebugConsoleOutput implements IStd {
    private readonly buffer = new Array<string>()
    write(message: string) {
        if (message.endsWith(delimiter)) {
            this.buffer.push(
                message.replace(delimiter, "\t")
            )
            return
        } else {
            this.buffer.push(message)
            console.log(this.buffer.join(''))
        }
    }
}

JustinGrote avatar Jul 28 '21 12:07 JustinGrote