KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > spi > logging > LogWrapperBase


1 /*
2  * @(#)LogWrapperBase.java 1.9 04/06/21
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.spi.logging ;
9
10 import java.util.logging.Level JavaDoc ;
11 import java.util.logging.Logger JavaDoc ;
12 import java.util.logging.LogRecord JavaDoc ;
13
14 public abstract class LogWrapperBase {
15     protected Logger JavaDoc logger ;
16
17     protected String JavaDoc loggerName ;
18
19     protected LogWrapperBase( Logger JavaDoc logger )
20     {
21     this.logger = logger ;
22         this.loggerName = logger.getName( );
23     }
24
25     protected void doLog( Level JavaDoc level, String JavaDoc key, Object JavaDoc[] params, Class JavaDoc wrapperClass,
26     Throwable JavaDoc thr )
27     {
28     LogRecord JavaDoc lrec = new LogRecord JavaDoc( level, key ) ;
29     if (params != null)
30         lrec.setParameters( params ) ;
31     inferCaller( wrapperClass, lrec ) ;
32     lrec.setThrown( thr ) ;
33         lrec.setLoggerName( loggerName );
34     lrec.setResourceBundle( logger.getResourceBundle() ) ;
35     logger.log( lrec ) ;
36     }
37
38     private void inferCaller( Class JavaDoc wrapperClass, LogRecord JavaDoc lrec )
39     {
40     // Private method to infer the caller's class and method names
41

42     // Get the stack trace.
43
StackTraceElement JavaDoc stack[] = (new Throwable JavaDoc()).getStackTrace();
44     StackTraceElement JavaDoc frame = null ;
45     String JavaDoc wcname = wrapperClass.getName() ;
46     String JavaDoc baseName = LogWrapperBase.class.getName() ;
47
48     // The top of the stack should always be a method in the wrapper class,
49
// or in this base class.
50
// Search back to the first method not in the wrapper class or this class.
51
int ix = 0;
52     while (ix < stack.length) {
53         frame = stack[ix];
54         String JavaDoc cname = frame.getClassName();
55         if (!cname.equals(wcname) && !cname.equals(baseName)) {
56         break;
57         }
58
59         ix++;
60     }
61
62     // Set the class and method if we are not past the end of the stack
63
// trace
64
if (ix < stack.length) {
65         lrec.setSourceClassName(frame.getClassName());
66         lrec.setSourceMethodName(frame.getMethodName());
67     }
68     }
69
70     protected void doLog( Level JavaDoc level, String JavaDoc key, Class JavaDoc wrapperClass, Throwable JavaDoc thr )
71     {
72     doLog( level, key, null, wrapperClass, thr ) ;
73     }
74 }
75
Popular Tags