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. package com.sun.corba.se.spi.logging ;
  8. import java.util.logging.Level ;
  9. import java.util.logging.Logger ;
  10. import java.util.logging.LogRecord ;
  11. public abstract class LogWrapperBase {
  12. protected Logger logger ;
  13. protected String loggerName ;
  14. protected LogWrapperBase( Logger logger )
  15. {
  16. this.logger = logger ;
  17. this.loggerName = logger.getName( );
  18. }
  19. protected void doLog( Level level, String key, Object[] params, Class wrapperClass,
  20. Throwable thr )
  21. {
  22. LogRecord lrec = new LogRecord( level, key ) ;
  23. if (params != null)
  24. lrec.setParameters( params ) ;
  25. inferCaller( wrapperClass, lrec ) ;
  26. lrec.setThrown( thr ) ;
  27. lrec.setLoggerName( loggerName );
  28. lrec.setResourceBundle( logger.getResourceBundle() ) ;
  29. logger.log( lrec ) ;
  30. }
  31. private void inferCaller( Class wrapperClass, LogRecord lrec )
  32. {
  33. // Private method to infer the caller's class and method names
  34. // Get the stack trace.
  35. StackTraceElement stack[] = (new Throwable()).getStackTrace();
  36. StackTraceElement frame = null ;
  37. String wcname = wrapperClass.getName() ;
  38. String baseName = LogWrapperBase.class.getName() ;
  39. // The top of the stack should always be a method in the wrapper class,
  40. // or in this base class.
  41. // Search back to the first method not in the wrapper class or this class.
  42. int ix = 0;
  43. while (ix < stack.length) {
  44. frame = stack[ix];
  45. String cname = frame.getClassName();
  46. if (!cname.equals(wcname) && !cname.equals(baseName)) {
  47. break;
  48. }
  49. ix++;
  50. }
  51. // Set the class and method if we are not past the end of the stack
  52. // trace
  53. if (ix < stack.length) {
  54. lrec.setSourceClassName(frame.getClassName());
  55. lrec.setSourceMethodName(frame.getMethodName());
  56. }
  57. }
  58. protected void doLog( Level level, String key, Class wrapperClass, Throwable thr )
  59. {
  60. doLog( level, key, null, wrapperClass, thr ) ;
  61. }
  62. }