在这里,我们将看到C ++中的__FILE,__ LINE__和__FUNCTION__。
此宏用于获取当前文件的路径。当我们要生成日志文件时,这很有用。以下代码将说明其功能。
#include<iostream>
using namespace std;
int errorLog (const char* file, const std::string& msg){
cerr << "[" << file << "] " << msg << endl;
}
#define LOG( msg ) errorLog( __FILE__, msg )
main() {
LOG("This is a dummy error");
}输出结果
[D:\Misc C and C++ Questions\test_prog.cpp] This is a dummy error
该宏可以在源文件中找到当前行号。该行号是一个整数值。当生成日志语句时,__ LINE__会发挥一些有用的作用。请参阅以下示例以了解主意。
#include<iostream>
using namespace std;
int errorLog (int line, const std::string& msg){
cerr << "[" << line << "] " << msg << endl;
}
#define LOG( msg ) errorLog( __LINE__, msg )
main() {
LOG("This is a dummy error");
}输出结果
[12] This is a dummy error
该宏可以返回当前函数。当生成日志语句时,__ FUNCTION__会发挥一些有用的作用。请参阅以下示例以了解主意。
long double rintl(long double argument)
#include<iostream>
using namespace std;
int errorLog (const char* func, const std::string& msg){
cerr << "[" << func << "] " << msg << endl;
}
#define LOG( msg ) errorLog( __FUNCTION__, msg )
void TestFunction(){
LOG("Send from Function");
}
main() {
TestFunction();
LOG("This is a dummy error");
}输出结果
[TestFunction] Send from Function [main] This is a dummy error