in ,

sharkdp / dbg-macro, Hacker News

sharkdp / dbg-macro, Hacker News


                    

        

Build StatusBuild statusTry it onlineLicense: MIT

A macro forprintf– style debugging fans.

Debuggers are great. But sometimes you just don’t have the time and nerve to set up everything correctly and just want a quick way to inspect some values ​​at runtime.

This projects provides asingle header filewith adbg (…)macro that can be used in all circumstances where you would typically writeprintf ("…",…)orstd :: cout, but with a few extras.

Examples

#includevector>#includedbg.h>//You can use "dbg (..)" in expressions:intfactorial( (int)  n) {   if(dbg(nreturndbg( (1) );   }else{     returndbg(n *factorial(n -1));   } }intmain() {   std :: string message="hello";   dbg(message);//[example.cpp:15 (main)] message="hello" (std :: string)  constintA=(2) ;   constintb=DBG( (3)  * a)    (1) ;//[example.cpp:18 (main)] 3 * a=6 (int)  std :: vectorint>numbers {b,13,42};   dbg(numbers);//[example.cpp:21 (main)] numbers={7, 13, 42} (size: 3) (std :: vector)  dbg("this line is executed");//[example.cpp:23 (main)] this line is executed  Factorial(4);    return0; }

The code above produces this output (try it yourself):

dbg(…) macro output

Features

    Easy to read, colorized output (colors auto-disable when the output is not an interactive terminal)

  • Prints file name, line number, function name and the original expression
  • Adds type information for the printed- out value
  • Specialized pretty-printers for containers, pointers, string literals, enums,std :: optional, etc.
  • Can be used inside expressions
  • Thedbg.hheader issues a compiler warning when included (so you don’t forget to remove).
  • C 11 compatible

Installation

To make this actually usable, thedbg.hheader should to be available from all kinds of different places and in all kinds of environments. The quick & dirty way is to actually copy the header file to/ usr / includeor to clone the repository and symlinkdbg.hto/ usr / include / dbg.h.

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

Ideally, if this turns out to be a good idea, we would ship packages for various distributions so you don’t have to make untracked changes to your filesystem.

Configuration

    Set theDBG_MACRO_DISABLEflag to disable thedbg (…)macro (ie to make it a no-op

  • Set theDBG_MACRO_NO_WARNINGflag to disable the“” dbg.h ‘header is included in your code base”Warnings.

If you wantdbg (…)to work for your custom datatype, you can simply overloadoperatorforstd :: ostream &:

std :: ostream &operatorconstuser_defined_type & v) {   out"";   returnout; }

If you want to modify the type name that is printed bydbg (…), you can add a customget_type_nameoverload:

//Customization point for type informationnamespacedbg_macro{     std :: stringget_type_name(type_tagbool>) {         return"truth value";     } }

Acknowledgement

This project is inspired by Rustsdbg! (…)macro.

  

Brave Browser
Read More
Payeer

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Maduro Mandates Petro-Financed Public Housing Scheme in Venezuela – CCN.com, Crypto Coins News

Maduro Mandates Petro-Financed Public Housing Scheme in Venezuela – CCN.com, Crypto Coins News

Horror film Jennifer’s Body bombed 10 years; now it’s a cult classic, Ars Technica

Horror film Jennifer’s Body bombed 10 years; now it’s a cult classic, Ars Technica