1

I have been working with a lot of legacy plsql code off late. I see debug statements like below all over the code.The 'if' check was added to avoid the overhead of procedure call when debug mode is OFF.

IF (Debug mode is on) THEN
  Debug_pkg.logmsg(l_module_name,'Unexpected error : '|| SQLERRM, FND_LOG.LEVEL_WARNING);
END IF;

Is there a way to write debug statement in one line? In C, we have macros that can be useful to avoid procedural overhead. Can I do something similar in plsql code?

Edit:

Adding a little more details since my question might have confused some. What I am trying to avoid is writing 3 lines to print one debug statement. Can I write debug statement in one line using macros? Reason why I am trying to do this, When such statements are added all over the place it gives rise to clutter and reduces readability. When it should have been just one line, I see 3 lines for each debug statement.

Second edit:

Added my answer below.

2
  • can you please edit and move your approach to an answer? Commented Feb 27, 2013 at 4:46
  • @Sathya - Added my answer below. Commented Feb 27, 2013 at 19:13

2 Answers 2

3

Depending on the version of Oracle you're using, it is possible to use conditional compilation in PL/SQL. Something like

$IF $$debug_mode
$THEN
  Debug_pkg.logmsg(l_module_name,'Unexpected error : '|| SQLERRM, FND_LOG.LEVEL_WARNING);
$END

where $debug_mode is set in the session's PLSQL_CCFLAGS setting or

$IF package_name.debug_mode = 1
$THEN
  Debug_pkg.logmsg(l_module_name,'Unexpected error : '|| SQLERRM, FND_LOG.LEVEL_WARNING);
$END

where package_name.debug_mode is a package-level variable that indicates whether the code should be compiled in debug mode.

Again, depending on the version of Oracle you're using, you may be able to put the debug mode check in debug_pkg.logmsg and rely on the compiler either to automatically determine that it would be beneficial to inline the call (eliminating the procedure call overhead) or to instruct the compiler to inline the call with the INLINE pragma. Here is an article that discusses inlining in PL/SQL in more detail.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your examples. What I am trying to do is avoid writing 3 lines to print one debug statement. Your answers seems to enable the compiler to remove the debug statements when not in debug mode.This is not what I am looking for. I am looking for a way for the developer to write just one line to print debug statement when in debug mode.
@plspl - Correct. If that isn't what you're asking, potentially you're looking for subprogram inlining. Expanded my answer for that.
Thanks, the link seems to be useful.
can you edit your answer to remove text related to conditional compilation? I can then accept your answer. I also added an example myself. Thanks for your help.
0

Thanks for the link http://www.oracle-developer.net/display.php?id=502 from Justin Cave. I ended up doing this for now,

  PROCEDURE debug_msg(txt VARCHAR2) 
  IS 
  BEGIN  
      IF (debug mode is on) THEN
        debug_pkg.logmsg('Module', txt , LEVEL_WARNING);
      END IF;
  END;

To inline the above procedure, you need to do this.

PRAGMA INLINE(debug_msg, 'YES');     

However inline is just a request to the compiler, it may or may not be inlined. Also, the procedure being inlined needs to be defined in the same package.

2 Comments

I am curious what the "debug mode is on" is? is it a valid pl/sql expression, or is it a pseudo code
its just pseudo code to indicate a variable/expression that determines if debug mode is on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.