1 /*
   2  * Copyright (c) 2012, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.replacements;
  24 
  25 import java.io.PrintStream;
  26 
  27 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  28 import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
  29 import org.graalvm.compiler.graph.Node.NodeIntrinsic;
  30 import org.graalvm.compiler.nodes.extended.ForeignCallNode;
  31 
  32 import jdk.vm.ci.meta.JavaKind;
  33 
  34 //JaCoCo Exclude
  35 
  36 /**
  37  * Provides {@link PrintStream}-like logging facility.
  38  */
  39 public final class Log {
  40 
  41     public static final ForeignCallDescriptor LOG_PRIMITIVE = new ForeignCallDescriptor("logPrimitive", void.class, int.class, long.class, boolean.class);
  42     public static final ForeignCallDescriptor LOG_OBJECT = new ForeignCallDescriptor("logObject", void.class, Object.class, boolean.class, boolean.class);
  43     public static final ForeignCallDescriptor LOG_PRINTF = new ForeignCallDescriptor("logPrintf", void.class, Object.class, long.class, long.class, long.class);
  44 
  45     @NodeIntrinsic(ForeignCallNode.class)
  46     private static native void log(@ConstantNodeParameter ForeignCallDescriptor logObject, Object object, boolean asString, boolean newline);
  47 
  48     @NodeIntrinsic(ForeignCallNode.class)
  49     private static native void log(@ConstantNodeParameter ForeignCallDescriptor logPrimitive, int typeChar, long value, boolean newline);
  50 
  51     @NodeIntrinsic(ForeignCallNode.class)
  52     private static native void printf(@ConstantNodeParameter ForeignCallDescriptor logPrintf, String format, long v1, long v2, long v3);
  53 
  54     public static void print(boolean value) {
  55         log(LOG_PRIMITIVE, JavaKind.Boolean.getTypeChar(), value ? 1L : 0L, false);
  56     }
  57 
  58     public static void print(byte value) {
  59         log(LOG_PRIMITIVE, JavaKind.Byte.getTypeChar(), value, false);
  60     }
  61 
  62     public static void print(char value) {
  63         log(LOG_PRIMITIVE, JavaKind.Char.getTypeChar(), value, false);
  64     }
  65 
  66     public static void print(short value) {
  67         log(LOG_PRIMITIVE, JavaKind.Short.getTypeChar(), value, false);
  68     }
  69 
  70     public static void print(int value) {
  71         log(LOG_PRIMITIVE, JavaKind.Int.getTypeChar(), value, false);
  72     }
  73 
  74     public static void print(long value) {
  75         log(LOG_PRIMITIVE, JavaKind.Long.getTypeChar(), value, false);
  76     }
  77 
  78     /**
  79      * Prints a formatted string to the log stream.
  80      *
  81      * @param format a C style printf format value that can contain at most one conversion specifier
  82      *            (i.e., a sequence of characters starting with '%').
  83      * @param value the value associated with the conversion specifier
  84      */
  85     public static void printf(String format, long value) {
  86         printf(LOG_PRINTF, format, value, 0L, 0L);
  87     }
  88 
  89     public static void printf(String format, long v1, long v2) {
  90         printf(LOG_PRINTF, format, v1, v2, 0L);
  91     }
  92 
  93     public static void printf(String format, long v1, long v2, long v3) {
  94         printf(LOG_PRINTF, format, v1, v2, v3);
  95     }
  96 
  97     public static void print(float value) {
  98         if (Float.isNaN(value)) {
  99             print("NaN");
 100         } else if (value == Float.POSITIVE_INFINITY) {
 101             print("Infinity");
 102         } else if (value == Float.NEGATIVE_INFINITY) {
 103             print("-Infinity");
 104         } else {
 105             log(LOG_PRIMITIVE, JavaKind.Float.getTypeChar(), Float.floatToRawIntBits(value), false);
 106         }
 107     }
 108 
 109     public static void print(double value) {
 110         if (Double.isNaN(value)) {
 111             print("NaN");
 112         } else if (value == Double.POSITIVE_INFINITY) {
 113             print("Infinity");
 114         } else if (value == Double.NEGATIVE_INFINITY) {
 115             print("-Infinity");
 116         } else {
 117             log(LOG_PRIMITIVE, JavaKind.Double.getTypeChar(), Double.doubleToRawLongBits(value), false);
 118         }
 119     }
 120 
 121     public static void print(String value) {
 122         log(LOG_OBJECT, value, true, false);
 123     }
 124 
 125     public static void printObject(Object o) {
 126         log(LOG_OBJECT, o, false, false);
 127     }
 128 
 129     public static void println(boolean value) {
 130         log(LOG_PRIMITIVE, JavaKind.Boolean.getTypeChar(), value ? 1L : 0L, true);
 131     }
 132 
 133     public static void println(byte value) {
 134         log(LOG_PRIMITIVE, JavaKind.Byte.getTypeChar(), value, true);
 135     }
 136 
 137     public static void println(char value) {
 138         log(LOG_PRIMITIVE, JavaKind.Char.getTypeChar(), value, true);
 139     }
 140 
 141     public static void println(short value) {
 142         log(LOG_PRIMITIVE, JavaKind.Short.getTypeChar(), value, true);
 143     }
 144 
 145     public static void println(int value) {
 146         log(LOG_PRIMITIVE, JavaKind.Int.getTypeChar(), value, true);
 147     }
 148 
 149     public static void println(long value) {
 150         log(LOG_PRIMITIVE, JavaKind.Long.getTypeChar(), value, true);
 151     }
 152 
 153     public static void println(float value) {
 154         if (Float.isNaN(value)) {
 155             println("NaN");
 156         } else if (value == Float.POSITIVE_INFINITY) {
 157             println("Infinity");
 158         } else if (value == Float.NEGATIVE_INFINITY) {
 159             println("-Infinity");
 160         } else {
 161             log(LOG_PRIMITIVE, JavaKind.Float.getTypeChar(), Float.floatToRawIntBits(value), true);
 162         }
 163     }
 164 
 165     public static void println(double value) {
 166         if (Double.isNaN(value)) {
 167             println("NaN");
 168         } else if (value == Double.POSITIVE_INFINITY) {
 169             println("Infinity");
 170         } else if (value == Double.NEGATIVE_INFINITY) {
 171             println("-Infinity");
 172         } else {
 173             log(LOG_PRIMITIVE, JavaKind.Double.getTypeChar(), Double.doubleToRawLongBits(value), true);
 174         }
 175     }
 176 
 177     public static void println(String value) {
 178         log(LOG_OBJECT, value, true, true);
 179     }
 180 
 181     public static void printlnObject(Object o) {
 182         log(LOG_OBJECT, o, false, true);
 183     }
 184 
 185     public static void println() {
 186         println("");
 187     }
 188 }