Follow Me On...
« Creating a VisualForce email template which invokes an Apex Class. | Main | Inductor Core Type Tradeoffs »
Wednesday
May202009

Using RegEx to comment out log statements cleanly.

I needed to optimize my code space usage on a recent embedded project. I wanted to estimate how much space was being used by my various print statements. In my project I have debug, info, warn, error level print statements.  I used the regex below to find each of the statements and  was able to find all statements, comment them out, and rebuild my project to see
how much space each log level used.

Find: (^\s*)((info)\S*\(.*)
Replace: $1;//CSOPT_$3 $2
File Types: *.c <—- important otherwise you could update header files or .txt files.

Example:
    infoln(“some info statement”);    —- BECOMES —->
    ;//CSOPT_info infoln(“some info statement”);

To use this regex you replace the “info” text with the command prefix you want to replace. It also will find things like infoln.  You may be wondering why the replace expression looks so complicated…

  • For one, you do not need to edit each replace statement for each find.
  • It maintains whitespace properly.
  • The reason for the ; in replace is to prevent issues where you had a if statements without brackets… the next statement isn’t what you meant for it to be.

To reenable your statements all you have to do is search for “//CSOPT_info ” (to replace one log type ata time) or “//CSOPT_\S+\s” (to replace all at once) and leave the replace box blank.

One issue you may encounter is if you have print statements which extend on to multiple lines… these you will either need to combine into one line or handle specially. Since these always create a compile error it’s not a big deal.

 

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.