vsnprintf字符串格式化输出实例:日志中打印程序名和行号

365bet网上娱乐场 📅 2026-02-04 18:27:45 👤 admin 👀 1376 ❤️ 370
vsnprintf字符串格式化输出实例:日志中打印程序名和行号

/**

* @FileName vsnprintf_name_line.c

* @Describe A simple example for using vsnprintf to print the name and line-num of source file in linux.

* @Author vfhky 2016-03-13 23:28 https://typecodes.com/cseries/vsnprintffilenameline.html

* @Compile gcc vsnprintf_name_line.c -o vsnprintf_name_line

*/

#include

#include

#include

#define FILENAME_LEN 100

#define MAXLINE 1024

#define MAXBYTES 50

static char c_FileNameFILENAME_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 bufMAXLINE = {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;

}

🍵 相关养生推荐

这次双倍大小铁,圆我多年橙武梦
365bet网上平台

这次双倍大小铁,圆我多年橙武梦

📅 12-09 👀 9756
【路畅导航】价格表
365bet网上平台

【路畅导航】价格表

📅 09-29 👀 1615
一定的意思、解释和含义
365bet网上平台

一定的意思、解释和含义

📅 10-15 👀 802
剃刀是什么_生肖
必发365app官网

剃刀是什么_生肖

📅 12-08 👀 3021