printf 列印 檔名 函式名 行號

2021-08-23 14:18:44 字數 1650 閱讀 8822

輸出原始檔的標題,目前執行行的行數,編譯的日期,編譯的時間。

linux下實現

#include

int main()

printf("當前**行:%d\n", __line__); 

printf("當前源**檔名:%s\n", __file__); 

printf("當前檔案編譯的日期%s\n", __date__); 

printf("當前檔案編譯的時間%s\n", __time__); 

return 0;

windows下實現

#include

int main()

printf("當前**的行數:%d\n", __line__);

printf("當前原始檔的檔名:%s\n", __file__);

printf("當前原始檔的編譯日期:%s\n", __date__);

return 0;

上面用linux和windows兩種環境進行了測試,他們的函式還是有區別的,例如wingdows下有 __timestamp__這個變數,而linux下沒有。還有__file__這個變數在linux下的執行結果直接是檔名,而windows下這個變數的結果為其具體的路徑。

以上的這兩個例子我覺得主要運用在對原始檔的除錯中,如果使用者的程式在執行過程中出錯,可以將這些出錯資訊通過e-mail傳送給程式設計師,程式設計師能夠迅速的定位**的出錯位置。

使用巨集__file__               檔名

__line__               行號

__function__      函式名

即可。#define debug_msg(fmt,...) printf("%s[%d]:"fmt,__file__,__line__,##__va_args__)

#define printf_my(fmt, ...) printf("%s %s %s %s %d:"fmt, __file__, __function__, __date__, __time__, __line__, ##__va_args__)

int printf_my(const char *fmt, ...)

#ifdef debug

printf("%s %s %s %s %d:"fmt, __file__, __function__, __date__, __time__, __line__, ##__va_args__)

#endif

#include "stdarg.h"

int my_printf (const char *format, ...)

//引用標頭檔案

debug.h

#ifndef __debug__h_

#define __debug__h_

#include

#define debug

#ifdef debug

#define printf_debug(fmt, ...) printf("%s %s %s %s %d:"fmt, __file__, 

__function__, __date__, __time__, __line__, ##__va_args__)

#else

#define printf_debug(fmt, ...)

#endif

#endif

C語言 列印呼叫函式的行號,檔名 函式名用於除錯

c語言為了除錯方便,經常需要在故障處輸出問題描述,此時能夠獲得呼叫函式的相關資訊,就會很方面找到問題所在,現在將實現方法描述如下 第一種方法 void debugout void 當該函式被呼叫時,輸出的是本函式的檔案檔名 函式名 函式所在行號,無法定位問題所在。方法二 define debug o...

QT 列印當前檔名,行號和函式名稱

定義 include define mydebug qdebug tr 檔名 file tr 行號 line tr 函式名 function 在函式中執行mydebug 即可 void mainwindow doactionnew slot 執行結果 檔名 mainwindow.cpp 行號 39 ...

Qt下編寫日誌模組(同時記錄檔名 函式名 行數)

說來慚愧,一直以來,我都是使用乙個單例模式來完成日誌模組,具體操作就是呼叫單例的寫檔案函式,自己編輯日誌內容,記錄在日誌檔案裡。這種做法不利於查詢除錯。而使用qt內建的qinstallmessagehandler函式,重新編輯除錯函式的輸出內容才是簡單高效的做法。名字很高大上,其實大家都在使用的qd...