src/share/vm/utilities/ostream.hpp

Mon, 02 Jul 2012 13:11:28 -0400

author
coleenp
date
Mon, 02 Jul 2012 13:11:28 -0400
changeset 3901
24b9c7f4cae6
parent 2964
2a241e764894
child 3992
4ee06e614636
permissions
-rw-r--r--

Merge

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

mercurial