Nikhil's blog

By Nikhil, 12 years ago, In English

Hi guys,

Many of you would've used a very handy function in Java that takes in a variable number of arguments (of possibly different types) and outputs them.

void debug(Object...args)
{
    System.out.println(Arrays.deepToString(args));
}


I've written some C++ code that behaves in an exactly identical manner that you can put in your C++ template as well.

Features :
1) Nifty and quick debug statements

2) It can also output some STL containers like vector, set and maps in a pretty and easy to read format.

3) If you compile your code with DEBUG flag, these debug statements are executed, else all these statements are removed from the code. So you don't even need to strip them off manually by commenting or deleting. In particular at any online judge, none of these statements would be executed.

Usage : 

So to compile a file and use debug statements,  you just need to write g++ -DDEBUG a.cpp .If you dont give this flag and compile in ordinary way like : g++ a.cpp, then these statements are removed from the code.

You can find the code here You just need to copy lines 10 to 91. I've tested it on GCC, though I expect it to work with other compilers as well.

Merry Christmas & Happy Coding :)

  • Vote: I like it
  • +38
  • Vote: I do not like it

| Write comment?
»
12 years ago, # |
  Vote: I like it +5 Vote: I do not like it
In line 26 and 31, you wrote "std:cerr" instead of "std::cerr", which gave warnings when compiling.
  • »
    »
    12 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    Thanks a lot for pointing out. I've corrected it now :)
»
12 years ago, # |
  Vote: I like it 0 Vote: I do not like it

for example we have vector<int> a, consisting of number [0..5]; // 0 1 2 3 4 5

if I write debug(a) -

debugger now shows like this: [0, 1, 2, 3, 4, 5].

it would be better if it shows like this: a = [0, 1, 2, 3, 4, 5]. (I mean to showing the name of variable).

  • »
    »
    12 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    I tried to create a debugger like that but I couldn't as variable name is lost as soon as it is passed as a function argument. What you can do however (this is what I do as well) is manually call it like this :  debug("a : ", a)

    Hope it helps :)
    • »
      »
      »
      12 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it
      You can use macros. For example:
      #define debug_var(x) debug(#x ": ", x)
      So
      debug_var(2+2);
      will be compiled to
      debug("2+2" ": ", 2+2);
      • »
        »
        »
        »
        12 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        This would work perfectly if you want to debug with only a single variable. However with multiple arguments to debug :
        1. You'd have to change it to a variadic macro. So you'd need to write #define debug_var(x...) debug(#x, ": ", x)
        2. And so now stringification would be done on whole of x (multiple arguments in this case). So debug_var(A, B) would output "A, B: , (A) , (B)" where I assume (v) denotes value of a variable v.

        I'm not aware of any way in which I could make debug(A,B) print as "A :  (A) , "B : (B)

        • »
          »
          »
          »
          »
          6 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Here is a code I use (it is from some blog on codeforces, sorry I don't remember by whom), IDE to play:

          Code

          For example,

              vector<int> arr ={1,2,3};
              int A = 123;
              float B = 10.123;
              EVARS(A, arr, B);
          

          outputs

          #37: A=123; arr=[1,2,3]; B=10.123;
          

          It has limitations of course, but I find it very convenient and customizable. For example it does not parse calls correctly, e.g. EVARS(gcd(a,b)) results in #40: gcd(A=138;

        • »
          »
          »
          »
          »
          5 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Need some help ...i was solving previous codeforces contest problem 1199C - MP3 .... when i submitted the code with the debugging statements 58073974 i was getting TLE ...but after some time just randomly I comment down the debugging statement and surprisingly I got the accepted verdict 58074310 ....in just 328 sec can anyone explain me is these statements affect this much ??

          • »
            »
            »
            »
            »
            »
            5 years ago, # ^ |
              Vote: I like it +3 Vote: I do not like it

            This is a very common thing. Your trace function is sending output data to the standard error stream through cerr. Outputting data consumes execution time of the program. Codeforces checker will read data output data only from stdout, which is output through cout. But the data that is going to stderr by cerr is still going there, which consumes execution time and hence the code TLEs. So, always remember to comment the line #define TRACE in your code before submitting or to comment every line that calls trace() function so that it doesn't happen.

»
12 years ago, # |
  Vote: I like it 0 Vote: I do not like it
For maps, can the output be like :
"hello" ----> 45
  • »
    »
    12 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it
    Yes of course :)
    Lines 74 - 87 are defining how a map should be printed. You can rewrite that to suit your needs.
    In particular you'd need to rewrite line 83 as :
    os << "(" << (*ii).first << " ---> " << (*ii).second <<")"
    • »
      »
      »
      12 years ago, # ^ |
        Vote: I like it +5 Vote: I do not like it
      Thanks! All the best for ICPC finals.
      Win it this time! :)
»
9 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Hi, any chance you can upload this again or upload it elsewhere? The link just leads to a blank page for me on both Firefox and Chrome :/

Cheers, Esuhi

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    His last visit was 18 months ago, so I don't think he will see your comment.

»
9 years ago, # |
  Vote: I like it +3 Vote: I do not like it

The code is not there, Can anybody share the code again? please ...

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it
»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone please post link? The mentionded ideone link isn't working.

»
6 years ago, # |
  Vote: I like it +13 Vote: I do not like it
»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can I ask you guys a really really stupid question (^^)? How can I use it with Codeblock compile engine. (Like if I need to press some other button, and if click "Build and Run" print the debug lines out ? ).