src/share/vm/utilities/ostream.hpp

Fri, 16 Aug 2013 10:06:58 -0700

author
dcubed
date
Fri, 16 Aug 2013 10:06:58 -0700
changeset 5529
e5003079dfa5
parent 5409
dbc0b5dc08f5
child 5683
621eda7235d2
permissions
-rw-r--r--

Merge

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

mercurial