LogWrapperBase.java revision 608:7e06bf1dcb09
1/* 2 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.corba.se.spi.logging ; 27 28import java.util.logging.Level ; 29import java.util.logging.Logger ; 30import java.util.logging.LogRecord ; 31 32public abstract class LogWrapperBase { 33 protected Logger logger ; 34 35 protected String loggerName ; 36 37 protected LogWrapperBase( Logger logger ) 38 { 39 this.logger = logger ; 40 this.loggerName = logger.getName( ); 41 } 42 43 protected void doLog( Level level, String key, Object[] params, Class wrapperClass, 44 Throwable thr ) 45 { 46 LogRecord lrec = new LogRecord( level, key ) ; 47 if (params != null) 48 lrec.setParameters( params ) ; 49 inferCaller( wrapperClass, lrec ) ; 50 lrec.setThrown( thr ) ; 51 lrec.setLoggerName( loggerName ); 52 lrec.setResourceBundle( logger.getResourceBundle() ) ; 53 logger.log( lrec ) ; 54 } 55 56 private void inferCaller( Class wrapperClass, LogRecord lrec ) 57 { 58 // Private method to infer the caller's class and method names 59 60 // Get the stack trace. 61 StackTraceElement stack[] = (new Throwable()).getStackTrace(); 62 StackTraceElement frame = null ; 63 String wcname = wrapperClass.getName() ; 64 String baseName = LogWrapperBase.class.getName() ; 65 66 // The top of the stack should always be a method in the wrapper class, 67 // or in this base class. 68 // Search back to the first method not in the wrapper class or this class. 69 int ix = 0; 70 while (ix < stack.length) { 71 frame = stack[ix]; 72 String cname = frame.getClassName(); 73 if (!cname.equals(wcname) && !cname.equals(baseName)) { 74 break; 75 } 76 77 ix++; 78 } 79 80 // Set the class and method if we are not past the end of the stack 81 // trace 82 if (ix < stack.length) { 83 lrec.setSourceClassName(frame.getClassName()); 84 lrec.setSourceMethodName(frame.getMethodName()); 85 } 86 } 87 88 protected void doLog( Level level, String key, Class wrapperClass, Throwable thr ) 89 { 90 doLog( level, key, null, wrapperClass, thr ) ; 91 } 92} 93