#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#define FILENAME_LEN 100
#define MAXLINE 1024
#define MAXBYTES 50
static char c_FileName[FILENAME_LEN];
static int i_FileLineNum;
//Self-define a function which can print the name and line-number of the source file calling it.
#define PRINT Get_File_Line( __FILE__, __LINE__ );\
F_vsnprintf
/**
* Get the linenum and filename of the source file.
* @Para-in: p_FileName: The name of the source file.
* @Para-in: i_FileLine: The line-number of the source file.
*/
void Get_File_Line( char *p_FileName, int i_FileLine )
{
strcpy( c_FileName, p_FileName );
i_FileLineNum = i_FileLine;
return;
}
/**
* Print the arguments according to the first argument, name as fmt.
*/
void F_vsnprintf( char *fmt, ... )
{
char buf[MAXLINE] = {0x00};
snprintf( buf, MAXBYTES, "[%s:%d] ", c_FileName, i_FileLineNum );
va_list ap;
va_start( ap, fmt );
vsnprintf( buf+strlen(buf), MAXLINE, fmt, ap );
va_end( ap );
strcat( buf, "\n" );
fflush( stdout );
fputs( buf, stderr );
fflush( NULL );
return;
}
int main( int argc, char **argv )
{
PRINT( "[%s]", "Hello." );
PRINT( "[%s %s]", "Hello", "world." );
return 0;
}
|