plspl
plspl

Reputation: 728

Debug statements in plsql code

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.

Upvotes: 1

Views: 4047

Answers (2)

plspl
plspl

Reputation: 728

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.

Upvotes: 0

Justin Cave
Justin Cave

Reputation: 231651

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.

Upvotes: 3

Related Questions