Last Updated: February 25, 2016
·
2.01K
· errordeveloper

GNU screen is the most robust serial debug utility

Doing embedded development with microcontrollers, I had been using GNU screen as a serial terminal for quite a while now and I love how simple it is:

screen /dev/ttyUSB1 38400

I am surprised that until recently I didn't realise that dumping the debug log to a file would extremely useful. I have used stty and cat in the past, but it is a little bit tedious with USB serial adaptors which are mostly in uses these days. Basically you only need to run stty to set baud rate after the device had been unplugged and plugged back in, so you do forget to run it every now and again and you cannot use it with interactive shells anyway.

So setting log scrollback buffer to some high or infinite size and using screen's search can be a solution, but it's a bit more limiting then using grep and actually being able to preserve the file for future reference.

So looked in screen's man page and found a few very handy commands:

hardcopy [-h] [<file>]

Writes out the currently displayed image to the file file, or, if no filename is specified, to hardcopy.n in the default directory, where n is the number of the current window. This either appends or overwrites the file if it exists. See below. If the option -h is specified, dump also the contents of the scrollback buffer.

hardcopy_append [on|off]

If set to on, screen will append to the hardcopy.n files created by the command C-a h, otherwise these files are overwritten each time. Default is off.

hardcopydir <directory>

Defines a directory where hardcopy files will be placed. If unset, hardcopys are dumped in screen's current working directory.

Potentially I could use hardcopy, but I like log a bit more as I don't need to remember to actually run it.

log [on|off]

Start/stop writing output of the current window to a file "screenlog.%n" in the window's default directory, where %n is the number of the current window. This filename can be changed with the logfile command. If no parameter is given, the state of logging is toggled. The session log is appended to the previous contents of the file if it already exists. The current contents and the contents of the scrollback history are not included in the session log. Default is off.

logfile <filename>
logfile flush <secs>

Defines the name the logfiles will get. The default is "screenlog.%n". The second form changes the number of seconds screen will wait before flushing the logfile buffer to the file-system. The default value is 10 seconds.

logtstamp [on|off]
logtstamp after <secs>
logtstamp string <format>

This command controls logfile time-stamp mechanism of screen. If time-stamps are turned on, screen adds a string containing the current time to the logfile after two minutes of inactivity. When output continues and more than another two minutes have passed, a second time-stamp is added to document the restart of the output. You can change this timeout with the second form of the command. The third form is used for customizing the time-stamp string ("-- %n:%t -- time-stamp -- %M/%d/%y %c:%s --\n" by default).

deflog [on|off]

Same as the log command except that the default setting for new windows is changed. Initial setting is off.

Some of my colleagues and friends switch from screen to tmux, but I noticed that this is one of the features that tmux just didn't happen to have, last time I checked.