प्रीडिफाइंड मैक्रोज़ फॉर डिबगिंग
ये विशेष मैक्रोज़ कंपाइलर द्वारा प्रदान किए जाते हैं और डिबगिंग, लॉगिंग, और डायग्नोस्टिक्स के लिए उपयोगी होते हैं।
__FILE__ मैक्रो
यह मैक्रो वर्तमान फाइल का पथ प्राप्त करने के लिए उपयोग किया जाता है। यह तब उपयोगी होता है जब हम लॉग फाइल्स जनरेट करना चाहते हैं।
उदाहरण
#include <iostream>
using namespace std;
int errorLog(const char* file, const string& msg) {
cerr << "[" << file << "] " << msg << endl;
return 0;
}
#define LOG(msg) errorLog(__FILE__, msg)
int main() {
LOG("This is a dummy error");
return 0;
}
आउटपुट
[D:\Misc C and C++ Questions\test_prog.cpp] This is a dummy error
__LINE__ मैक्रो
यह मैक्रो सोर्स फाइल में वर्तमान लाइन नंबर ढूंढ सकता है। यह लाइन नंबर एक इंटीजर वैल्यू है। जब लॉग स्टेटमेंट्स जनरेट हो रहे होते हैं तो __LINE__ उपयोगी भूमिका निभाता है।
उदाहरण
#include <iostream>
using namespace std;
int errorLog(int line, const string& msg) {
cerr << "[" << line << "] " << msg << endl;
return 0;
}
#define LOG(msg) errorLog(__LINE__, msg)
int main() {
LOG("This is a dummy error");
return 0;
}
आउटपुट
[12] This is a dummy error
__FUNCTION__ मैक्रो
यह मैक्रो वर्तमान फ़ंक्शन का नाम वापस कर सकता है। जब लॉग स्टेटमेंट्स जनरेट हो रहे होते हैं तो __FUNCTION__ उपयोगी भूमिका निभाता है।
उदाहरण
#include <iostream>
using namespace std;
int errorLog(const char* func, const string& msg) {
cerr << "[" << func << "] " << msg << endl;
return 0;
}
#define LOG(msg) errorLog(__FUNCTION__, msg)
void TestFunction() {
LOG("Send from Function");
}
int main() {
TestFunction();
LOG("This is a dummy error");
return 0;
}
आउटपुट
[TestFunction] Send from Function [main] This is a dummy error
संयुक्त लॉगिंग मैक्रो
#include <iostream>
#include <string>
using namespace std;
#define LOG(msg) \
std::cerr << "[" << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "] " << msg << '\n'
void processData() {
LOG("Starting processing");
// ...
LOG("Processing complete");
}
int main() {
LOG("Program started");
processData();
LOG("Program ended");
return 0;
}
आउटपुट
[test.cpp:25 main] Program started [test.cpp:18 processData] Starting processing [test.cpp:20 processData] Processing complete [test.cpp:27 main] Program ended
C++20: std::source_location (मॉडर्न विकल्प)
#include <source_location>
#include <iostream>
#include <string>
void log(const std::string& msg,
const std::source_location& loc = std::source_location::current()) {
std::cerr << "[" << loc.file_name() << ":" << loc.line()
<< " " << loc.function_name() << "] " << msg << '\n';
}
void processData() {
log("Processing..."); // स्वचालित कैप्चर
}
int main() {
log("Start");
processData();
log("End");
return 0;
}
मैक्रो तुलना
| मैक्रो | टाइप | कंटेंट | स्टैंडर्ड |
|---|---|---|---|
__FILE__ | स्ट्रिंग लिटरल | वर्तमान फाइल पथ | C89, C++98 |
__LINE__ | इंटीजर | वर्तमान लाइन नंबर | C89, C++98 |
__FUNCTION__ | स्ट्रिंग/वेरिएबल | वर्तमान फ़ंक्शन नाम | C99, C++11 (GCC/MSVC एक्सटेंशन) |
__func__ | स्थैतिक चर | वर्तमान फ़ंक्शन नाम | C99, C++11 |
__PRETTY_FUNCTION__ | स्ट्रिंग | सुंदर सिग्नेचर | GCC/Clang एक्सटेंशन |