#define MY_LOG (level, FMT, args ...) do{ if (bit_on (Debug_flag,level)) { printf ("[%s]:", __function__); printf (FMT, # #args); } } while (0)
Of course we also need to define a set of debug level methods, my idea is this:
- Using Bit-map idea, define a number of unsigned int, and each one represents a level, such as a number of unsigned int can represent 32 levels
- Define the corresponding Function/macro definition to set the corresponding bit
Based on these ideas, you can set up the following series of actions:
#define PRESENT_BIT32 (x) (((UInt32) ((UInt32) 1<< (x))) #define BIT_ON32 (M, b) ((( m) & Present_bit32 (b))! = 0) #define SET_BIT32 (M, b) ((m) |= Present_bit32 (b)) #define CLEAR_BIT32 (M, b) ((m) &= ~present_bit32 (b))
Explain:
- Present_bit32 (x): position of the bit corresponding to level
- Bit_on32 (M, b): Determine whether a person is 1
- Set_bit32 (M, b): Set pointing to 1
- Clear_bit32 (M, b): will be specified as set to 0
We used a macro definition to define a log with a level, so how do we use these logs? Here's how to use it:
if (pthread_create (&thread2_id, NULL, (void*) msg_sender2, null)) { My_log (FATAL, "create handler thread fail!\n "); return-1; }my_log (DEBUG, "I have recieved a message!\n"); My_log (DEBUG, "msgtype:%d msg_src:%d dst:%d\n\n",msg->hdr.msg_type,msg->hdr.msg_src,msg-> HDR.MSG_DST);
Of course, you need to open the corresponding log switch before use, such as I want to see the debug log, can do this: Set_bit (Debug_flag, debug); This way we define the debug level of the system as Debug. Say another print trick: Give your print color! printf ("\033[46;31m[%s:%d]\033[0m" #fmt "errno=%d,%m\r\n", __func__, __line__, # #args, errno, errno);
The above printf when the Linux command line print out the color of the font, easy to distinguish between different kinds of debugging information, just add some color code, for example: here 46 for the background , 31 for the color of the font.
Using ASCII code is a color call with the following format:
\033[, M ... \033[0m
Which of the following "\033[0m" is the end of the previous color loading, restore to the end of the original background color and font color, you can change the following to try:
#define DEBUG_ERR (FMT, args ...) printf ("\033[46;31m[%s:%d]\033[40;37m" #fmt "errno=%d,%m\r\n", __func__, __line__, # # Args, errno, errno);
the color values for ASCII code are listed below:
Word Background color range : 49----Color :-----------
40: Black 30: Black
41: Crimson 31: Red
42: Green 32: Green
43: Yellow 33: Yellow
44: Blue 34: Blue
45: Purple 35: Purple
46: Dark green 36: Dark green
47: White 37: White
The memory color format is too troublesome, we make it into a macro definition, so it is much more convenient to use later.
#define NONE "\e[0m" #define BLACK "\e[0;30m" #define L_black "\e[1;30m" #define RED "\e[0;31m" #define l_red "\e[1;31m" #define GREEN "\e[0;32m" #define L_gre EN "\e[1;32m" #define BROWN "\e[0;33m" #define YELLOW "\e[1;33m" #define BLUE "\e[0;34m" #define L_blue "\e[1;34m" #define PURPLE "\e[0;35m" #define L_purple "\e[1;35m" #define CYAN "\e[0;36m" #define L_cyan "\e[1;36m" #define GRAY "\e[0;37m" #define WHITE "\e[1;37m" #define BOLD "\e[1m" #define Underline "\e[4m" #define BLINK "\e[5m" #define REVERSE "\e[7m" #define HIDE "\e[8m" #define CLEAR "\E[2J" #define Clrline "\r\e[k"//or "\e[1k\r" #define DEBUG_ERROR (FMT, args ...) do{print F (RED "[%s]:" NONE, __funcTION__); printf (FMT, # #args); }while (0);
Effect: So, I recommend fatal a class of fatal error level log with the highlight color, and once such errors occur we can be aware of the first time.
Linux Programming Custom Band-level log