1 /*
   2  * Copyright (c) 1997, 2016, 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_UTILITIES_OSTREAM_HPP
  26 #define SHARE_VM_UTILITIES_OSTREAM_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "runtime/timer.hpp"
  30 #include "utilities/globalDefinitions.hpp"
  31 
  32 DEBUG_ONLY(class ResourceMark;)
  33 
  34 // Output streams for printing
  35 //
  36 // Printing guidelines:
  37 // Where possible, please use tty->print() and tty->print_cr().
  38 // For product mode VM warnings use warning() which internally uses tty.
  39 // In places where tty is not initialized yet or too much overhead,
  40 // we may use jio_printf:
  41 //     jio_fprintf(defaultStream::output_stream(), "Message");
  42 // This allows for redirection via -XX:+DisplayVMOutputToStdout and
  43 // -XX:+DisplayVMOutputToStderr
  44 class outputStream : public ResourceObj {
  45  protected:
  46    int _indentation; // current indentation
  47    int _width;       // width of the page
  48    int _position;    // position on the current line
  49    int _newlines;    // number of '\n' output so far
  50    julong _precount; // number of chars output, less _position
  51    TimeStamp _stamp; // for time stamps
  52    char* _scratch;   // internal scratch buffer for printf
  53    size_t _scratch_len; // size of internal scratch buffer
  54 
  55    void update_position(const char* s, size_t len);
  56    static const char* do_vsnprintf(char* buffer, size_t buflen,
  57                                    const char* format, va_list ap,
  58                                    bool add_cr,
  59                                    size_t& result_len)  ATTRIBUTE_PRINTF(3, 0);
  60 
  61    // calls do_vsnprintf and writes output to stream; uses an on-stack buffer.
  62    void do_vsnprintf_and_write_with_automatic_buffer(const char* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0);
  63    // calls do_vsnprintf and writes output to stream; uses the user-provided buffer;
  64    void do_vsnprintf_and_write_with_scratch_buffer(const char* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0);
  65    // calls do_vsnprintf, then writes output to stream.
  66    void do_vsnprintf_and_write(const char* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0);
  67 
  68  public:
  69    // creation
  70    outputStream(int width = 80);
  71    outputStream(int width, bool has_time_stamps);
  72 
  73    // indentation
  74    outputStream& indent();
  75    void inc() { _indentation++; };
  76    void dec() { _indentation--; };
  77    void inc(int n) { _indentation += n; };
  78    void dec(int n) { _indentation -= n; };
  79    int  indentation() const    { return _indentation; }
  80    void set_indentation(int i) { _indentation = i;    }
  81    void fill_to(int col);
  82    void move_to(int col, int slop = 6, int min_space = 2);
  83 
  84    // sizing
  85    int width()    const { return _width;    }
  86    int position() const { return _position; }
  87    int newlines() const { return _newlines; }
  88    julong count() const { return _precount + _position; }
  89    void set_count(julong count) { _precount = count - _position; }
  90    void set_position(int pos)   { _position = pos; }
  91 
  92    // printing
  93    void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
  94    void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
  95    void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
  96    void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
  97    void print_raw(const char* str)            { write(str, strlen(str)); }
  98    void print_raw(const char* str, int len)   { write(str,         len); }
  99    void print_raw_cr(const char* str)         { write(str, strlen(str)); cr(); }
 100    void print_raw_cr(const char* str, int len){ write(str,         len); cr(); }
 101    void print_data(void* data, size_t len, bool with_ascii);
 102    void put(char ch);
 103    void sp(int count = 1);
 104    void cr();
 105    void cr_indent();
 106    void bol() { if (_position > 0)  cr(); }
 107 
 108    // Print a human readable size.
 109    // byte_size: size, in bytes, to be printed.
 110    // scale: one of 1 (byte-wise printing), sizeof(word) (word-size printing), K, M, G (scaled by KB, MB, GB respectively,
 111    //         or 0, which means the best scale is choosen dynamically.
 112    // width: printing width.
 113    void print_human_readable_size(size_t byte_size, size_t scale = 0, int width = -1);
 114 
 115    // Prints a percentage value. Values smaller than 1% but not 0 are displayed as "<1%", values
 116    // larger than 99% but not 100% are displayed as ">100%".
 117    void print_percentage(size_t total, size_t part);
 118 
 119    // Time stamp
 120    TimeStamp& time_stamp() { return _stamp; }
 121    void stamp();
 122    void stamp(bool guard, const char* prefix, const char* suffix);
 123    void stamp(bool guard) {
 124      stamp(guard, "", ": ");
 125    }
 126    // Date stamp
 127    void date_stamp(bool guard, const char* prefix, const char* suffix);
 128    // A simplified call that includes a suffix of ": "
 129    void date_stamp(bool guard) {
 130      date_stamp(guard, "", ": ");
 131    }
 132 
 133    // portable printing of 64 bit integers
 134    void print_jlong(jlong value);
 135    void print_julong(julong value);
 136 
 137    // flushing
 138    virtual void flush() {}
 139    virtual void write(const char* str, size_t len) = 0;
 140    virtual void rotate_log(bool force, outputStream* out = NULL) {} // GC log rotation
 141    virtual ~outputStream() {}   // close properly on deletion
 142 
 143    // Caller may specify their own scratch buffer to use for printing; otherwise,
 144    // an automatic buffer on the stack (with O_BUFLEN len) is used.
 145    void set_scratch_buffer(char* p, size_t len) { _scratch = p; _scratch_len = len; }
 146 
 147    void dec_cr() { dec(); cr(); }
 148    void inc_cr() { inc(); cr(); }
 149 };
 150 
 151 // standard output
 152 // ANSI C++ name collision
 153 extern outputStream* tty;           // tty output
 154 
 155 class streamIndentor : public StackObj {
 156  private:
 157   outputStream* _str;
 158   int _amount;
 159 
 160  public:
 161   streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) {
 162     _str->inc(_amount);
 163   }
 164   ~streamIndentor() { _str->dec(_amount); }
 165 };
 166 
 167 // advisory locking for the shared tty stream:
 168 class ttyLocker: StackObj {
 169   friend class ttyUnlocker;
 170  private:
 171   intx _holder;
 172 
 173  public:
 174   static intx  hold_tty();                // returns a "holder" token
 175   static void  release_tty(intx holder);  // must witness same token
 176   static bool  release_tty_if_locked();   // returns true if lock was released
 177   static void  break_tty_lock_for_safepoint(intx holder);
 178 
 179   ttyLocker()  { _holder = hold_tty(); }
 180   ~ttyLocker() { release_tty(_holder); }
 181 };
 182 
 183 // Release the tty lock if it's held and reacquire it if it was
 184 // locked.  Used to avoid lock ordering problems.
 185 class ttyUnlocker: StackObj {
 186  private:
 187   bool _was_locked;
 188  public:
 189   ttyUnlocker()  {
 190     _was_locked = ttyLocker::release_tty_if_locked();
 191   }
 192   ~ttyUnlocker() {
 193     if (_was_locked) {
 194       ttyLocker::hold_tty();
 195     }
 196   }
 197 };
 198 
 199 // for writing to strings; buffer will expand automatically
 200 class stringStream : public outputStream {
 201  protected:
 202   char*  buffer;
 203   size_t buffer_pos;
 204   size_t buffer_length;
 205   bool   buffer_fixed;
 206   DEBUG_ONLY(ResourceMark* rm;)
 207  public:
 208   stringStream(size_t initial_bufsize = 256);
 209   stringStream(char* fixed_buffer, size_t fixed_buffer_size);
 210   ~stringStream();
 211   virtual void write(const char* c, size_t len);
 212   size_t      size() { return buffer_pos; }
 213   const char* base() { return buffer; }
 214   void  reset() { buffer_pos = 0; _precount = 0; _position = 0; }
 215   char* as_string();
 216 };
 217 
 218 class fileStream : public outputStream {
 219  protected:
 220   FILE* _file;
 221   bool  _need_close;
 222  public:
 223   fileStream() { _file = NULL; _need_close = false; }
 224   fileStream(const char* file_name);
 225   fileStream(const char* file_name, const char* opentype);
 226   fileStream(FILE* file, bool need_close = false) { _file = file; _need_close = need_close; }
 227   ~fileStream();
 228   bool is_open() const { return _file != NULL; }
 229   void set_need_close(bool b) { _need_close = b;}
 230   virtual void write(const char* c, size_t len);
 231   size_t read(void *data, size_t size, size_t count) { return ::fread(data, size, count, _file); }
 232   char* readln(char *data, int count);
 233   int eof() { return feof(_file); }
 234   long fileSize();
 235   void rewind() { ::rewind(_file); }
 236   void flush();
 237 };
 238 
 239 CDS_ONLY(extern fileStream*   classlist_file;)
 240 
 241 // unlike fileStream, fdStream does unbuffered I/O by calling
 242 // open() and write() directly. It is async-safe, but output
 243 // from multiple thread may be mixed together. Used by fatal
 244 // error handler.
 245 class fdStream : public outputStream {
 246  protected:
 247   int  _fd;
 248   bool _need_close;
 249  public:
 250   fdStream(const char* file_name);
 251   fdStream(int fd = -1) { _fd = fd; _need_close = false; }
 252   ~fdStream();
 253   bool is_open() const { return _fd != -1; }
 254   void set_fd(int fd) { _fd = fd; _need_close = false; }
 255   int fd() const { return _fd; }
 256   virtual void write(const char* c, size_t len);
 257   void flush() {};
 258 };
 259 
 260 void ostream_init();
 261 void ostream_init_log();
 262 void ostream_exit();
 263 void ostream_abort();
 264 
 265 // In the non-fixed buffer case an underlying buffer will be created and
 266 // managed in C heap. Not MT-safe.
 267 class bufferedStream : public outputStream {
 268  protected:
 269   char*  buffer;
 270   size_t buffer_pos;
 271   size_t buffer_max;
 272   size_t buffer_length;
 273   bool   buffer_fixed;
 274  public:
 275   bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10);
 276   bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10);
 277   ~bufferedStream();
 278   virtual void write(const char* c, size_t len);
 279   size_t      size() { return buffer_pos; }
 280   const char* base() { return buffer; }
 281   void  reset() { buffer_pos = 0; _precount = 0; _position = 0; }
 282   char* as_string();
 283 };
 284 
 285 #define O_BUFLEN 2000   // max size of output of individual print() methods
 286 
 287 #ifndef PRODUCT
 288 
 289 class networkStream : public bufferedStream {
 290 
 291   private:
 292     int _socket;
 293 
 294   public:
 295     networkStream();
 296     ~networkStream();
 297 
 298     bool connect(const char *host, short port);
 299     bool is_open() const { return _socket != -1; }
 300     int read(char *buf, size_t len);
 301     void close();
 302     virtual void flush();
 303 };
 304 
 305 #endif
 306 
 307 #endif // SHARE_VM_UTILITIES_OSTREAM_HPP