विषय पर बढ़ें

अंतिम अद्यतन:2026-10-12

C++ dbg-macro डीबगिंग मैक्रो

logging करना C++ development में एक ज़रूरी debug तरीका है, dbg-macro, Rust भाषा के dbg से प्रेरित है, और printf तथा std::cout से बेहतर macro functions उपलब्ध कराता है। इसकी मुख्य विशेषताएँ इस प्रकार हैं:

  • सुंदर रंगीन आउटपुट (जब आउटपुट interactive terminal नहीं होता, तो रंग अपने आप disable हो जाते हैं)
  • C++11 के साथ compatible, और header-only
  • basic types और STL container types के आउटपुट का समर्थन
  • फ़ाइल का नाम, line number, function का नाम और original expression आउटपुट कर सकता है

उदाहरण के लिए निम्न कोड:

#include <vector>
#include <dbg.h>

// You can use "dbg(..)" in expressions:
int factorial(int n) {
  if (dbg(n <= 1)) {
    return dbg(1);
  } else {
    return dbg(n * factorial(n - 1));
  }
}

int main() {
  std::string message = "hello";
  dbg(message);  // [example.cpp:15 (main)] message = "hello" (std::string)

  const int a = 2;
  const int b = dbg(3 * a) + 1;  // [example.cpp:18 (main)] 3 * a = 6 (int)

  std::vector<int> numbers{b, 13, 42};
  dbg(numbers);  // [example.cpp:21 (main)] numbers = {7, 13, 42} (size: 3) (std::vector<int>)

  dbg("this line is executed");  // [example.cpp:23 (main)] this line is executed

  factorial(4);

  return 0;
}

परिणाम इस प्रकार है:

68747470733a2f2f692e696d6775722e636f6d2f4e4845596b39412e706e67

इंस्टॉल करना

git clone https://github.com/sharkdp/dbg-macro
sudo ln -s $(readlink -f dbg-macro/dbg.h) /usr/include/dbg.h

इस तरह आप कहीं भी dbg.h का उपयोग कर सकते हैं।

Arch Linux पर

yay -S dbg-macro

vcpkg का उपयोग करते हुए

vcpkg install dbg-macro

अधिक जानकारी के लिए आधिकारिक documentation देखें।

संबंधित विषय

टिप्पणियाँ