TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: A dbg(...) macro for C++

162 pointsby sharkdpover 5 years ago

16 comments

the_dukeover 5 years ago
This seems very much inspired by the `dbg` macro recently introduced in Rust. [1]<p>Which in turn was inspired by Haskell s `Debug.Trace` functions [2].<p>It&#x27;s definitely a convenient tool if you can&#x27;t use a debugger or want to follow more complex interactions.<p>[1]<a href="https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;macro.dbg.html" rel="nofollow">https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;macro.dbg.html</a><p>[2] <a href="https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Debug-Trace.html" rel="nofollow">https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Debug...</a>
评论 #21035080 未加载
评论 #21035070 未加载
评论 #21034986 未加载
评论 #21035067 未加载
评论 #21035333 未加载
asahover 5 years ago
Would be nice to offer runtime enable&#x2F;disable. Out of curiosity, I wrote a little benchmark and submitted as a PR: <a href="https:&#x2F;&#x2F;github.com&#x2F;sharkdp&#x2F;dbg-macro&#x2F;pull&#x2F;57" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sharkdp&#x2F;dbg-macro&#x2F;pull&#x2F;57</a><p>The key idea is that modern CPUs dynamically branch-predict-away if-statements that are rarely&#x2F;never invoked, such as a dynamically disabled dbg() call.<p>With the performance difference magnified 10x by loop unrolling, this is what I&#x27;m seeing on my Mac laptop:<p>830000000 iterations in 3 secs = 2.76667e+08 iters&#x2F;sec (compiled-out). 840000000 iterations in 3 secs = 2.8e+08 iters&#x2F;sec (dynamically disabled).<p>And this is the worst case, where the whole program does nothing but call dbg() - real world programs contain lots of other real work, drowning the minute difference in performance, i.e. in a real program I doubt you&#x27;d see even 0.1% total performance difference, littering it with dynamic dbg() statements.<p>p.s. my C&#x2F;C++ is pretty rusty - feedback welcome, but pls be kind.
评论 #21035467 未加载
评论 #21036086 未加载
s_gourichonover 5 years ago
I wrote and use regularly similar macros in C++ context.<p>They are less advanced on some areas, but more advanced on some.<p>Particularly nice features:<p>* a structured log indented by stack trace depth (fully portable, put a simple macro at function start, will log start and any exit)<p>* log any expression: `somemacro(foo.bar())` will log `foo.bar() = 42`<p>* log scopes like functions<p>* with possibility to jump to source code line on one click or keypress<p>* browse the log with structured jumps (like step into function vs advance one line, back and forth)<p>...just by generating a text format that is parsed by common pre-existing tools (namely emacs compilation-mode and a few other short settings).<p>I&#x27;ve been planning to share that for a moment. It will eventually appear on <a href="https:&#x2F;&#x2F;github.com&#x2F;fidergo-stephane-gourichon?tab=repositories" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;fidergo-stephane-gourichon?tab=repositori...</a><p>I wrote a similar set of macros for C, see my comment <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21071497" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21071497</a> on eerimoq&#x27;s &quot;Show HN&quot; <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21040649" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21040649</a>
fabrice_dover 5 years ago
Mozilla has something similar in Gecko&#x27;s codebase, inspired by the Rust macro: <a href="https:&#x2F;&#x2F;searchfox.org&#x2F;mozilla-central&#x2F;source&#x2F;mfbt&#x2F;DbgMacro.h" rel="nofollow">https:&#x2F;&#x2F;searchfox.org&#x2F;mozilla-central&#x2F;source&#x2F;mfbt&#x2F;DbgMacro.h</a>
评论 #21039020 未加载
hellofunkover 5 years ago
I wrote something similar a couple years ago, except my macro automatically compiled to a no op if compiled in release mode, which is very convenient for switching back-and-forth to the testing.
评论 #21035902 未加载
bt848over 5 years ago
This is neat. The obvious comparison is glog&#x27;s DLOG feature.
评论 #21038380 未加载
rightbyteover 5 years ago
Oh ... this is really neat. I like how you can wrap an subexpression or prettyprint vectors.<p>When the debugger is too cumbersome to use good old prints is always a nice fallback.
DFHippieover 5 years ago
The name reminds me of <a href="https:&#x2F;&#x2F;metacpan.org&#x2F;pod&#x2F;DBG" rel="nofollow">https:&#x2F;&#x2F;metacpan.org&#x2F;pod&#x2F;DBG</a>, which, full disclosure, I wrote. Not that it&#x27;s nearly as cool or likely to be used by anyone, but ... it has a superficial resemblance.
rat87over 5 years ago
I did one of these for C(or at least gcc&#x2F;llvm c) but it&#x27;s a lot harder to abstract over types in c so it&#x27;s a bit more limited<p><a href="https:&#x2F;&#x2F;github.com&#x2F;rtaycher&#x2F;debug_print" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rtaycher&#x2F;debug_print</a>
pnakoover 5 years ago
It looks pretty cool. I agree that installing the header in &#x2F;usr&#x2F;include is a good idea.
WhiteSageover 5 years ago
Nice idea! I would appreciate a warning from the author specifying whether dbg evaluates its argument twice, as if so one must be careful with side effects when using it.
评论 #21036092 未加载
maurodelazeriover 5 years ago
I was thinking about writing a lib like this... really useful
halfer53over 5 years ago
It would be great if we can see similar things in Java or golang
israrkhanover 5 years ago
would be nice if we had something similar for C...
评论 #21040932 未加载
97b683f8over 5 years ago
in lisp this would be<p><pre><code> (defmacro dbg [expr] (println &#x27;~expr ~expr))</code></pre>
评论 #21036346 未加载
einpoklumover 5 years ago
This feels like too much of a &quot;ricer&quot; feature, at least the way it&#x27;s implemented.<p>Instead, you want:<p>* A pretty-printer library (possibly single-header) * A function for obtaining the typename; see:<p><pre><code> https:&#x2F;&#x2F;stackoverflow.com&#x2F;q&#x2F;35941045&#x2F;1593077 https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;56766138&#x2F;1593077 for a constexpr approach. </code></pre> and it won&#x27;t hurt to have:<p>* A logging library with log levels (so that you don&#x27;t necessarily need to recompile to enable these outputs) * A stack trace printing library<p>When you have that, such a macro becomes nearly trivial. Oh, yeah, and - drop the silly ANSI coloring.
评论 #21093039 未加载
评论 #21037343 未加载