FILE__,__LINE__,FUNCTION__ implementation Code tracking (reprint)

Source: Internet
Author: User
Tags numeric value volatile

root@xuanfei-desktop:~/cpropram/2# cat global.h//header file
#ifndef Clobal_h
#define Global_h
#include <stdio.h>
int Funca (void);
int funcb (void);
#endif
root@xuanfei-desktop:~/cpropram/2# cat FUNCA.C//function A
#include "global.h"
int Funca (void)
{
printf ("This is function/n");
return 0;
}
root@xuanfei-desktop:~/cpropram/2# cat FUNCB.C//function B
#include "global.h"
int funcb (void)
{
printf ("This is function/n");
return 0;
}
root@xuanfei-desktop:~/cpropram/2# gcc-wall funca.c FUNCB.C MAIN.C//Federated compilation
root@xuanfei-desktop:~/cpropram/2#./a.out//Run
This is main
This isfunction
This is main
This is function
This is main
The same result is difficult to see there is a mistake, below we use __file__,__line__,__function__ to add code to see what the difference is.
Add __file__,__line__,__function__ to the MAIL.C.
root@xuanfei-desktop:~/cpropram/2# Cat Main.c
#include "global.h"
int main (int argc, char **argv)
{
printf ("%s (%d)-%s:this is main/n", __file__,__line__,__function__);
Funca ();
printf ("%s (%d)-%s:this is main/n", __file__,__line__,__function__);
FUNCB ();
printf ("%s (%d)-%s:this is main/n", __file__,__line__,__function__);
return 0;
}
root@xuanfei-desktop:~/cpropram/2# Gcc-wall funca.c FUNCB.C main.c
root@xuanfei-desktop:~/cpropram/2#./a.out
MAIN.C (4)-main:this is main
This is function
MAIN.C (6)-main:this is main
This is function
MAIN.C (8)-main:this is main
The results above MAIN.C (4)-main:this is main represents the this is main that is printed in the fourth line of the MIAN.C source code in the main function
In that case, it would be convenient for programmers to make mistakes in their own programs.
To make it easier to use, we can make a macro definition in global.h code
root@xuanfei-desktop:~/cpropram/2# Cat Global.h
#ifndef Clobal_h
#define Global_h
#include <stdio.h>
int Funca (void);
int funcb (void);
#define DEBUGFMT "%s (%d)-%s"
#define Debugargs __file__,__line__,__function__
#endif
root@xuanfei-desktop:~/cpropram/2# Cat FUNCA.C
#include "global.h"
int Funca (void)
{
printf (debugfmt "This is function/n", Debugargs);
return 0;
}
root@xuanfei-desktop:~/cpropram/2# Cat FUNCB.C
#include "global.h"
int funcb (void)
{
printf (debugfmt "This is function/n", Debugargs);
return 0;
}
root@xuanfei-desktop:~/cpropram/2# Cat Main.c
#include "global.h"
int main (int argc, char **argv)
{
printf (debugfmt "This is main/n", Debugargs);
Funca ();
printf (debugfmt "This is main/n", Debugargs);
FUNCB ();
printf (debugfmt "This is main/n", Debugargs);
return 0;
}
root@xuanfei-desktop:~/cpropram/2# Gcc-wall funca.c FUNCB.C main.c
root@xuanfei-desktop:~/cpropram/2#./a.out
MAIN.C (4)-mainthis is main
FUNCA.C (4)-funca this is function
MAIN.C (6)-main this is main
FUNCB.C (4)-FUNCB this is function
MAIN.C (8)-main this is main
root@xuanfei-desktop:~/cpropram/2#
This is the simple implementation of code tracking and debugging by defining __FILE__,__LINE__,FUNCTION__ macros:
The following is a header file that can be used for debugging
#ifndef _gold_debug_h
#define _gold_debug_h

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif/* __cplusplus * *

#define Gi_debug

#ifdef Gi_debug

#define GI_DEBUG_POINT () printf ("/n/n[file:%s line:%d] fun:%s/n/n", __file__, __line__, __function__)
#define DBG_PRINTF (Arg ...) printf (ARG);

#define GI_ASSERT (expr)/
do{/
if (!) ( Expr)) {/
printf ("/nassert failed at:/n >file name:%s/n >function:%s/n >line No.:%d/n >condition:%s/n",/
__file__,__function__, __line__, #expr);
} /
}while (0);

/* Debug macros, for pause */
#define Gi_debug_pause ()/
Do/
{                /
Gi_debug_point (); /
printf ("Pause for debug, press ' Q ' to exit!/n"); /
char c; /
while ((c = GetChar ()))/
{              /
if (' q ' = = c)/
{            /
GetChar (); /
Break /
}            /
}              /
}while (0);
#define GI_DEBUG_PAUSE_ARG (ARG ...) /
Do/
{                /
printf (ARG); /
Gi_debug_pause ()/
}while (0);

#define GI_DEBUG_ASSERT (expression)/
if (!) ( expression))/
{                                  /
printf ("[assert],%s,%s:%d/n", __file__, __function__, __line__);
Exit (-1); /
}
#else
#define Gi_assert (expr)
#define Gi_debug_pause ()
#define GI_DEBUG_PAUSE_ARG (ARG ...)
#define GI_DEBUG_POINT ()
#define DBG_PRINTF (Arg ...)
#define GI_DEBUG_ASSERT (expression)

#endif

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif/* __cplusplus * *

#endif

C language Common macro definition

01: Prevent a header file from being repeatedly contained
#ifndef Comdef_h
#define Comdef_h
Header file Contents
#endif
02: Redefine some types to prevent different types of platform and compiler, resulting in a difference in the number of bytes, easy to migrate.
typedef unsigned char Boolean; /* Boolean value type. */
typedef unsigned long int uint32; /* Unsigned bit value * *
typedef unsigned short uint16; /* Unsigned bit value */
typedef unsigned char uint8; /* Unsigned 8 bit value */
typedef signed Long int int32; /* Signed bit value * *
typedef signed short Int16; /* Signed bit value */
typedef signed CHAR int8; /* Signed 8 bit value */
The following are not recommended for use
typedef unsigned char byte; /* Unsigned 8 bit value type. */
typedef unsigned short word; /* unsinged bit value type. */
typedef unsigned long DWORD; /* Unsigned bit value type. */
typedef unsigned char uint1; /* Unsigned 8 bit value type. */
typedef unsigned short Uint2; /* Unsigned bit value type. */
typedef unsigned long uint4; /* Unsigned bit value type. */
typedef signed Char Int1; /* Signed 8 bit value type. */
typedef signed short Int2; /* Signed bit value type. */
typedef long int int4; /* Signed bit value type. */
typedef signed Long SINT31; /* Signed bit value * *
typedef signed short sint15; /* Signed bit value */
typedef signed Char SINT7; /* Signed 8 bit value */
03: Get a Byte or word on the specified address
#define MEM_B (x) (* ((BYTE *) (x))
#define MEM_W (x) (* (Word *) (x))
04: Find the maximum and minimum value
#define MAX (X,y) (((x) > (y))? (x): (y))
#define MIN (X,y) (((x) < (y))? (x): (y))
05: Get the offset of a field in the structure body (struct)
#define FPOS (Type,field) ((DWORD) & ((type *) 0)->field)
06: Get the number of bytes occupied by field in a struct
#define FSIZ (Type,field) sizeof ((type *) 0)->field)
07: Converts two bytes to a word in the LSB format
#define FLIPW (((Word) (Ray) [0]) * 256) + (ray) [1])
08: Converts a word to two bytes in the LSB format
#define FLOPW (Ray,val) (Ray) [0] = ((val)/256); (Ray) [1] = ((val) & 0xFF)
09: Get the address of a variable (word width)
#define B_PTR (Var) ((BYTE *) (void *) & (Var))
#define W_PTR (VAR) (word *) (void *) & (Var))
10: Get a high and low byte of a word
#define WORD_LO (XXX) ((byte) (WORD) (XXX) & 255)
#define WORD_HI (XXX) ((byte) (WORD) (XXX) >> 8)
11: Returns a multiple of the nearest 8 that is larger than X
#define RND8 (x) (((x) + 7)/8) * 8)
12: Converts a letter to uppercase
#define UPCASE (c) ((c) >= ' A ' && (c) <= ' Z ')? ((c)-0x20): (c))
13: Determine whether the character is a number of 10 values
#define DECCHK (c) ((c) >= ' 0 ' && (c) <= ' 9 ')
14: Determine whether the character is a number of 16 values
#define HEXCHK (c) ((c) >= ' 0 ' && (c) <= ' 9 ') ((c) >= ' A ' && (c) <= ' F ')
((c) >= ' A ' && (c) <= ' F '))
15: One way to prevent overflow
#define INC_SAT (Val) (Val= (Val) +1> (val))? (val) +1: (val))
16: Returns the number of elements in an array
#define ARR_SIZE (a) (sizeof ((a))/sizeof ((a[0)))
17: Returns an unsigned number N-tailed value Mod_by_power_of_two (x,n) =x% (2^n)
#define Mod_by_power_of_two (Val, mod_by) ((DWORD) (VAL) & (DWORD) (MOD_BY)-1)
18: For IO space mapping in the storage space structure, input and output processing
#define INP (Port) (* (Volatile byte *) (port))
#define INPW (Port) (* (volatile word *) (port))
#define INPDW (Port) (* (volatile DWORD *) (port))
#define OUTP (Port,val) (* (Volatile byte *) (Port) = ((Byte) (val))
#define OUTPW (Port, Val) (* (volatile word *) (port) = ((Word) (val))
#define OUTPDW (Port, Val) (* (volatile DWORD *) (Port) = ((DWORD) (Val))
19: Use some macro to track debugging
The ANSI standard describes five predefined macro names. They are:
__line__
__file__
__date__
__time__
__stdc__
C + + also defines the __cplusplus
If the compiler is not standard, it may support only a few of the macro names above, or it is not supported at all. Remember that the compiler may also provide other predefined macro names.
__LINE__ and __FILE__ macros indicate that, #line指令可以改变它的值, simply, at compile time, they contain the current number of lines and file names for the program.
The __DATE__ macro directive contains a string of months/days/years, indicating the date when the source file was translated to code.
The __TIME__ macro instruction contains the time that the program was compiled. The time is represented by a string, in the form of: minute: sec
The meaning of the __STDC__ macro directive is defined at compile time. In general, if __STDC__ is already defined, the compiler will accept only standard C + + code that does not contain any non-standard extensions. If the implementation is standard, the macro __stdc__ contains a decimal constant of 1. If it contains any other number, the implementation is nonstandard.
__cplusplus is defined by a compiler consistent with standard C + + as a numeric value containing at least 6. Compilers that are inconsistent with standard C + + will use a value of 5 bits or less.
You can define macros, such as:
When _DEBUG is defined, the output data information is in line with the file
#ifdef _DEBUG
#define DEBUGMSG (msg,date) printf (msg);p rintf ("%d%d%d", Date,_line_,_file_)
#else
#define DEBUGMSG (Msg,date)
#endif
20: Macro definition prevents errors using parentheses contained.
For example:
Problematic definition: #define Dump_write (ADDR,NR) {memcpy (BUFP,ADDR,NR); BUFP + + nr;}
Definition to be used: #difne do (a,b) do{a+b;a++} while (0)
For example:
if (addr)
Dump_write (ADDR,NR);
Else
Do_somethong_else ();
The macro expands to become like this:
if (addr)
{memcpy (BUFP,ADDR,NR); bufp = nr;};
Else
Do_something_else ();
When GCC encounters the ";" before else, it thinks that the IF statement has ended, so that the subsequent else is not in the IF statement. The definition of do{} while (0) is not a problem under any circumstances. Instead #difne do (a,b) do{a+b;a++} The while (0) definition does not make an error under any circumstances.

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.