C++__FILE__, __FUNCTION__, __LINE__作用及使用

__FILE__, __FUNCTION__, __LINE__ 主要是用來logging和debugging的.
帶#條件編譯的文章:
C++條件編譯(#define等)
舉例:

#include<iostream>
#include <stdio.h>
#define __DEBUG__  
#ifdef __DEBUG__  
#define DEBUG(format,...) printf("File: "__FILE__", Line: %05d: "format"\n", __LINE__, ##__VA_ARGS__)  
#else  
#define DEBUG(format,...)  
#endif  
int main() {  
    char str[]="Hello World";  
    DEBUG("A ha, check me: %s",str);  
    return 0;  
}

輸出:

File: test.cpp, Line: 00011: A ha, check me: Hello World

參考資料:
__FILE__,__LINE__
What are__FILE__, __LINE__, and __FUNCTION__in C++

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章