4 Debugging
Jakub Sobon edited this page 2020-11-21 19:41:36 -05:00

Debugging Termdash applications can be challenging, because one cannot use regular debug prints directly. The terminal is initialised in a different mode that makes any debug prints unreadable.

This page suggests a few ways of getting around this limitation.

Using the log package.

The log package has a function that allows redirection of all logged messages into a file:

https://godoc.org/log#SetOutput

Example:

f, err := os.Create("debug.log")
if err != nil {
    log.Fatal(err)
}
log.SetOutput(f)

Now we can add debug statement anywhere in the code like this:

log.Printf("Debugging: %v", someVar)

The log messages will be written into a file named debug.log.

Using the text widget.

Termdash comes with a Text widget which is capable of displaying any text messages. If your application allows it, consider including a separate Text widget in your layout and have it display any debug messages directly on the terminal.

While this may not be a very flexible option, it is often the easiest to implement.

Writing debug messages to files.

If you need to log a large amount of messages, or need to analyse them later, consider writing them to a file instead. A typical setup would look like this:

// In the module where you need to originate the debug messages.
var (
    debug bytes.Buffer
)

// Some function that needs to be debugged.
func SomeFunc() {
    debug.WriteString(fmt.Sprintf("debug message..."))
}

// Position this line in a code that happens once or after the critical section that needs to be debugged.
ioutil.WriteFile("/tmp/debug", debug.Bytes(), 0644)

Once executed, you can read debug messages in /tmp/debug.