jdk/src/jdk.snmp/share/classes/sun/management/snmp/util/MibLogger.java
changeset 27285 1b4ca1c84d23
parent 27284 98699328cef3
parent 27263 819f5f87d485
child 27288 a3f652a37d1e
child 27503 b75a635f1ff3
equal deleted inserted replaced
27284:98699328cef3 27285:1b4ca1c84d23
     1 /*
       
     2  * Copyright (c) 2003, 2012, 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 package sun.management.snmp.util;
       
    26 
       
    27 import java.util.logging.Logger;
       
    28 import java.util.logging.Level;
       
    29 
       
    30 public class MibLogger {
       
    31 
       
    32     final Logger logger;
       
    33     final String className;
       
    34 
       
    35     static String getClassName(Class<?> clazz) {
       
    36         if (clazz == null) return null;
       
    37         if (clazz.isArray())
       
    38             return getClassName(clazz.getComponentType()) + "[]";
       
    39         final String fullname = clazz.getName();
       
    40         final int lastpoint   = fullname.lastIndexOf('.');
       
    41         final int len         = fullname.length();
       
    42         if ((lastpoint < 0) || (lastpoint >= len))
       
    43             return fullname;
       
    44         else return fullname.substring(lastpoint+1,len);
       
    45     }
       
    46 
       
    47     static String getLoggerName(Class<?> clazz) {
       
    48         if (clazz == null) return "sun.management.snmp.jvminstr";
       
    49         Package p = clazz.getPackage();
       
    50         if (p == null) return "sun.management.snmp.jvminstr";
       
    51         final String pname = p.getName();
       
    52         if (pname == null) return "sun.management.snmp.jvminstr";
       
    53         else return pname;
       
    54     }
       
    55 
       
    56     public MibLogger(Class<?> clazz) {
       
    57         this(getLoggerName(clazz),getClassName(clazz));
       
    58     }
       
    59 
       
    60     public MibLogger(Class<?> clazz, String postfix) {
       
    61         this(getLoggerName(clazz)+((postfix==null)?"":"."+postfix),
       
    62              getClassName(clazz));
       
    63     }
       
    64 
       
    65     public MibLogger(String className) {
       
    66         this("sun.management.snmp.jvminstr",className);
       
    67     }
       
    68 
       
    69     public MibLogger(String loggerName, String className) {
       
    70         Logger l = null;
       
    71         try {
       
    72             l = Logger.getLogger(loggerName);
       
    73         } catch (Exception x) {
       
    74             // OK. Should not happen
       
    75         }
       
    76         logger = l;
       
    77         this.className=className;
       
    78     }
       
    79 
       
    80     protected Logger getLogger() {
       
    81         return logger;
       
    82     }
       
    83 
       
    84     public boolean isTraceOn() {
       
    85         final Logger l = getLogger();
       
    86         if (l==null) return false;
       
    87         return l.isLoggable(Level.FINE);
       
    88     }
       
    89 
       
    90     public boolean isDebugOn() {
       
    91         final Logger l = getLogger();
       
    92         if (l==null) return false;
       
    93         return l.isLoggable(Level.FINEST);
       
    94     }
       
    95 
       
    96     public boolean isInfoOn() {
       
    97         final Logger l = getLogger();
       
    98         if (l==null) return false;
       
    99         return l.isLoggable(Level.INFO);
       
   100     }
       
   101 
       
   102     public boolean isConfigOn() {
       
   103         final Logger l = getLogger();
       
   104         if (l==null) return false;
       
   105         return l.isLoggable(Level.CONFIG);
       
   106     }
       
   107 
       
   108     public void config(String func, String msg) {
       
   109         final Logger l = getLogger();
       
   110         if (l!=null) l.logp(Level.CONFIG,className,
       
   111                         func,msg);
       
   112     }
       
   113 
       
   114     public void config(String func, Throwable t) {
       
   115         final Logger l = getLogger();
       
   116         if (l!=null) l.logp(Level.CONFIG,className,
       
   117                         func,t.toString(),t);
       
   118     }
       
   119 
       
   120     public void config(String func, String msg, Throwable t) {
       
   121         final Logger l = getLogger();
       
   122         if (l!=null) l.logp(Level.CONFIG,className,
       
   123                         func,msg,t);
       
   124     }
       
   125 
       
   126     public void error(String func, String msg) {
       
   127         final Logger l = getLogger();
       
   128         if (l!=null) l.logp(Level.SEVERE,className,
       
   129                         func,msg);
       
   130     }
       
   131 
       
   132     public void info(String func, String msg) {
       
   133         final Logger l = getLogger();
       
   134         if (l!=null) l.logp(Level.INFO,className,
       
   135                         func,msg);
       
   136     }
       
   137 
       
   138     public void info(String func, Throwable t) {
       
   139         final Logger l = getLogger();
       
   140         if (l!=null) l.logp(Level.INFO,className,
       
   141                         func,t.toString(),t);
       
   142     }
       
   143 
       
   144     public void info(String func, String msg, Throwable t) {
       
   145         final Logger l = getLogger();
       
   146         if (l!=null) l.logp(Level.INFO,className,
       
   147                         func,msg,t);
       
   148     }
       
   149 
       
   150     public void warning(String func, String msg) {
       
   151         final Logger l = getLogger();
       
   152         if (l!=null) l.logp(Level.WARNING,className,
       
   153                         func,msg);
       
   154     }
       
   155 
       
   156     public void warning(String func, Throwable t) {
       
   157         final Logger l = getLogger();
       
   158         if (l!=null) l.logp(Level.WARNING,className,
       
   159                         func,t.toString(),t);
       
   160     }
       
   161 
       
   162     public void warning(String func, String msg, Throwable t) {
       
   163         final Logger l = getLogger();
       
   164         if (l!=null) l.logp(Level.WARNING,className,
       
   165                         func,msg,t);
       
   166     }
       
   167 
       
   168     public void trace(String func, String msg) {
       
   169         final Logger l = getLogger();
       
   170         if (l!=null) l.logp(Level.FINE,className,
       
   171                         func,msg);
       
   172     }
       
   173 
       
   174     public void trace(String func, Throwable t) {
       
   175         final Logger l = getLogger();
       
   176         if (l!=null) l.logp(Level.FINE,className,
       
   177                         func,t.toString(),t);
       
   178     }
       
   179 
       
   180     public void trace(String func, String msg, Throwable t) {
       
   181         final Logger l = getLogger();
       
   182         if (l!=null) l.logp(Level.FINE,className,
       
   183                         func,msg,t);
       
   184     }
       
   185 
       
   186     public void debug(String func, String msg) {
       
   187         final Logger l = getLogger();
       
   188         if (l!=null) l.logp(Level.FINEST,className,
       
   189                         func,msg);
       
   190     }
       
   191 
       
   192     public void debug(String func, Throwable t) {
       
   193         final Logger l = getLogger();
       
   194         if (l!=null) l.logp(Level.FINEST,className,
       
   195                         func,t.toString(),t);
       
   196     }
       
   197 
       
   198     public void debug(String func, String msg, Throwable t) {
       
   199         final Logger l = getLogger();
       
   200         if (l!=null) l.logp(Level.FINEST,className,
       
   201                         func,msg,t);
       
   202     }
       
   203 }