Spanish Italian
17435 Users    

Give colors to your log trace with ANSI Escape sequences

  Download PDF version of the Article

The scope of this article is to show how you can enhance your serial trace logs adding a touch of originality and distinction: the colors. Colored trace logs are very useful, since by using a common communication program (such as Tera Term Pro on Windows or minicom on Linux, both freeware), you can give a different level of priority (importance) to your trace logs.

Having a chance to work with embedded systems is indeed a challenging and satisfying experience. You can have control on all the hardware resources, programming and debugging the microprocessor as well as different types of peripherals. Today’s microcontrollers are very powerful, integrating a large number of communication interfaces: depending on the particular application, they can offer USB interface, Ethernet controller, SPI, I2C, CAN, LIN, and so on. There is however an “old” but still useful and chip interface: the serial interface (the most common version is the RS232). It is and probably will be for a long time the most common debug interface adopted by hardware and software engineers.

There are many cases where sophisticated and technologically advanced equipment are sold to the customers with fast and powerful interfaces, allowing also a remote control of the device (that happens for example with the telnet and the web interface), but if you need to debug the software, see what happens during the power-on, check or modify the status of register, you will probably need a serial link.

The scope of this article is to show how you can enhance your serial trace logs adding a touch of originality and distinction: the colors. Colored trace logs are very useful, since by using a common communication program (such as Tera Term Pro on Windows or minicom on Linux, both freeware), you can give a different level of priority (importance) to your trace logs.
For instance, a possible strategy for assigning different colors to each type of trace log could be the following:

  • use the default color (usually it is the black) for ordinary and common trace logs – for instance the logs generated during a successful power-on procedure or during the execution of an operator command
  • use the BLUE color for the trace logs which are related to meaningful and not common event or state – for instance the logs generated when a timer expires, an external event is detected, a debug procedure is executed, etc.
  • use the MAGENTA color for the trace logs used to indicate abnormal conditions encountered during the execution of the application – an example could be the raise of an alarm or warning affecting a feature made available by the system, as for instance a battery low warning, or a firmware upgrade in progress warning
  • use the RED color for the trace logs that indicate anomalies and errors of very high priority – for instance to indicate an over temperature alarm, an error encountered during a write operation on the flash memory, an error encountered when accessing an external peripherals, and so on.

How can we add the colors to our trace logs? The answer to this question is very simple, we just use the ANSI Escape sequences, also called as ANSI Escape codes. They consist of a sequence of ASCII characters, always started with the ESC character (1B hex) followed by the left-bracket character (5B hex), which defines an alphanumeric code that controls a display or keyboard function. In order to have the ANSI Escape sequences interpreted and executed, we need to set the communication program in VT100 or VT52 emulation mode; that can be easily performed on both the TeraTerm Pro and minicom communication tools. VT100/VT52 mode is necessary because ANSI Escape sequences are intended for use on text terminal (in the past they were widely used to connect to mainframes and minicomputers).

In particular, the ANSI Escape function which is used to obtain the colored trace logs is the one called Set Graphics Mode. Its purpose is to change the colors and attributes of the text displayed on screen; these functions remain active until the next occurrence of a Set Graphics Mode sequence.

The syntax of this Escape sequence is the following: ESC[Value;…;Value,m, where Value can be any of the following values (the following lists are not complete, but the omitted codes are the ones which are not widely supported):

Text attributes

0 All attributes off
1 Bold on
4 Underscore single (only on monochrome display)
5 Blink (slow) on
7 Reverse video on (swap foreground and background)
8 Concealed on (not widely supported)

Foreground colors

30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White

Background colors

40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White

The code

We are going to use the C language, probably the most widely used programming language in embedded systems and in firmware applications development. Trace logs can be produced in C by using the printf function that, on embedded systems, usually redirects the output to the serial link. First of all, we are going to introduce the definitions related to the available colors; in doing that, we will apply the Escape sequences described above acting on the following three steps (in C the Escape character can be emitted with the “\e” combination:

  1. output a first Escape sequence with text attributes off (the first character after the left-bracket is 0) and foreground color set as requested
  2. output the string associated to the trace log
  3. output a second Escape sequence with text attributes off and default foreground color

The definitions are as follows:
#define COLOR_OFF "\e[0m"
#define RED(text) "\e[0;31m" text COLOR_OFF
#define GREEN(text) "\e[0;32m" text COLOR_OFF
#define YELLOW(text) "\e[0;33m" text COLOR_OFF
#define BLUE(text) "\e[0;34m" text COLOR_OFF
#define MAGENTA(text) "\e[0;35m" text COLOR_OFF
#define CYAN(text) "\e[0;36m" text COLOR_OFF
#define WHITE(text) "\e[0;37m" text COLOR_OFF

Examples of usage are the following:
printf(BLUE(“This is an example of a BLUE colored trace log\n”);
printf(RED(“Fatal error writing on flash sector %d\n”, 0x1234);
printf(MAGENTA(“Power-on procedure executed in %4.4X msec\n”, 0x0570);

This is just the basic idea, of course you can introduce more macros and take advantage of all the other available Escape sequence codes.

Reference

http://ascii-table.com/ansi-escape-sequences.php

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Who's new

  • mauriss
  • christiank79
  • agabor
  • fabriziopd
  • irenix
  • pepershoe
  • raghun14
  • andreaspousette
  • rilhyk
  • thientruong

Who's online

There are currently 0 users and 49 guests online.