Posts mit dem Label StackExchange werden angezeigt. Alle Posts anzeigen
Posts mit dem Label StackExchange werden angezeigt. Alle Posts anzeigen

Freitag, 11. Dezember 2015

Screencast an X11 window by keyboard events

Recently I had the requirement to generate a screen cast of a keyboard interaction. There are many full featured application for Windows or Linux to generate screen casts, but they generate often big MPEG files, because they capture 30 or 60 screens per second and use lossy compression algorithms to minimize the file size. This is fine for video games and OBS does a pretty fine job, I have used it recently myself to capture a Diablo III PTR 2.4 session. But all this is not necessary, if one wants to capture keyboard interactions.

Keyboard interaction is triggered by pressing any keys. On Linux running a X11 server xinput can be used to capture keyboard events. And the standard X11 tool xwd can capture a window. And the ImageMagic can be used to combine several images into an animated GIF. This makes it possible to illustrate a keyboard interaction by pretty small GIF images.

I wrote a simple tool called xscreencast, which implements the above idea. It is hosted on GitHub. I have used it to illustrate the difference of the cursor color of a terminal window and Emacs on Stackexchange. This are the screen casts generated by the Perl script.

This is the terminal.

And this is Emacs.

Dienstag, 28. Oktober 2014

Redirecting stdout and stderr into files without loosing the original file descriptors

Redirecting in Bash can be tricky. But in order to log the output of shell commands it is sometimes necessary. Sometimes it is also necessary to add some time stamps in front of the output. And sometimes it is even necessary to save the original file descriptors. The following example shows how to do all of that.

#! /bin/bash

stamp ()
{
  local LINE
  while IFS='' read -r LINE; do
    echo "$(date '+%Y-%m-%d %H:%M:%S,%N %z') $$ $LINE"
  done
}

exec {STDOUT}>&1
exec {STDERR}>&2
exec 2> >(exec {STDOUT}>&-; exec {STDERR}>&-; exec &>> stderr.log; stamp)
exec > >(exec {STDOUT}>&-; exec {STDERR}>&-; exec >> stdout.log; stamp)

for n in $(seq 3); do
  echo loop $n >&$STDOUT
  echo o$n
  echo e$n >&2
done

Some best practices followed in the code are:

  • Declare local variables as local.
  • Use named file descriptors to avoid collisions.
  • Close unused file descriptors in sub shells.

First I tied to solve my problem with the answers given at Stackexchange. But the answers where not elaborated enough for my needs. So I wrote the above answer.

BTW this is a nice overview for the different ways to redirect in Bash.

Mittwoch, 19. Dezember 2012

Random (type 4) Universally Unique IDentifier in Oracle

Oracle provides a function called sys_guid() to generate unique identifier. But this function produces different results on different systems. It is explained here. The following custom function generates a 16 byte RAW variable containing a random UUID. This is a type 4 UUID according to RFC 4122.
CREATE OR REPLACE FUNCTION random_uuid RETURN RAW IS
  v_uuid RAW(16);
BEGIN
  v_uuid := sys.dbms_crypto.randombytes(16);
  RETURN (utl_raw.overlay(utl_raw.bit_or(utl_raw.bit_and(utl_raw.substr(v_uuid, 7, 1), '0F'), '40'), v_uuid, 7));
END random_uuid;
The function uses the package dbms_crypto to generate 16 random bytes and sets the high nibble of the 7th byte (starting with 1) to 0100b (see 4.1.3. in RFC 4122). This is done with the binary functions from the utl_raw package. It might be necessary to grant the execute permission for the dbms_crypto package:
grant execute on sys.dbms_crypto to random_uuid_user;
This is a result of a StackExchange question.