In the project development project, if you can determine which file under the
which line under which function is wrong--lock error, that would be good, the article is for this purpose.
First, let's take a look at the file default output information function:
File information functions:
Copy Code code as follows:
printf ("Line:%d\n", __line__); Current number of rows
printf ("FileName:%s\n", __file__); Current file name
printf ("Function:%s\n", __function__); Current function
printf ("Time:%s\n", __time__); Current time
printf ("Date:%s\n", __date__); Current date
Output:
Line:10
Filename:test.c
Function:main.c
Time:14:13:51
Date:oct 13 2012
The theory is enough, let's see how to lock the bug:
First, the source file:
Copy Code code as follows:
[Root@localhost for_test]# Cat erroutput.c
#include <stdio.h>
#include <assert.h>
#define _DEBUG (msg ...) printf ("[%s,%s,%d]=>", __file__, __function__, __line__); printf (msg);p rintf ("\ r \ n")
#define _ERROR (msg ...) printf ("[ERROR:%s,%d]=>", __file__, __line__);p rintf (msg); printf ("\ r \ n")
#define _ASSERT (exp) \
do {\
if (!) ( EXP)) {\
printf ("[%s]", #exp);p rintf ("\ r \ n"); \
ASSERT (exp); \
}\
} while (0)
int main (void)
{
char *p = NULL;
_DEBUG ("debug!");
_error ("error!");
_assert (NULL!= p);
return 0;
}
Second, the output:
Copy Code code as follows:
[Root@localhost for_test]# gcc erroutput.c
[Root@localhost for_test]#./a.out
[Erroutput.c,main,]=>debug!
[Error:erroutput.c, 18]=>error!
[NULL!= P]
A.out:erroutput.c:19:main:assertion ' ((void *) 0)!= p ' failed.
Has abandoned
ti Processing:
Copy Code code as follows:
#ifdef DEBUG
#define DBG (FMT, args ...) printf ("Debug" FMT, # #args)//# #运算符用于把参数连接到一起. The preprocessor appears in # #两侧的参数合并成一个符号.
#else
#define DBG (FMT, args ...)
#endif
#define ERR (FMT, args ...) printf ("Error" FMT, # #args)
[Root@localhost for_test]# Cat Debug_err.c
#include <stdio.h>
#define DEBUG
int main (void)
{
DBG ("xxxx\n");
ERR ("xxxx\n");
return 0;
}
[Root@localhost for_test]#./a.out
Error xxxx
#ifdef __debug
#define DBG (FMT, args ...) fprintf (stderr, "Encode Debug:" Fmt, # # args)
#else
#define DBG (FMT, args ...)
#endif
#define ERR (FMT, args ...) fprintf (stderr, "Encode Error:" Fmt, # # args)