src/share/vm/utilities/ostream.hpp

Wed, 02 Jul 2008 12:55:16 -0700

author
xdono
date
Wed, 02 Jul 2008 12:55:16 -0700
changeset 631
d1605aabd0a1
parent 535
c7c777385a15
child 670
9c2ecc2ffb12
permissions
-rw-r--r--

6719955: Update copyright year
Summary: Update copyright year for files that have been modified in 2008
Reviewed-by: ohair, tbell

     1 /*
     2  * Copyright 1997-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 // Output streams for printing
    26 //
    27 // Printing guidelines:
    28 // Where possible, please use tty->print() and tty->print_cr().
    29 // For product mode VM warnings use warning() which internally uses tty.
    30 // In places where tty is not initialized yet or too much overhead,
    31 // we may use jio_printf:
    32 //     jio_fprintf(defaultStream::output_stream(), "Message");
    33 // This allows for redirection via -XX:+DisplayVMOutputToStdout and
    34 // -XX:+DisplayVMOutputToStderr
    35 class outputStream : public ResourceObj {
    36  protected:
    37    int _indentation; // current indentation
    38    int _width;       // width of the page
    39    int _position;    // position on the current line
    40    int _newlines;    // number of '\n' output so far
    41    julong _precount; // number of chars output, less _position
    42    TimeStamp _stamp; // for time stamps
    44    void update_position(const char* s, size_t len);
    45    static const char* do_vsnprintf(char* buffer, size_t buflen,
    46                                    const char* format, va_list ap,
    47                                    bool add_cr,
    48                                    size_t& result_len);
    50  public:
    51    // creation
    52    outputStream(int width = 80);
    53    outputStream(int width, bool has_time_stamps);
    55    // indentation
    56    void indent();
    57    void inc() { _indentation++; };
    58    void dec() { _indentation--; };
    59    int  indentation() const    { return _indentation; }
    60    void set_indentation(int i) { _indentation = i;    }
    61    void fill_to(int col);
    62    void move_to(int col, int slop = 6, int min_space = 2);
    64    // sizing
    65    int width()    const { return _width;    }
    66    int position() const { return _position; }
    67    int newlines() const { return _newlines; }
    68    julong count() const { return _precount + _position; }
    69    void set_count(julong count) { _precount = count - _position; }
    70    void set_position(int pos)   { _position = pos; }
    72    // printing
    73    void print(const char* format, ...);
    74    void print_cr(const char* format, ...);
    75    void vprint(const char *format, va_list argptr);
    76    void vprint_cr(const char* format, va_list argptr);
    77    void print_raw(const char* str)            { write(str, strlen(str)); }
    78    void print_raw(const char* str, int len)   { write(str,         len); }
    79    void print_raw_cr(const char* str)         { write(str, strlen(str)); cr(); }
    80    void print_raw_cr(const char* str, int len){ write(str,         len); cr(); }
    81    void put(char ch);
    82    void sp(int count = 1);
    83    void cr();
    84    void bol() { if (_position > 0)  cr(); }
    86    // Time stamp
    87    TimeStamp& time_stamp() { return _stamp; }
    88    void stamp();
    89    // Date stamp
    90    void date_stamp(bool guard, const char* prefix, const char* suffix);
    91    // A simplified call that includes a suffix of ": "
    92    void date_stamp(bool guard) {
    93      date_stamp(guard, "", ": ");
    94    }
    96    // portable printing of 64 bit integers
    97    void print_jlong(jlong value);
    98    void print_julong(julong value);
   100    // flushing
   101    virtual void flush() {}
   102    virtual void write(const char* str, size_t len) = 0;
   103    virtual ~outputStream() {}  // close properly on deletion
   105    void dec_cr() { dec(); cr(); }
   106    void inc_cr() { inc(); cr(); }
   107 };
   109 // standard output
   110                                 // ANSI C++ name collision
   111 extern outputStream* tty;           // tty output
   112 extern outputStream* gclog_or_tty;  // stream for gc log if -Xloggc:<f>, or tty
   114 // advisory locking for the shared tty stream:
   115 class ttyLocker: StackObj {
   116  private:
   117   intx _holder;
   119  public:
   120   static intx  hold_tty();                // returns a "holder" token
   121   static void  release_tty(intx holder);  // must witness same token
   122   static void  break_tty_lock_for_safepoint(intx holder);
   124   ttyLocker()  { _holder = hold_tty(); }
   125   ~ttyLocker() { release_tty(_holder); }
   126 };
   128 // for writing to strings; buffer will expand automatically
   129 class stringStream : public outputStream {
   130  protected:
   131   char*  buffer;
   132   size_t buffer_pos;
   133   size_t buffer_length;
   134   bool   buffer_fixed;
   135  public:
   136   stringStream(size_t initial_bufsize = 256);
   137   stringStream(char* fixed_buffer, size_t fixed_buffer_size);
   138   ~stringStream();
   139   virtual void write(const char* c, size_t len);
   140   size_t      size() { return buffer_pos; }
   141   const char* base() { return buffer; }
   142   void  reset() { buffer_pos = 0; _precount = 0; _position = 0; }
   143   char* as_string();
   144 };
   146 class fileStream : public outputStream {
   147  protected:
   148   FILE* _file;
   149   bool  _need_close;
   150  public:
   151   fileStream(const char* file_name);
   152   fileStream(FILE* file) { _file = file; _need_close = false; }
   153   ~fileStream();
   154   bool is_open() const { return _file != NULL; }
   155   virtual void write(const char* c, size_t len);
   156   void flush();
   157 };
   159 // unlike fileStream, fdStream does unbuffered I/O by calling
   160 // open() and write() directly. It is async-safe, but output
   161 // from multiple thread may be mixed together. Used by fatal
   162 // error handler.
   163 class fdStream : public outputStream {
   164  protected:
   165   int  _fd;
   166   bool _need_close;
   167  public:
   168   fdStream(const char* file_name);
   169   fdStream(int fd = -1) { _fd = fd; _need_close = false; }
   170   ~fdStream();
   171   bool is_open() const { return _fd != -1; }
   172   void set_fd(int fd) { _fd = fd; _need_close = false; }
   173   int fd() const { return _fd; }
   174   virtual void write(const char* c, size_t len);
   175   void flush() {};
   176 };
   178 void ostream_init();
   179 void ostream_init_log();
   180 void ostream_exit();
   181 void ostream_abort();
   183 // staticBufferStream uses a user-supplied buffer for all formatting.
   184 // Used for safe formatting during fatal error handling.  Not MT safe.
   185 // Do not share the stream between multiple threads.
   186 class staticBufferStream : public outputStream {
   187  private:
   188   char* _buffer;
   189   size_t _buflen;
   190   outputStream* _outer_stream;
   191  public:
   192   staticBufferStream(char* buffer, size_t buflen,
   193                      outputStream *outer_stream);
   194   ~staticBufferStream() {};
   195   virtual void write(const char* c, size_t len);
   196   void flush();
   197   void print(const char* format, ...);
   198   void print_cr(const char* format, ...);
   199   void vprint(const char *format, va_list argptr);
   200   void vprint_cr(const char* format, va_list argptr);
   201 };
   203 // In the non-fixed buffer case an underlying buffer will be created and
   204 // managed in C heap. Not MT-safe.
   205 class bufferedStream : public outputStream {
   206  protected:
   207   char*  buffer;
   208   size_t buffer_pos;
   209   size_t buffer_length;
   210   bool   buffer_fixed;
   211  public:
   212   bufferedStream(size_t initial_bufsize = 256);
   213   bufferedStream(char* fixed_buffer, size_t fixed_buffer_size);
   214   ~bufferedStream();
   215   virtual void write(const char* c, size_t len);
   216   size_t      size() { return buffer_pos; }
   217   const char* base() { return buffer; }
   218   void  reset() { buffer_pos = 0; _precount = 0; _position = 0; }
   219   char* as_string();
   220 };
   222 #define O_BUFLEN 2000   // max size of output of individual print() methods
   224 #ifndef PRODUCT
   226 class networkStream : public bufferedStream {
   228   private:
   229     int _socket;
   231   public:
   232     networkStream();
   233     ~networkStream();
   235     bool connect(const char *host, short port);
   236     bool is_open() const { return _socket != -1; }
   237     int read(char *buf, size_t len);
   238     void close();
   239     virtual void flush();
   240 };
   242 #endif

mercurial