এখানে আমরা C++ এ __FILE, __LINE__ এবং __FUNCTION__ কি কি তা দেখব।
__FILE__
বর্তমান ফাইলের পাথ পেতে এই ম্যাক্রো ব্যবহার করা হয়। যখন আমরা লগ ফাইল তৈরি করতে চাই তখন এটি কার্যকর। নিম্নলিখিত কোডটি এর কার্যকারিতা ব্যাখ্যা করবে।
উদাহরণ
#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
The __LINE__
এই ম্যাক্রো সোর্স ফাইলে বর্তমান লাইন নম্বর খুঁজে পেতে পারে। এই লাইন সংখ্যা একটি পূর্ণসংখ্যা মান. যখন লগ স্টেটমেন্ট তৈরি হয় তখন __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__
এই ম্যাক্রো বর্তমান ফাংশন ফিরিয়ে দিতে পারে। যখন লগ স্টেটমেন্ট তৈরি হয় তখন __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