jdk/src/java.desktop/share/classes/sun/java2d/marlin/MarlinUtils.java
changeset 34419 14108cfd0823
parent 34417 57a3863abbb4
child 34422 438c2710ab20
equal deleted inserted replaced
34418:a947f6b4e0b3 34419:14108cfd0823
       
     1 /*
       
     2  * Copyright (c) 2015, 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 
       
    26 package sun.java2d.marlin;
       
    27 
       
    28 import jdk.internal.misc.JavaLangAccess;
       
    29 import jdk.internal.misc.SharedSecrets;
       
    30 
       
    31 public final class MarlinUtils {
       
    32     // TODO: use sun.util.logging.PlatformLogger once in JDK9
       
    33     private static final java.util.logging.Logger log;
       
    34 
       
    35     static {
       
    36         if (MarlinConst.useLogger) {
       
    37             log = java.util.logging.Logger.getLogger("sun.java2d.marlin");
       
    38         } else {
       
    39             log = null;
       
    40         }
       
    41     }
       
    42 
       
    43     private MarlinUtils() {
       
    44         // no-op
       
    45     }
       
    46 
       
    47     public static void logInfo(final String msg) {
       
    48         if (MarlinConst.useLogger) {
       
    49             log.info(msg);
       
    50         } else if (MarlinConst.enableLogs) {
       
    51             System.out.print("INFO: ");
       
    52             System.out.println(msg);
       
    53         }
       
    54     }
       
    55 
       
    56     public static void logException(final String msg, final Throwable th) {
       
    57         if (MarlinConst.useLogger) {
       
    58 //            log.warning(msg, th);
       
    59             log.log(java.util.logging.Level.WARNING, msg, th);
       
    60         } else if (MarlinConst.enableLogs) {
       
    61             System.out.print("WARNING: ");
       
    62             System.out.println(msg);
       
    63             th.printStackTrace(System.err);
       
    64         }
       
    65     }
       
    66 
       
    67     // Returns the caller's class and method's name; best effort
       
    68     // if cannot infer, return the logger's name.
       
    69     static String getCallerInfo(String className) {
       
    70         String sourceClassName = null;
       
    71         String sourceMethodName = null;
       
    72 
       
    73         JavaLangAccess access = SharedSecrets.getJavaLangAccess();
       
    74         Throwable throwable = new Throwable();
       
    75         int depth = access.getStackTraceDepth(throwable);
       
    76 
       
    77         boolean lookingForClassName = true;
       
    78         for (int ix = 0; ix < depth; ix++) {
       
    79             // Calling getStackTraceElement directly prevents the VM
       
    80             // from paying the cost of building the entire stack frame.
       
    81             StackTraceElement frame = access.getStackTraceElement(throwable, ix);
       
    82             String cname = frame.getClassName();
       
    83             if (lookingForClassName) {
       
    84                 // Skip all frames until we have found the first frame having the class name.
       
    85                 if (cname.equals(className)) {
       
    86                     lookingForClassName = false;
       
    87                 }
       
    88             } else {
       
    89                 if (!cname.equals(className)) {
       
    90                     // We've found the relevant frame.
       
    91                     sourceClassName = cname;
       
    92                     sourceMethodName = frame.getMethodName();
       
    93                     break;
       
    94                 }
       
    95             }
       
    96         }
       
    97 
       
    98         if (sourceClassName != null) {
       
    99             return sourceClassName + " " + sourceMethodName;
       
   100         } else {
       
   101             return "unknown";
       
   102         }
       
   103     }
       
   104 }