DBA Data[Home] [Help]

PACKAGE BODY: APPS.JAI_CMN_DEBUG_CONTEXTS_PKG

Source


1 package body jai_cmn_debug_contexts_pkg AS
2 /*$Header: jai_cmn_dbg_ctx.plb 120.3.12000000.3 2007/10/25 02:12:53 rallamse noship $*/
3 
4   /** Stack Declaration */
5   jai_debug_stack_overflow        exception;
6   t_call_stack                    tab_log_manager_typ;
7   ln_stack_top	                  number	      := 0;
8 
9   /** Buffers */
10 
11   t_context_cache                 tab_log_manager_typ;
12   ln_context_idx                  number  := 0;
13 
14   last_file_name                  varchar2(250);
15   last_file_handle                utl_file.file_type;
16   last_utl_dir                    varchar2(2000);
17   ln_user_id                      fnd_user.user_id%type;
18   lv_default_log_file             varchar2 (512);
19   lv_file_suffix                  varchar2 (250);
20   r_temp_rec                      jai_cmn_debug_contexts%rowtype;
21 
22   /** Debuging Setting */
23   ln_stack_max_cnt                number        := 200000;
24   lv_each_file_open_mode          varchar2(1)   := 'a';
25   lv_default_log_file_prefix varchar2 (240)     := 'jai_cmn_debug_contexts';
26   lv_default_log_file_suffix varchar2 (10)      := '.log';
27   lv_exception_propagation   varchar2 (2)       := jai_constants.No;
28   ln_context_cache_size      number             :=  20;
29   lv_debug                   varchar2(2)        := jai_constants.no; -- internal debug  --modified by csahoo for bug#6401388
30   ln_print_each_context      varchar2(2)        := jai_constants.yes; -- Prints each context when registered
31   lv_internal_log_file       varchar2 (100)     := 'JaiDebugLogInternal.log';
32 
33 /***************************************************************************************************
34 -- #
35 -- # Change History -
36 
37 
38 1.   02/02/2007   Bgowrava for bug#5631784. File Version 120.0
39 									Forward Porting of 11i BUG#4742259 (TCS Enhancement)
40                   lv_exception_propagation is initialised to jai_constants.No instead of jai_constants.Yes
41 2.  12/06/2007   Kunkumar made changes for bug#5915101
42                  replaced the write log with debug .
43 3.  11/09/2007    CSahoo for bug#6401388, File Version 120.3.12000000.2
44 									Initially lv_debug was assigned to jai_constants.yes. Changes that to jai_constants.no
45 ********************************************************************************************************/
46 
47 
48   procedure init;
49 
50   /*------------------------------------------------------------------------------------------------------------*/
51   procedure when_stack_empty
52   is
53   begin
54     /** Perfom cleanup */
55     debug ('Cleanup');
56     last_file_name := null;
57 
58     if utl_file.is_open (last_file_handle) then
59       begin
60         utl_file.fclose (last_file_handle);
61       exception
62         when others then
63         if sqlcode = -29282 then
64           /** no such file is open, Do nothing */
65           null;
66         end if;
67       end;
68     end if;
69 
70     ln_stack_top := 0;
71     t_call_stack.delete;
72     debug ('Stack Flushed, count is' ||t_call_stack.count);
73   exception
74     when others then
75     debug('when_stack_empty->'||sqlerrm);
76     if lv_exception_propagation = jai_constants.yes then
77       raise;
78     end if;
79 
80   end when_stack_empty;
81   /*------------------------------------------------------------------------------------------------------------*/
82   function get_log_file_handle (pv_file_name varchar2) return utl_file.file_type
83   is
84   begin
85     if last_file_name is null or (last_file_name <> pv_file_name) then
86 
87       if utl_file.is_open (last_file_handle) then
88         begin
89           utl_file.fclose (last_file_handle);
90         exception
91           when others then
92           if sqlcode = -29282 then
93             /** no such file is open, Do nothing */
94             null;
95           end if;
96         end;
97       end if;
98 
99       if last_utl_dir is null then
100         select	decode(substr (value,1,instr(value,',') -1)	,null,	value,substr (value,1,instr(value,',') -1))
101         into last_utl_dir
102         from	v$parameter
103         where	name = 'utl_file_dir';
104       end if;
105 
106       last_file_handle:= utl_file.fopen
107                             ( last_utl_dir
108                             , pv_file_name
109                             , lv_each_file_open_mode
110                             );
111       last_file_name := pv_file_name;
112     end if;
113     return last_file_handle;
114 
115   exception
116   when others then
117     debug('get_log_file_handle->'||sqlerrm);
118     if lv_exception_propagation = jai_constants.yes then
119       raise;
120     end if;
121 
122   end get_log_file_handle ;
123 
124   /*---------------------------------------------------------------------------------------------------*/
125    procedure parse_and_print_stack (pn_reg_id  number default null)
126    is
127     ln_print_upto number;
128     ln_stack_ptr number;
129     ln_reg_id   number;
130   begin
131     debug ('Begin - PARSE_AND_PRINT_STACK');
132     debug ('pn_reg_id ='||pn_reg_id);
133     ln_reg_id := nvl(pn_reg_id, ln_stack_top);
134 
135     if pn_reg_id is not null then
136       ln_print_upto := pn_reg_id;
137     else
138       ln_print_upto := 0;
139       print  (ln_reg_id
140              ,'--------------------------------------------------------------------------------------------------- '
141              ,summary
142              );
143       print  (ln_reg_id
144              , 'WARNING:  Call stack was holding following methods when trying to de-register: '||t_call_stack(ln_reg_id).registered_name
145              , summary
146              );
147       print  (ln_reg_id
148              ,'---------------------------------------------------------------------------------------------------- '
149              ,summary
150              );
151     end if;
152     debug ('t_call_stack.count='||t_call_stack.count);
153     if t_call_stack.count > 0 then
154 
155       print (ln_reg_id, 'Call Stack:');
156       print (ln_reg_id, rpad('Registered Name',100,' ') ||lpad(' ',5,' ')||lpad('Stack position',15,' ')
157             ,summary
158             );
159       print (ln_reg_id, rpad('---------------',100,'-') ||lpad(' ',5,' ')||lpad('--------------',15,'-')
160             );
161     end if;
162 
163 
164     ln_stack_ptr := t_call_stack.last;
165     debug ('ln_stack_ptr='||ln_stack_ptr  ||', ln_print_upto='||ln_print_upto);
166     while (ln_stack_ptr > ln_print_upto)
167     loop
168       print (ln_reg_id,rpad(t_call_stack(ln_stack_ptr).registered_name,100,' ')||lpad(' ',5,' ')||lpad(ln_stack_ptr,15,' '));
169       ln_stack_ptr := ln_stack_ptr - 1;
170     end loop;
171 
172   exception
173   when others then
174     debug('parse_and_print_stack->'||sqlerrm);
175     if lv_exception_propagation = jai_constants.yes then
176       raise;
177     end if;
178 
179   end parse_and_print_stack;
180   /*---------------------------------------------------------------------------------------------------*/
181   procedure pop_stack (pn_reg_id number default null)
182   is
183     ln_new_stack_top number;
184   begin
185     debug ('Begin -> POP_STACK');
186     debug ('pn_reg_id='|| pn_reg_id);
187     if pn_reg_id is null then
188       if t_call_stack.exists(ln_stack_top) then
189         t_call_stack.delete(pn_reg_id);
190         ln_stack_top := ln_stack_top - 1;
191       end if;
192     else
193         parse_and_print_stack(pn_reg_id);
194         t_call_stack.delete(pn_reg_id,ln_stack_top);
195         ln_stack_top := pn_reg_id-1;
196     end if;
197     debug ('Stack count= '||ln_stack_top);
198     if ln_stack_top = 0 then
199       when_stack_empty;
200     end if;
201 
202   exception
203   when others then
204     debug('pop_stack->'||sqlerrm);
205     if lv_exception_propagation = jai_constants.yes then
206       raise;
207     end if;
208   end pop_stack;
209 
210  /*---------------------------------------------------------------------------------------------------*/
211 
212   procedure write_log ( pn_reg_id   in number default null
213                       , pv_log_msg  in varchar2
214                       , pn_statement_level in number default jai_cmn_debug_contexts_pkg.off
215                       , pv_new_line_flag  in varchar2 default jai_constants.yes
216                       )
217   is
218     f_handle  utl_file.file_type;
219   begin
220 
221     if pn_reg_id is null AND NVL(lv_debug,jai_constants.no)=jai_constants.yes then --Added the second condition by kunkumar in if statement for bug#5915101
222       /** Internal calll to write in the default log file */
223       f_handle := get_log_file_handle (lv_internal_log_file);
224       utl_file.put_line (f_handle, pv_log_msg);
225       return;
226     end if;
227 
228     if t_call_stack(pn_reg_id).row.log_status >= pn_statement_level then
229       f_handle := get_log_file_handle (nvl(t_call_stack(pn_reg_id).log_file_name,lv_default_log_file));
230       if pv_new_line_flag = jai_constants.no then
231         utl_file.put (f_handle, pv_log_msg);
232       else
233         utl_file.put_line (f_handle, pv_log_msg);
234       end if;
235     end if;
236 
237   exception
238 
239   when others then
240     if lv_exception_propagation = jai_constants.yes then
241       raise;
242     end if;
243 
244   end write_log;
245   /*---------------------------------------------------------------------------------------------------*/
246   procedure debug (lv_msg varchar2)
247   is
248   begin
249     if lv_debug=jai_constants.yes then
250       write_log (pv_log_msg => '(#):'|| lv_msg);
251       --dbms_output.put_line (lv_msg);
252     end if;
253   end debug;
254 
255  /*------------------------------------------------------------------------------------------------------------*/
256   procedure register ( pv_context in  varchar2
257                      , pn_reg_id OUT NOCOPY number
258                      )
259   is
260     cursor c_check_context
261     is
262     select  debug_log_id
263            ,log_context
264            ,nvl(log_status,0)    log_status
265            ,log_file_prefix
266            ,debug_user_id
267            ,OBJECT_VERSION_NUMBER      -- added by bgowrava for forward porting bug #5631784
268     from   jai_cmn_debug_contexts dlog
269     where  pv_context like dlog.log_context || '%'
270     and    debug_user_id  = ln_user_id
271     order by (length(pv_context)-length(dlog.log_context))
272 	          , dlog.debug_log_id ;
273 
274   begin
275 
276     if ln_user_id is null then
277       init;
278     end if;
279 
280     debug ('ln_user_id='||ln_user_id);
281     debug ('pv_context='||pv_context);
282 
283     r_temp_rec.log_context := null;
284     for i in 1 .. ln_context_cache_size
285     loop
286       if  t_context_cache.exists(i)
287       and t_context_cache (i).registered_name = pv_context then
288 
289         r_temp_rec := t_context_cache(i).row;
290         exit;
291 
292       end if;
293     end loop;
294     debug('r_temp_rec.log_context='||r_temp_rec.log_context);
295     if r_temp_rec.log_context is null then
296       open  c_check_context;
297       fetch c_check_context into r_temp_rec;
298       close c_check_context;
299     end if;
300         debug('After Fetch : r_temp_rec.log_context='||r_temp_rec.log_context);
301     if r_temp_rec.log_context is not null then
302       debug ('context found in the setup');
303 
304       ln_stack_top := ln_stack_top + 1 ;
305 
306       if ln_stack_top > ln_stack_max_cnt then
307         raise jai_debug_stack_overflow;
308       end if;
309 
310       t_call_stack(ln_stack_top).row := r_temp_rec;
311       t_call_stack(ln_stack_top).registered_name := pv_context;
312       if t_call_stack (ln_stack_top).row.log_status = 0 then
313         pn_reg_id := -1 * ln_stack_top;
314       else
315         if t_call_stack (ln_stack_top).row.log_file_prefix is not null then
316           t_call_stack (ln_stack_top).log_file_name := t_call_stack (ln_stack_top).row.log_file_prefix || lv_file_suffix;
317         end if;
318 
319       pn_reg_id :=  ln_stack_top;
320 
321       if ln_context_idx = ln_context_cache_size then
322         ln_context_idx := 1;
323       else
324         ln_context_idx := ln_context_idx + 1;
325       end if;
326       debug ('ln_context_idx='||ln_context_idx);
327       t_context_cache (ln_context_idx) := t_call_stack(ln_stack_top);
328 
329       end if;
330       debug ('PN_REG_ID='||PN_REG_ID);
331     else
332       debug ('Context='||pv_context ||' not registered' );
333       pn_reg_id := 0;
334       debug ('pn_reg_id='||pn_reg_id);
335     end if; /**r_temp_rec is not null */
336 
337     if ln_print_each_context = jai_constants.yes then
338       print (pn_reg_id, lpad('-',100,'-'),summary);
339       print (pn_reg_id, lpad('BEGIN -> '|| pv_context,240,' '),summary);
340       print (pn_reg_id, lpad('-',100,'-'),summary);
341     end if;
342     debug ('End -> Register');
343 
344   exception
345 
346   when jai_debug_stack_overflow then
347 
348       print_stack;
349       debug('Stack overflow error in jai_cmn_debug_contexts_pkg');--kunkumar replaced write_log with debug for bug#5915101
350 
351       if lv_exception_propagation = jai_constants.yes then
352         raise_application_error (-20275, 'Stack overflow error in jai_cmn_debug_contexts_pkg');
353       end if;
354   when others then
355     debug('register->'||sqlerrm);
359 
356     if lv_exception_propagation = jai_constants.yes then
357       raise;
358     end if;
360   end register;
361   /*------------------------------------------------------------------------------------------------------------*/
362   procedure print  ( pn_reg_id   in number
363                    , pv_log_msg  in varchar2
364                    , pn_statement_level in number default jai_cmn_debug_contexts_pkg.detail
365                    )
366   is
367   begin
368     debug ('Begin -> PRINT');
369     debug ('pn_reg_id  ='||pn_reg_id);
370     debug ('pv_log_msg ='||pv_log_msg);
371 
372     if pn_reg_id <= 0  or pn_reg_id is null then
373       return;
374     end if;
375     if t_call_stack.exists(pn_reg_id) then
376       write_log (pn_reg_id, pv_log_msg, pn_statement_level);
377       debug ('WRITE_LOG successful');
378     else
379       raise_application_error (-20275, 'REGISTER method must be called before calling PRINT method');
380     end if;
381     debug ('End -> PRINT');
382   exception
383     when others then
384     debug('print->'||sqlerrm);
385     if lv_exception_propagation = jai_constants.yes then
386       raise;
387     end if;
388 
389   end print;
390   /*------------------------------------------------------------------------------------------------------------*/
391    procedure print_stack
392    is
393    begin
394       debug ('Begin -> PRINT_STACK');
395       parse_and_print_stack ;
396       debug ('End  -> PRINT_STACK');
397    exception
398     when others then
399     debug('print_stack->'||sqlerrm);
400     if lv_exception_propagation = jai_constants.yes then
401       raise;
402     end if;
403 
404    end print_stack;
405   /*------------------------------------------------------------------------------------------------------------*/
406   procedure deregister(pn_reg_id in number)
407   is
408    ln_reg_id number;
409   begin
410     debug ('Begin -> DEREGISTER');
411     debug ('pn_reg_id ='||pn_reg_id);
412 
413     if pn_reg_id = 0 then
414       return ;
415     end if;
416 
417     ln_reg_id := abs(pn_reg_id);
418 
419     if ln_print_each_context = jai_constants.yes then
420       print (ln_reg_id, lpad('-',100,'-'),summary);
421       print (ln_reg_id, lpad('End -> '|| t_call_stack(ln_reg_id).registered_name,240,' '),summary);
422       print (ln_reg_id, lpad('-',100,'-'),summary);
423     end if;
424 
425     debug ('ln_stack_top='||ln_stack_top);
426     if ln_reg_id < ln_stack_top then
427       pop_stack (ln_reg_id );
428     elsif ln_reg_id  = ln_stack_top then
429       pop_stack;
430     end if;
431     debug ('End -> DEREGISTER');
432   exception
433     when others then
434     debug('deregister->'||sqlerrm);
435     if lv_exception_propagation = jai_constants.yes then
436      print_stack;
437       raise;
438     end if;
439   end deregister;
440 
441 /*---------------------------------------PACKAGE CONSTRUCTOR ------------------------------------*/
442   procedure init
443   is
444   ln_audsid number;
445   begin
446 
447       debug ('Begin -> INIT');
448       if ln_user_id is null then
449         when_stack_empty;
450         select fnd_global.user_id into ln_user_id from dual;
451         select sys_context('USERENV','SESSIONID') into ln_audsid from dual;
452       end if;
453       debug ('ln_user_id='||ln_user_id||', ln_audsid='||ln_audsid );
454 
455 
456       if lv_file_suffix is null then -- Only once when package is loaded
457         lv_file_suffix := '_'||to_char(sysdate,'DDMM_HH24MI')
458                              ||'_'||replace(ln_user_id, -1, 'DB')
459                              ||'_'||ln_audsid
460                              ||lv_default_log_file_suffix;
461 
462         lv_default_log_file := lv_default_log_file_prefix || lv_file_suffix;
463       end if;
464 --write log replaced with debug  by kunkumar for bug#5915101
465       debug( 'JAI Debug Settings:'
466                    ||fnd_global.local_chr(10)||'--------------------------------------------------------------------------'
467                    ||fnd_global.local_chr(10)||'Maximum Stack Size='||ln_stack_max_cnt
468                    ||fnd_global.local_chr(10)||'Each Log File Open Mode='||lv_each_file_open_mode
469                    ||fnd_global.local_chr(10)||'Default Log File Prefix='||lv_default_log_file_prefix
470                    ||fnd_global.local_chr(10)||'Default Log File Suffix='||lv_default_log_file_suffix
471                    ||fnd_global.local_chr(10)||'Internal Exception Propagation='||  lv_exception_propagation
472                    ||fnd_global.local_chr(10)||'Context Cache Size (max)='||ln_context_cache_size
473                    ||fnd_global.local_chr(10)||'Internal Debug Logging='||lv_debug
474                    ||fnd_global.local_chr(10)||'Context Printing='||ln_print_each_context
475                    ||fnd_global.local_chr(10)||'--------------------------------------------------------------------------'
476                 );
477       if lv_debug = jai_constants.yes then
478         write_log(pv_log_msg=>'NOTE: Internal logging is enabled.  Lines marked with "(#)" are internal log messages.  To disable internal logging
479                     set private variable jai_cmn_debug_contexts_pkg.lv_debug=''N'''
480                   );
481       end if;
482       debug ('End -> INIT');
483    exception
484    when others then
485     debug('INIT ->'||sqlerrm);
486     if lv_exception_propagation = jai_constants.yes then
487       raise;
488     end if;
489   end init;
490 
491 end jai_cmn_debug_contexts_pkg;