[logging] Save Screen (program) output to a file

I need to save the whole output of Screen to a file to check later all the content.

The reason is that I'm dumping a flash memory through a serial port, using Screen to interface with it. I would like to save it to a file to check memory structure.

I've tried:

$: screen /dev/ttyUSB0 115200 >> foo.txt
$: screen /dev/ttyUSB0 115200 | tee foo.txt

And I've also tried to use bufferfile from screen, but I don't understand how to use it.

Is there an easy way?

This question is related to logging buffer dump gnu-screen

The answer is


Ctrl+A then Shift+H works for me. You can view the file screenlog.0 while the program is still running.


The selected answer doesn't work quite well with multiple sessions and doesn't allow to specify a custom log file name.

For multiple screen sessions, this is my formula:

  1. Create a configuration file for each process:

    logfile test.log
    logfile flush 1
    log on
    logtstamp after 1
    logtstamp string "[ %t: %Y-%m-%d %c:%s ]\012"
    logtstamp on
    

    If you want to do it "on the fly", you can change logfile automatically. \012 means "new line", as using \n will print it on the log file: source.

  2. Start your command with the "-c" and "-L" flags:

    screen -c ./test.conf -dmSL 'Test' ./test.pl
    

    That's it. You will see "test.log" after the first flush:

    ...
    6 Something is happening...
    [ test.pl: 2016-06-01 13:02:53 ]
    7 Something else...
    [ test.pl: 2016-06-01 13:02:54 ]
    8 Nothing here
    [ test.pl: 2016-06-01 13:02:55 ]
    9 Something is happening...
    [ test.pl: 2016-06-01 13:02:56 ]
    10 Something else...
    [ test.pl: 2016-06-01 13:02:57 ]
    11 Nothing here
    [ test.pl: 2016-06-01 13:02:58 ]
    ...
    

I found that "-L" is still required even when "log on" is on the configuration file.

I couldn't find a list of the time format variables (like %m) used by screen. If you have a link of those formats, please post it bellow.

Extra

In case you want to do it "on the fly", you can use this script:

#!/bin/bash
if [[ $2 == "" ]]; then
    echo "Usage: $0 name command";
    exit 1;
fi
name=$1
command=$2
path="/var/log";
config="logfile ${path}/${name}.log
logfile flush 1
log on
logtstamp after 1
logtstamp string \"[ %t: %Y-%m-%d %c:%s ]\012\"
logtstamp on";
echo "$config" > /tmp/log.conf
screen -c /tmp/log.conf -dmSL "$name" $command
rm /tmp/log.conf

To use it, save it (screen.sh) and set +x permissions:

./screen.sh TEST ./test.pl

... and will execute ./test.pl and create a log file in /var/log/TEST.log


The following might be useful (tested on: Linux/Ubuntu 12.04 (Precise Pangolin)):

cat /dev/ttyUSB0

Using the above, you can then do all the re-directions that you need. For example, to dump output to your console while saving to your file, you'd do:

cat /dev/ttyUSB0 | tee console.log

Here's a trick: wrap it in sh -c!

screen sh -c './some-script 2>&1 | tee mylog.log'

Where 2>&1 redirects stderr to stdout so tee can catch and log error messages.


A different answer if you need to save the output of your whole scrollback buffer from an already actively running screen:

Ctrl-a [ g SPACE G $ >.

This will save your whole buffer to /tmp/screen-exchange


For the Mac terminal:

script -a -t 0 out.txt screen /dev/ttyUSB0 115200

Details

  • script: A built-in application to "make a typescript of terminal session"
  • -a: Append to output file
  • -t 0: Time between writing to output file is 0 seconds, so out.txt is updated for every new character
  • out.txt: Is just the output file name
  • screen /dev/ttyUSB0 115200: Command from question for connecting to an external device

You can then use tail to see that the file is updating.

tail -100 out.txt

The 'script' command under Unix should do the trick. Just run it at the start of your new console and you should be good.


The following command works for Screen version 4.06.02:

screen -L -Logfile Log_file_name_of_your_choice command_to_be_executed

From the man page of Screen:

-Logfile file : By default logfile name is "screenlog.0".
                You can set new logfile name with the "-Logfile" option.

You can check the existing version of Screen using screen -version. You can download and install the latest Screen version from https://www.gnu.org/software/screen/.


Existing screen log can be saved by :

Ctrl+A : hardcopy -h filename


You can also use Control-a + H to save loggings into screenlog.n file. One more Control-a + H to turn off.

C-a H: Begins/ends logging of the current window to the file "screenlog.n".


Examples related to logging

How to redirect docker container logs to a single file? Console logging for react? Hide strange unwanted Xcode logs Where are logs located? Retrieve last 100 lines logs Spring Boot - How to log all requests and responses with exceptions in single place? How do I get logs from all pods of a Kubernetes replication controller? Where is the Docker daemon log? How to log SQL statements in Spring Boot? How to do logging in React Native?

Examples related to buffer

Convert a JSON Object to Buffer and Buffer to JSON Object back C char array initialization Save Screen (program) output to a file Flushing buffers in C Char array to hex string C++ How to append binary data to a buffer in node.js Convert a binary NodeJS Buffer to JavaScript ArrayBuffer How to clear input buffer in C? How to find the socket buffer size of linux Ring Buffer in Java

Examples related to dump

Python JSON dump / append to .txt with each variable on new line Save Screen (program) output to a file How to restore PostgreSQL dump file into Postgres databases? Dump Mongo Collection into JSON format How can I convert an MDB (Access) file to MySQL (or plain SQL file)? kill -3 to get java thread dump Difference between javacore, thread dump and heap dump in Websphere What is the JavaScript equivalent of var_dump or print_r in PHP? List of tables, db schema, dump etc using the Python sqlite3 API Convert a string representation of a hex dump to a byte array using Java?

Examples related to gnu-screen

How do I force detach Screen from another SSH session? Kill Attached Screen in Linux Save Screen (program) output to a file How do I increase the scrollback buffer in a running screen session? How to send control+c from a bash script? Terminal Multiplexer for Microsoft Windows - Installers for GNU Screen or tmux How do I get out of 'screen' without typing 'exit'? How to assign name for a screen? Kill detached screen session How to list running screen sessions?