Author Topic: add trace line under each function/tag name  (Read 4833 times)

ehab

  • Senior Community Member
  • Posts: 285
  • Hero Points: 15
  • coding with SE is like playing music
add trace line under each function/tag name
« on: April 22, 2010, 08:24:44 am »
hi,

i wonder did anyone tried to add some trace line within each function eg:

void myfun(){

debug::print("some trace output");

}

this is useful when creating some flowcharts, or understanding what is happening.

if SE or someone has a method, macro then its mostly welome : )

my thanks in advances
ehab

LBCEi

  • Senior Community Member
  • Posts: 158
  • Hero Points: 9
Re: add trace line under each function/tag name
« Reply #1 on: April 22, 2010, 10:04:04 am »
One handy built-in tool that might get you started is generate-debug.

If you have your cursor on a variable name in your code and run this command (generate-debug on command line or Tools>Generate Debug Command for <variablename>), a printf statement will be inserted on the next line.  It detects the type of the variable and uses an appropriate conversion specifier.

For example, say you have the following code:

Code: [Select]
double result = 0;

for (int i = 1; i<5; i++)
{
    result = 2.5 * (double) i;
}

If you put your cursor on 'result' inside the loop and execute generate-debug, you will get the following:

Code: [Select]
double result = 0;

for (int i = 1; i<5; i++)
{
    result = 2.5 * (double) i;
    printf("result=%lf\n", result);
}

If you want to create your own custom version you can copy generate_debug from quickrefactor.e (fp generate-debug at the command line will open this), put it in your own new macro file, change the command name and customize as desired.

Hope this helps,
Les

ehab

  • Senior Community Member
  • Posts: 285
  • Hero Points: 15
  • coding with SE is like playing music
Re: add trace line under each function/tag name
« Reply #2 on: April 22, 2010, 10:14:01 am »
Thanks for the generate-debug fn i never checked it.

my request is a lot more simpler, just go to each function and add the line.

what i am doing now is as flows:

next-proc
move couple of lines until { is reaced
RDebug::Printf("Trace - somefile.cpp > functionname- line number\n");  // this is bond to key " already implemented"

i am thinking about

Code: [Select]
goto first line
repeat until end of file
{
   next-proc
   find {
   insert trace line
}

i haven't implemented the above yet but will do when desperate " many many fns".

LBCEi

  • Senior Community Member
  • Posts: 158
  • Hero Points: 9
Re: add trace line under each function/tag name
« Reply #3 on: April 22, 2010, 10:16:17 am »
I forgot to mention the use of generate-debug for functions (probably more what you were looking for).  It will generate a debug print statement showing the function name, parameters, and values.

Here is the comment header from the macro (including typos):

Code: [Select]
/**
 * This command will generated print code for the symbol under the cursor.
 * If the symbol is a function then a print will be generated that will
 * show the function name and list all of the parameters and their values.
 * If the symbol is a variable then the code to display the name and
 * contents of the variable will be generated.  The languages currently
 * supported are C/C++, Java, and Slick-C&reg;.  If the symbol is a struct
 * or class then all public members of the class/struct will have prints
 * generated for them.  Java array symbols will have loop code generated
 * to iterate through all the members.  For Slick-C&reg; the code to
 * iterate through both hash tables and arrays will be generated.
 */

Graeme

  • Senior Community Member
  • Posts: 1987
  • Hero Points: 228
Re: add trace line under each function/tag name
« Reply #4 on: April 22, 2010, 11:25:00 am »
Hi ehab

I wrote something like this for C/C++ quite a while ago.  I had it working but I've never used it. I think it inserts XTRACE(trace_id) at the start of every function and XTRACE_EXIT at the end.  You need to define these as macros. add_trace_all does it for all open buffers.  add_trace does it for just one.  The next_proc that you mention might well be better than find_tag which depends on the filter settings in the defs toolbar.  For C++ you can use the constructor/ destructor "RAII" style to capture the entry and exit.

Graeme

Code: [Select]
int trace_id;

_str trace_cross_ref[];

_command add_trace() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
   top();
   int line1;
   _str filen = strip_filename(p_buf_name,'DP');
   while (next_tag('Y') == 0)
   {
      find('{','I?');
      _deselect();
      end_line();
      c_enter();
      begin_line();
      _insert_text("XTRACE(" :+ (_str)++trace_id :+ ');');
      line1 = p_line;
      find('{','I-?');
      _deselect();
      find_matching_paren(true);
      keyin_enter();
      cursor_up();
      _insert_text("XTRACE_EXIT(" :+ (_str)trace_id :+ ');');

      _str cur_proc = '', cur_class = '';

      cur_proc = current_proc(false);
      cur_class = current_class(false);
      if ( length(cur_class))
         cur_proc = cur_class :+ '::' :+ cur_proc;

      trace_cross_ref[trace_cross_ref._length()] =
         (_str)trace_id :+ ' lines: ' :+ field(line1,5) :+ ' ' :+ field(p_line,5) :+
             ' ' :+ field(cur_proc,35) :+ '  ' :+ field('"' :+ filen :+ '"', 16) :+  '  "' :+ p_buf_name :+ '"';
   }
}

_str trace_files[];


int add_to_trace_list()
{
   if (p_extension == 'c') {
      trace_files[trace_files._length()] = p_buf_name;
   }
   return 0;
}

_command void add_trace_all() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
   trace_id = 10000;
   trace_files._makeempty();
   trace_cross_ref._makeempty();
   for_each_buffer('add_to_trace_list');
   trace_files._sort('F');
   int k;
   for (k = 0; k < trace_files._length(); ++k) {
      _str ext = get_extension(trace_files[k]);
      if ( ext == 'c' || ext == 'cpp') {
         message('Trace ' :+ field(trace_id,5) :+ ' ' :+ strip_filename(p_buf_name,'DP'));
         edit(maybe_quote_filename(trace_files[k]));
         add_trace();
      }
   }
   edit('trace_cross_ref.txt');
   bottom();
   for (k = 0; k < trace_cross_ref._length(); ++k) {
      keyin_enter();
      _insert_text(trace_cross_ref[k]);
   }
   keyin_enter();
   keyin_enter();
   _insert_text('End cross reference');
   keyin_enter();
}

evanratt

  • Senior Community Member
  • Posts: 300
  • Hero Points: 23
Re: add trace line under each function/tag name
« Reply #5 on: April 22, 2010, 07:13:26 pm »
Wow... I never knew about the generate-debug command... that's a nice little utility function. Now I just need to find a spare key combination to bind it to...  ;)   HP++!

ehab

  • Senior Community Member
  • Posts: 285
  • Hero Points: 15
  • coding with SE is like playing music
Re: add trace line under each function/tag name
« Reply #6 on: April 23, 2010, 04:30:54 pm »
 ;)

works nicely and used your modified your sample to do something even better : )

thanku(Graeme);

Scott H

  • Senior Community Member
  • Posts: 240
  • Hero Points: 9
Re: add trace line under each function/tag name
« Reply #7 on: April 27, 2010, 07:29:03 pm »
I just wrote a blog post that covers exactly this.  I'm a little embarrassed to admit that I never even knew about generate-debug until someone here saw that post and said, "ya know it already exists."  Regardless, it's still a good intro for starting to write macros.  I was actually going to do exactly what Graeme did as my second one, so now I need to think of another idea.  I want this to be the beginning of a series that builds on itself... think Compute! magazine back in the 80s, where you got program listings, typed them in and learned something from it.  Slick-C can be quite a challenge to learn on its own, so I'm hoping that doing a bunch of example projects will get people interested in writing their own macros.  Let me know if you guys have any ideas.

evanratt

  • Senior Community Member
  • Posts: 300
  • Hero Points: 23
Re: add trace line under each function/tag name
« Reply #8 on: April 27, 2010, 07:46:25 pm »
Hey Scott,
I saw your blog post and thought it might have been inspired by this thread :)  But really, I like your idea for this series of posts. I've only done very minor Slick-C stuff on my own, so a series of posts with ideas for things to be done and how to do them would be immensely useful. The one you already made was helpful for me to see how generate-debug might work, and gave me ideas already. Definitely a great idea!

ehab

  • Senior Community Member
  • Posts: 285
  • Hero Points: 15
  • coding with SE is like playing music
Re: add trace line under each function/tag name
« Reply #9 on: April 27, 2010, 07:52:06 pm »

check out the user submitted macros, since they are already solve problems and can be used as use cases using slick.

thanks for the blog, i hope you continue.

Graeme

  • Senior Community Member
  • Posts: 1987
  • Hero Points: 228
Re: add trace line under each function/tag name
« Reply #10 on: April 28, 2010, 07:31:18 am »
I just wrote a blog post that covers exactly this.  I'm a little embarrassed to admit that I never even knew about generate-debug until someone here saw that post and said, "ya know it already exists."  Regardless, it's still a good intro for starting to write macros.  I was actually going to do exactly what Graeme did as my second one, so now I need to think of another idea.  I want this to be the beginning of a series that builds on itself... think Compute! magazine back in the 80s, where you got program listings, typed them in and learned something from it.  Slick-C can be quite a challenge to learn on its own, so I'm hoping that doing a bunch of example projects will get people interested in writing their own macros.  Let me know if you guys have any ideas.

I was thinking of writing a slick C tutorial for exactly the same reasons you give in your blog post but I lack the time.  Like you said in your blog post, not many people want to take the time to learn a full blown language like slick C.  Maybe you could write a blog post that has a piece of code (e.g. 30 lines) that illustrates the most commonly used parts of slick C - aimed at C/C++/Java/javascript etc programmers who already know the basic syntax.  This would include things like for, if, while etc plus arrays and variables (local and global) - so people feel immediately "at home" and can see what else they need to learn.  Enough stuff to write something without feeling overwhelmed with detail.  Other blogs could mention 1. defload, definit, def_vars, def_events & module initialization; 2. the form designer.  Somewhere along the line you could mention things like de-referencing of integer constants and builtins.h plus finding your way round the Slick C API.  Also macro recording - that's one of Slickedit's best features.

How would people find these blog posts I wonder.  There's a few people seem to create their own language support stuff but I'm not sure that Dennis's blog post about it is easy to find.

Anyway, I don't see why you can't do one on the trace thing like I did.

Graeme
 

Scott H

  • Senior Community Member
  • Posts: 240
  • Hero Points: 9
Re: add trace line under each function/tag name
« Reply #11 on: April 28, 2010, 12:45:19 pm »
Those are great ideas.  I always learn best from scavenging other code and I thought this would be helpful for others.  I also wanted to write down exactly how I went about making a new macro... more than just here's the code, good luck.  I've started writing the next one and as I write it, I'm trying to get down all the paths I went down, like how I use the API help, how I use grep-tag and find in files to figure out how stuff works.  Thanks for the suggestions!