1 /*
   2  * Copyright (c) 2012, 2018, 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  */
  24 
  25 #ifndef SHARE_VM_TRACE_TRACESTREAM_HPP
  26 #define SHARE_VM_TRACE_TRACESTREAM_HPP
  27 
  28 #include "utilities/macros.hpp"
  29 #if INCLUDE_TRACE
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/klass.hpp"
  32 #include "oops/method.hpp"
  33 #include "oops/symbol.hpp"
  34 #include "utilities/ostream.hpp"
  35 
  36 class TraceStream : public StackObj {
  37  private:
  38   outputStream& _st;
  39 
  40  public:
  41   TraceStream(outputStream& stream): _st(stream) {}
  42 
  43   void print_val(const char* label, u1 val) {
  44     _st.print("%s = " UINT32_FORMAT, label, val);
  45   }
  46 
  47   void print_val(const char* label, u2 val) {
  48     _st.print("%s = " UINT32_FORMAT, label, val);
  49   }
  50 
  51   void print_val(const char* label, s2 val) {
  52     _st.print("%s = " INT32_FORMAT, label, val);
  53   }
  54 
  55   void print_val(const char* label, u4 val) {
  56     _st.print("%s = " UINT32_FORMAT, label, val);
  57   }
  58 
  59   void print_val(const char* label, s4 val) {
  60     _st.print("%s = " INT32_FORMAT, label, val);
  61   }
  62 
  63   void print_val(const char* label, u8 val) {
  64     _st.print("%s = " UINT64_FORMAT, label, val);
  65   }
  66 
  67   void print_val(const char* label, s8 val) {
  68     _st.print("%s = " INT64_FORMAT, label, (int64_t) val);
  69   }
  70 
  71   void print_val(const char* label, bool val) {
  72     _st.print("%s = %s", label, val ? "true" : "false");
  73   }
  74 
  75   void print_val(const char* label, float val) {
  76     _st.print("%s = %f", label, val);
  77   }
  78 
  79   void print_val(const char* label, double val) {
  80     _st.print("%s = %f", label, val);
  81   }
  82 
  83   void print_val(const char* label, const Klass* const val) {
  84     ResourceMark rm;
  85     const char* description = "NULL";
  86     if (val != NULL) {
  87       Symbol* name = val->name();
  88       if (name != NULL) {
  89         description = name->as_C_string();
  90       }
  91     }
  92     _st.print("%s = %s", label, description);
  93   }
  94 
  95   void print_val(const char* label, const Method* const val) {
  96     ResourceMark rm;
  97     const char* description = "NULL";
  98     if (val != NULL) {
  99       description = val->name_and_sig_as_C_string();
 100     }
 101     _st.print("%s = %s", label, description);
 102   }
 103 
 104   void print_val(const char* label, const char* val) {
 105     _st.print("%s = '%s'", label, val);
 106   }
 107 
 108   void print(const char* val) {
 109     _st.print("%s", val);
 110   }
 111 };
 112 
 113 #endif // INCLUDE_TRACE
 114 #endif // SHARE_VM_TRACE_TRACESTREAM_HPP