Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display file with escaped color codes - boot messages from bootlog daemon

I have a file containing color codes:

Fri May 25 17:13:04 2012: [....] Starting MTA: exim4^[[?25l^[[?1c^[7^[[1G[^[[32m ok ^[[39;49m^[8^[[?25h^[[?0c.
Fri May 25 17:13:05 2012: [....] Loading cpufreq kernel  modules...^[[?25l^[[?1c^[7^[[1G[^[[32m ok ^[[39;49m^[8^[[?25h^[[?0cdone (acpi-cpufreq).

How can I display it colorized on a linux terminal?

like image 326
ripat Avatar asked May 25 '12 16:05

ripat


4 Answers

Try less -R /your/file.

I found that more by default actually does what I'd expect: It shows colorized text in a terminal. The fact that more worked, while less (it's younger cousin) didn't made me look at the man less page.

It turns out that less supports the -R flag, which outputs the ESC sequences as raw control characters. This is the same behavior you get with more, plus all the search and navigation enhancements that come standard with less.

like image 117
John Anderson Avatar answered Sep 21 '22 05:09

John Anderson


For the sake of completeness, the file containing all these escape sequences is generated by the bootlogd daemon (bootlog package in the debian family) which captures all the colorized messages sent to the console during boot. On the console, these messages are first displayed like the following line:

[....] Starting periodic command scheduler: cron

then, when the service or command is executed, an escape sequence is sent to the console to reposition the cursor at the beginning of the line and prints ok, fail, info, warn etc...

[ ok ] Starting periodic command scheduler: cron.

All these messages are captured by the bootlogd daemon and written to a file with all its escape sequences including the repositioning one. No problem except that the ^[ must be replaced by octal 033 to have the file correctly displayed. But, because there is a catch, the daemon also add a date stamp in front of the message without changing the coordinates of the cursor repositioning sequence. Consequently, the ok, fail etc... messages overwrite part of the date stamp. Not nice.

Fri May 25 17:13:01 2012: [....] Starting periodic command scheduler: cron
becomes...
[ ok ay 25 17:13:01 2012: [....] Starting periodic command scheduler: cron.

The solution is to change that cursor positioning sequence. By try and error I found that sequence to be ^[1G. The following sed command finally get the job done:

sed 's/\^\[/\o33/g;s/\[1G\[/\[27G\[/' /var/log/boot

The bootlogd daemon should purge all the escape sequence before sending the console messages to the file. May we call this a bug?

This "bug" might also be present in all Debian heirs like Ubuntu, Mint etc...

like image 14
ripat Avatar answered Oct 23 '22 21:10

ripat


So ripat's answer didn't work for me. I found an alternative on the Debian wiki - https://wiki.debian.org/bootlogd.

At time of writing this is:

sed $'s/\^\[/\E/g' /var/log/boot

For bootlogd version <2.88 (no date stamp). For the later versions:

sed $'s/\^\[/\E/g;s/\[1G\[/\[27G\[/' /var/log/boot

The latter formats all but one line of my log perfectly, with only a small discrepancy. Note also that, as pointed out in the bug report for this issue, the leading $ on the sed pattern makes these solutions bash specific.

like image 5
Graeme Avatar answered Oct 23 '22 20:10

Graeme


You could use the bash built-ins :

$ echo "$(< /your/file)"
like image 5
keldrill Avatar answered Oct 23 '22 20:10

keldrill