dbg(…)适合printf风格调试爱好者的宏。
调试器很棒。但有时您只是没有时间或耐心来正确设置所有内容,而只是想要一种在运行时检查某些值的快速方法。
该项目提供了一个带有dbg(…)宏的单个头文件,可在您通常编写printf("…", …)或std::cout << …的所有情况下使用。但它还有一些额外的功能。
# include < dbg.h >
# include < cstdint >
# include < vector >
// You can use "dbg(..)" in expressions:
int32_t factorial ( int32_t n) {
if ( dbg (n <= 1 )) {
return dbg ( 1 );
} else {
return dbg (n * factorial (n - 1 ));
}
}
int32_t main () {
std::string message = " hello " ;
dbg (message); // [example.cpp:15 (main)] message = "hello" (std::string)
const int32_t a = 2 ;
const int32_t b = dbg ( 3 * a) + 1 ; // [example.cpp:18 (main)] 3 * a = 6 (int32_t)
std::vector< int32_t > numbers{b, 13 , 42 };
dbg (numbers); // [example.cpp:21 (main)] numbers = {7, 13, 42} (std::vector<int32_t>)
dbg ( " this line is executed " ); // [example.cpp:23 (main)] this line is executed
factorial ( 4 );
return 0 ;
}上面的代码产生以下输出(你自己尝试一下):
std::optional等的专用漂亮打印机。dbg.h标头在包含时会发出编译器警告(因此您不会忘记删除它)。为了使其实用, dbg.h标头应该可以从各种不同的地方和各种环境中轻松获得。快速而肮脏的方法是将头文件实际复制到/usr/local/include或克隆存储库并将符号链接dbg.h到/usr/local/include/dbg.h 。
git clone https://github.com/sharkdp/dbg-macro
sudo ln -s $( readlink -f dbg-macro/dbg.h ) /usr/local/include/dbg.h如果您不想对文件系统进行未跟踪的更改,请检查下面是否有适合您的操作系统或包管理器的包。
您可以从 AUR 安装dbg-macro :
yay -S dbg-macro您可以通过以下方式安装dbg-macro端口:
vcpkg install dbg-macro CMakeLists.txt
cmake_minimum_required ( VERSION 3.11) # FetchContent added in cmake 3.11
project (app) # name of executable
set (CMAKE_CXX_STANDARD 17)
# dbg-macro
include (FetchContent)
FetchContent_Declare(dbg_macro GIT_REPOSITORY https://github.com/sharkdp/dbg-macro)
FetchContent_MakeAvailable(dbg_macro)
add_executable ( ${PROJECT_NAME} main.cpp) # your source files goes here
target_link_libraries ( ${PROJECT_NAME} PRIVATE dbg_macro) # make dbg.h available main.cpp
# include < dbg.h >
int main () {
dbg ( 42 , " hello world " , false );
return 0 ;
}DBG_MACRO_DISABLE标志以禁用dbg(…)宏(即使其成为无操作)。DBG_MACRO_NO_WARNING标志以禁用“'dbg.h'标头包含在您的代码库中”警告。DBG_MACRO_FORCE_COLOR标志以强制彩色输出并跳过 tty 检查。 您可以将多个参数传递给dbg(…)宏。 dbg(x, y, z)的输出与dbg(x); dbg(y); dbg(z); :
dbg ( 42 , " hello world " , false );请注意,您必须将“不受保护的逗号”括在括号中:
dbg ( " a vector: " , (std::vector< int >{ 2 , 3 , 4 }));如果你想以十六进制、八进制或二进制表示形式格式化整数,你可以简单地将它们包装在dbg::hex(…) 、 dbg::oct(…)或dbg::bin(…)中:
const uint32_t secret = 12648430 ;
dbg (dbg::hex(secret));dbg(…)已经打印了括号中每个值的类型(参见上面的屏幕截图)。但有时您只想打印一个类型(可能是因为您没有该类型的值)。在这种情况下,您可以使用dbg::type<T>()帮助程序来漂亮地打印给定类型T 。例如:
template < typename T>
void my_function_template () {
using MyDependentType = typename std::remove_reference<T>::type&&;
dbg (dbg::type<MyDependentType>());
}要打印时间戳,您可以使用dbg::time()帮助程序:
dbg (dbg::time());如果您希望dbg(…)适用于您的自定义数据类型,您可以简单地为std::ostream&重载operator<< :
std::ostream& operator <<(std::ostream& out, const user_defined_type& v) {
out << " … " ;
return out;
}如果要修改dbg(…)打印的类型名称,可以添加自定义get_type_name重载:
// Customization point for type information
namespace dbg {
std::string get_type_name (type_tag< bool >) {
return " truth value " ;
}
}如果您想为dbg-macro做出贡献,可以通过以下方式构建测试和演示:
确保子模块是最新的:
git submodule update --init然后,使用典型的cmake工作流程。使用-DCMAKE_CXX_STANDARD=17是可选的,但建议使用,以便启用最大的功能集:
mkdir build
cd build
cmake .. -DCMAKE_CXX_STANDARD=17
make要运行测试,只需调用:
make test您可以在tests/basic.cpp中找到单元测试。
该项目的灵感来自 Rusts dbg!(…)宏。