< prev index next >

src/hotspot/share/utilities/ostream.hpp

Print this page




  25 #ifndef SHARE_UTILITIES_OSTREAM_HPP
  26 #define SHARE_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  private:
  46    outputStream(const outputStream&);
  47    outputStream& operator=(const outputStream&);
  48 
  49  protected:
  50    int _indentation; // current indentation
  51    int _width;       // width of the page
  52    int _position;    // position on the current line
  53    int _newlines;    // number of '\n' output so far
  54    julong _precount; // number of chars output, less _position
  55    TimeStamp _stamp; // for time stamps
  56    char* _scratch;   // internal scratch buffer for printf
  57    size_t _scratch_len; // size of internal scratch buffer
  58 
  59    void update_position(const char* s, size_t len);
  60    static const char* do_vsnprintf(char* buffer, size_t buflen,
  61                                    const char* format, va_list ap,
  62                                    bool add_cr,
  63                                    size_t& result_len)  ATTRIBUTE_PRINTF(3, 0);
  64 
  65    // calls do_vsnprintf and writes output to stream; uses an on-stack buffer.
  66    void do_vsnprintf_and_write_with_automatic_buffer(const char* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0);
  67    // calls do_vsnprintf and writes output to stream; uses the user-provided buffer;
  68    void do_vsnprintf_and_write_with_scratch_buffer(const char* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0);


 173   ttyLocker()  { _holder = hold_tty(); }
 174   ~ttyLocker() { release_tty(_holder); }
 175 };
 176 
 177 // Release the tty lock if it's held and reacquire it if it was
 178 // locked.  Used to avoid lock ordering problems.
 179 class ttyUnlocker: StackObj {
 180  private:
 181   bool _was_locked;
 182  public:
 183   ttyUnlocker()  {
 184     _was_locked = ttyLocker::release_tty_if_locked();
 185   }
 186   ~ttyUnlocker() {
 187     if (_was_locked) {
 188       ttyLocker::hold_tty();
 189     }
 190   }
 191 };
 192 
 193 // for writing to strings; buffer will expand automatically.
 194 // Buffer will always be zero-terminated.
 195 class stringStream : public outputStream {
 196  protected:
 197   char*  buffer;
 198   size_t buffer_pos;
 199   size_t buffer_length;
 200   bool   buffer_fixed;

 201  public:
 202   // Create a stringStream using an internal buffer of initially initial_bufsize size;
 203   // will be enlarged on demand. There is no maximum cap.
 204   stringStream(size_t initial_bufsize = 256);
 205   // Creates a stringStream using a caller-provided buffer. Will truncate silently if
 206   // it overflows.
 207   stringStream(char* fixed_buffer, size_t fixed_buffer_size);
 208   ~stringStream();
 209   virtual void write(const char* c, size_t len);
 210   // Return number of characters written into buffer, excluding terminating zero and
 211   // subject to truncation in static buffer mode.
 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   virtual void write(const char* c, size_t len);
 230   size_t read(void *data, size_t size, size_t count) { return ::fread(data, size, count, _file); }
 231   char* readln(char *data, int count);


 261 // In the non-fixed buffer case an underlying buffer will be created and
 262 // managed in C heap. Not MT-safe.
 263 class bufferedStream : public outputStream {
 264  protected:
 265   char*  buffer;
 266   size_t buffer_pos;
 267   size_t buffer_max;
 268   size_t buffer_length;
 269   bool   buffer_fixed;
 270  public:
 271   bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10);
 272   bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10);
 273   ~bufferedStream();
 274   virtual void write(const char* c, size_t len);
 275   size_t      size() { return buffer_pos; }
 276   const char* base() { return buffer; }
 277   void  reset() { buffer_pos = 0; _precount = 0; _position = 0; }
 278   char* as_string();
 279 };
 280 
 281 #define O_BUFLEN 2000   // max size of output of individual print() methods
 282 
 283 #ifndef PRODUCT
 284 
 285 class networkStream : public bufferedStream {
 286 
 287   private:
 288     int _socket;
 289 
 290   public:
 291     networkStream();
 292     ~networkStream();
 293 
 294     bool connect(const char *host, short port);
 295     bool is_open() const { return _socket != -1; }
 296     int read(char *buf, size_t len);
 297     void close();
 298     virtual void flush();
 299 };
 300 
 301 #endif


  25 #ifndef SHARE_UTILITIES_OSTREAM_HPP
  26 #define SHARE_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);


 169   ttyLocker()  { _holder = hold_tty(); }
 170   ~ttyLocker() { release_tty(_holder); }
 171 };
 172 
 173 // Release the tty lock if it's held and reacquire it if it was
 174 // locked.  Used to avoid lock ordering problems.
 175 class ttyUnlocker: StackObj {
 176  private:
 177   bool _was_locked;
 178  public:
 179   ttyUnlocker()  {
 180     _was_locked = ttyLocker::release_tty_if_locked();
 181   }
 182   ~ttyUnlocker() {
 183     if (_was_locked) {
 184       ttyLocker::hold_tty();
 185     }
 186   }
 187 };
 188 
 189 // for writing to strings; buffer will expand automatically

 190 class stringStream : public outputStream {
 191  protected:
 192   char*  buffer;
 193   size_t buffer_pos;
 194   size_t buffer_length;
 195   bool   buffer_fixed;
 196   DEBUG_ONLY(ResourceMark* rm;)
 197  public:


 198   stringStream(size_t initial_bufsize = 256);


 199   stringStream(char* fixed_buffer, size_t fixed_buffer_size);
 200   ~stringStream();
 201   virtual void write(const char* c, size_t len);


 202   size_t      size() { return buffer_pos; }
 203   const char* base() { return buffer; }
 204   void  reset() { buffer_pos = 0; _precount = 0; _position = 0; }
 205   char* as_string();
 206 };
 207 
 208 class fileStream : public outputStream {
 209  protected:
 210   FILE* _file;
 211   bool  _need_close;
 212  public:
 213   fileStream() { _file = NULL; _need_close = false; }
 214   fileStream(const char* file_name);
 215   fileStream(const char* file_name, const char* opentype);
 216   fileStream(FILE* file, bool need_close = false) { _file = file; _need_close = need_close; }
 217   ~fileStream();
 218   bool is_open() const { return _file != NULL; }
 219   virtual void write(const char* c, size_t len);
 220   size_t read(void *data, size_t size, size_t count) { return ::fread(data, size, count, _file); }
 221   char* readln(char *data, int count);


 251 // In the non-fixed buffer case an underlying buffer will be created and
 252 // managed in C heap. Not MT-safe.
 253 class bufferedStream : public outputStream {
 254  protected:
 255   char*  buffer;
 256   size_t buffer_pos;
 257   size_t buffer_max;
 258   size_t buffer_length;
 259   bool   buffer_fixed;
 260  public:
 261   bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10);
 262   bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10);
 263   ~bufferedStream();
 264   virtual void write(const char* c, size_t len);
 265   size_t      size() { return buffer_pos; }
 266   const char* base() { return buffer; }
 267   void  reset() { buffer_pos = 0; _precount = 0; _position = 0; }
 268   char* as_string();
 269 };
 270 
 271 #define O_BUFLEN 20000   // max size of output of individual print() methods
 272 
 273 #ifndef PRODUCT
 274 
 275 class networkStream : public bufferedStream {
 276 
 277   private:
 278     int _socket;
 279 
 280   public:
 281     networkStream();
 282     ~networkStream();
 283 
 284     bool connect(const char *host, short port);
 285     bool is_open() const { return _socket != -1; }
 286     int read(char *buf, size_t len);
 287     void close();
 288     virtual void flush();
 289 };
 290 
 291 #endif
< prev index next >