Re: Ansi colors



Hello Dag and Mike.

Here I go with a more detailed description of the ANSI sequence.

Before starting, Mike: don't worry about the "tail -f" and all that.
Here is a micro-xplanation:

---- [Begin of micro-explanation]

bash = Command line interpreter of linux. Like command.com from MS-DOS.

shell = For this post, shell == bash.

tail -f = tail is a program that shows the last "n" lines of a file. If
"-f" is specified, then the program shows the last "n" lines but does
NOT finish execution and waits for more lines to be read int he file
being printed to the screen, and when a new line is written to the
file, that new line is printed on the screen. This is very useful for
"viewing logs" because you "link" to a log file and the screen shows
every new line that is logged to the file. This way you are completely
independent of "who wrote the log file".

less = a tool to view a file without the need of loading it completely
in memory (only the needed part), scrolling up and down. As the file is
not loaded in memory, you can scroll thru files that may be several MB
long with no overload.

---- [End of micro-explanation]

bash supports ANSI coloring, so if the "tailed" file contains "ANSI"
escape sequences, the strings then are colored and you highlight some
words.

This is also supported by the MS-DOS command.com if you setup ANSI by
placing this line
DEVICE=C:\DOS\ANSI.SYS
in your config.sys file.

Ansi codes are a standard defined to "alter" the behaviour a text
should be displayed. I only use color coding, but it is used to set
bold, underline, and even to control the cursor position on a screen of
given dimensions.

Here go an OK an and ERR as they appear in my log files, if I do a
"less" in a shell:

This is the data in the file:

0003 [ESC[1;32m OK ESC[0m] '05/07/2005 16:41:41' [user@unknown]:
SS_Kernel.CSoundDriverDigigram.DigigramRegister: Driver
: 5.70.Official Tools: 5.52.Official
0004 [ESC[1;31mERR!ESC[0m] '05/07/2005 16:42:18' [user@unknown]:
SS_Kernel.CMacro.Cart_Set: Cart.Name = '' - A cart with
CartType different from CT_Macro has been tried to be assigned to a
macro

The ESC letters here are representing a chr(27), ie: The string
beginning with 0003 is:
"0003" & chr(32) & "[" & chr(27) & "[1;32m" & chr(32) & "OK" & chr(32)
& chr(27) & "[0m]....."

This is how it is seen if I open it in the Notepad:
0003 [ OK ] '05/07/2005 16:18:36' [user@unknown]:
SS_Kernel.CSoundDriverDigigram.DigigramRegister: Driver: 5.70.Official
Tools: 5.52.Official
0004 [ERR!] '05/07/2005 16:19:44' [user@unknown]:
SS_Kernel.CMacro.Cart_Set: Cart.Name = '' - A cart with CartType
different from CT_Macro has been tried to be assigned to a macro

The chr(27) character is represented by an square .

This is how it should look like:

0003 [ OK ] '05/07/2005 16:18:36' [user@unknown]:
SS_Kernel.CSoundDriverDigigram.DigigramRegister: Driver: 5.70.Official
Tools: 5.52.Official
0004 [ERR!] '05/07/2005 16:19:44' [user@unknown]:
SS_Kernel.CMacro.Cart_Set: Cart.Name = '' - A cart with CartType
different from CT_Macro has been tried to be assigned to a macro

But the word OK appear in "green" and the word ERR appear in "red".

The cause is the "sequence" after the "ESC" char:

chr(27) & "[1;34m" is a sequence that sets "forecolor to number 4. 32,
to number 2, 36 to number6 and so on. the "1;" says to set it "bright
color", not "dimmed color".

The sequence:

chr(27) & "[0m" means to restore the original color.

The functions I use (in other programs) to generate those logs are:

Private Function BuildCompleteMsg(LogType As e_LoggerType, ProjectName
As String, ClassName As String, FunctionName As String, Msg As String)
As String

Dim Header As String
Dim LogTab As Integer

BuildCompleteMsg = LogCount & " " & LogTypeString(LogType) & " '" &
Date & " " & Time & "' [" & UserStationID & "]: " & Header &
String(LogTab * 4, " ") & ProjectName & "." & ClassName & "." &
FunctionName & ": " & Msg

End Function

This function generates the complete log line. The interesting part
comes when calling the "LogTypeString(LogType)" which receives an enum
and generates the whole header.

Private Function LogTypeString(LogType As e_LoggerType)
Select Case LogType
Case LT_Ok
LogTypeString = LogTag(" OK ", ColorGreen)
Case LT_Warning
LogTypeString = LogTag("WARN", ColorYellow)
Case LT_Error
LogTypeString = LogTag("ERR!", ColorRed)
Case LT_Dump
LogTypeString = LogTag("DUMP", ColorBlue)
End Select
End Function

This function just selects between 4 different headers and does the
corresponding header. For example, LogTag("ERR!", ColorRed) does the
[ERR!] string with ERR! colored in red.

Private Function LogTag(ID As String, Color As String) As String
LogTag = "[" & Color & ID & ColorReset & "]"
End Function

LogTag assumes that ColorRed is already the ANSI string and does
nothing magical: Sets the "[", the color string. the title, the color
reset and the "]". So the magic must be in ColorRed and ColorReset.

The color generators are:

Private Property Get ColorRed() As String
ColorRed = Chr(27) & "[1;31m"
End Property

Private Property Get ColorYellow() As String
ColorYellow = Chr(27) & "[1;33m"
End Property

Private Property Get ColorGreen() As String
ColorGreen = Chr(27) & "[1;32m"
End Property

Private Property Get ColorBlue() As String
ColorBlue = Chr(27) & "[1;34m"
End Property

The color "resetter" is:

Private Property Get ColorReset() As String
ColorReset = Chr(27) & "[0m"
End Property

Finally, before finishing, a link that explains the ANSI color codes.
In google searching ANSI COLOR CODES or ANSI ESCAPE SEQUENCES there are
plenty of them.

http://www.dee.ufcg.edu.br/~rrbrandt/tools/ansi.html

This is a standard and I'd like to represent it in some window created
with Visual Basic. If not all the fcodes, at least the color ones.

THANKS IN ADVANCE!!!!.
Xavi.

.



Relevant Pages

  • Re: Did Borland doing well in Q4? Listen to the Earning CC
    ... AS an ANSI application you will need to change all "String" ... declarations to "ANSIString" (and Char to ANSIChar and PChar to ... Only THEN can you start thinking about how Unicode is best ... Even the specifically "ANSI" FS routines do not use ...
    (borland.public.delphi.non-technical)
  • Re: Converting a LPSTR* to BSTR* and back again.
    ... int ListItem(int itemNumber, ref string buffer, int tabbed, int ... you may consider the following code to convert BSTR ANSI (I ... // Convert an ANSI string to a BSTR ...
    (microsoft.public.vc.mfc)
  • Re: Converting a LPSTR* to BSTR* and back again.
    ... int ListItem(int itemNumber, ref string buffer, int tabbed, int ... you may consider the following code to convert BSTR ANSI (I ... // Convert an ANSI string to a BSTR ...
    (microsoft.public.vc.mfc)
  • Re: api call for tchar*
    ... > String passes an ANSI version while a ByRef passes a Unicode version. ... The row of characters is preceded by a Long ...
    (microsoft.public.vb.winapi)
  • Merry Christmas!
    ... dictionaries and every variant possibility has a separate "word" entry. ... The byte string of the "word", whose length is specified by a four ... match is found for a source byte sequence in the dictionary. ...
    (rec.arts.sf.written)

Loading