Different types of logs are displayed in different colors on the Windows console.

Source: Internet
Author: User

The function "setconsoletextattribute" is used to implement different types of logs and print the log Content in different colors.

BOOL SetConsoleTextAttribute( HANDLE hConsoleOutput, // handle to console screen buffer 

            WORD wAttributes // text and background colors);

This function is valid for logs after the call.

HandleHconsoleoutput-- Output window handle

WordWattributes-- Attribute settings can be used to connect each attribute component through logic or operator (|)

++

Foreground_blue text blue

Foreground_green text green

Foreground_red text red

Foreground_intensity highlight text color (increase brightness)

Background_blue background blue

Background_green background green

Background_red background red

Background_intensity highlight background color (increase brightness)

Common_lvb_grid_horizontal

Common_lvb_underscore: underline printed text


Different colors can be produced by combining these attributes (the principle of adding three primary colors)

For example: foreground_intensity | foreground_green | foreground_blue highlight

(1) Add three primary colors

(2) Foreground view (Foreground) And background color (Background)

Foreground color: Specifies the color of the paint brush.

Background Color: The color of the canvas, which is the background color of the console.

Sample Code for printing logs on the console:

 1 # pragma once
2
3 typedef Enum errorlev
4 {
5 trace_lev= 0,
6 info_lev,
7 warnning_lev,
8 error_lev,
9 fault_lev
10} errorlev;
11
12 const char * err_headers [] =
13 {
14 "Trace :",
15 "info :",
16 "warnning :",
17 "error :",
18 "fault :"
19 };
20
21
22
23 void println (const char * MSG, errorleverr_lev= error_lev)
24 {
25 intptr_t handle = (intptr_t) getstdhandle (std_output_handle );
26 switch (err_lev)
27 {
28 case trace_lev:
29 // text green
30 setconsoletextattribute (handle) handle, foreground_green );
31 break;
32 case info_lev:
33 // text cyan
34 setconsoletextattribute (handle) handle, foreground_green | foreground_blue );
35 break;
36 case warnning_lev:
37 // text yellow
38 setconsoletextattribute (handle) handle, foreground_red | foreground_green );
39 break;
40 case error_lev:
41 // text in red
42 setconsoletextattribute (handle) handle, foreground_red | foreground_blue );
43 break;
44 case fault_lev:
45 // text red
46 setconsoletextattribute (handle) handle, foreground_red );
47 break;
48 default:
49 // white text
50 setconsoletextattribute (handle) handle, foreground_red | foreground_green | foreground_blue );
51 break;
52}
53 printf ("% S % s \ n", err_headers [err_lev], MSG );
54}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.