< prev index next >

src/share/vm/utilities/ostream.hpp

Print this page
rev 9831 : 8146905 - cleanup ostream, staticBufferStream
Summary: get rid of staticBufferStream and implement the use-caller-provided-scratch-buffer feature in a simpler way.


  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 
  53    void update_position(const char* s, size_t len);
  54    static const char* do_vsnprintf(char* buffer, size_t buflen,
  55                                    const char* format, va_list ap,
  56                                    bool add_cr,
  57                                    size_t& result_len)  ATTRIBUTE_PRINTF(3, 0);
  58 







  59  public:
  60    // creation
  61    outputStream(int width = 80);
  62    outputStream(int width, bool has_time_stamps);
  63 
  64    // indentation
  65    outputStream& indent();
  66    void inc() { _indentation++; };
  67    void dec() { _indentation--; };
  68    void inc(int n) { _indentation += n; };
  69    void dec(int n) { _indentation -= n; };
  70    int  indentation() const    { return _indentation; }
  71    void set_indentation(int i) { _indentation = i;    }
  72    void fill_to(int col);
  73    void move_to(int col, int slop = 6, int min_space = 2);
  74 
  75    // sizing
  76    int width()    const { return _width;    }
  77    int position() const { return _position; }
  78    int newlines() const { return _newlines; }


 102    void stamp(bool guard) {
 103      stamp(guard, "", ": ");
 104    }
 105    // Date stamp
 106    void date_stamp(bool guard, const char* prefix, const char* suffix);
 107    // A simplified call that includes a suffix of ": "
 108    void date_stamp(bool guard) {
 109      date_stamp(guard, "", ": ");
 110    }
 111 
 112    // portable printing of 64 bit integers
 113    void print_jlong(jlong value);
 114    void print_julong(julong value);
 115 
 116    // flushing
 117    virtual void flush() {}
 118    virtual void write(const char* str, size_t len) = 0;
 119    virtual void rotate_log(bool force, outputStream* out = NULL) {} // GC log rotation
 120    virtual ~outputStream() {}   // close properly on deletion
 121 




 122    void dec_cr() { dec(); cr(); }
 123    void inc_cr() { inc(); cr(); }
 124 };
 125 
 126 // standard output
 127 // ANSI C++ name collision
 128 extern outputStream* tty;           // tty output
 129 
 130 class streamIndentor : public StackObj {
 131  private:
 132   outputStream* _str;
 133   int _amount;
 134 
 135  public:
 136   streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) {
 137     _str->inc(_amount);
 138   }
 139   ~streamIndentor() { _str->dec(_amount); }
 140 };
 141 


 232   virtual void write(const char* c, size_t len);
 233   void flush() {};
 234 };
 235 
 236 class logStream : public outputStream {
 237 private:
 238   stringStream _current_line;
 239   void (*_log_func)(const char* fmt, ...);
 240 public:
 241   void write(const char* s, size_t len);
 242   logStream(void (*log_func)(const char* fmt, ...)) : _log_func(log_func) {}
 243   ~logStream() {
 244     guarantee(_current_line.size() == 0, "Buffer not flushed. Missing call to print_cr()?");
 245   }
 246 };
 247 
 248 void ostream_init();
 249 void ostream_init_log();
 250 void ostream_exit();
 251 void ostream_abort();
 252 
 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, ...) ATTRIBUTE_PRINTF(2, 3);
 268   void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
 269   void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
 270   void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
 271 };
 272 
 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 };




  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; }


 111    void stamp(bool guard) {
 112      stamp(guard, "", ": ");
 113    }
 114    // Date stamp
 115    void date_stamp(bool guard, const char* prefix, const char* suffix);
 116    // A simplified call that includes a suffix of ": "
 117    void date_stamp(bool guard) {
 118      date_stamp(guard, "", ": ");
 119    }
 120 
 121    // portable printing of 64 bit integers
 122    void print_jlong(jlong value);
 123    void print_julong(julong value);
 124 
 125    // flushing
 126    virtual void flush() {}
 127    virtual void write(const char* str, size_t len) = 0;
 128    virtual void rotate_log(bool force, outputStream* out = NULL) {} // GC log rotation
 129    virtual ~outputStream() {}   // close properly on deletion
 130 
 131    // Caller may specify an own scratch buffer to use for printing; otherwise,
 132    // an automatic buffer on the stack (with O_BUFLEN len) is used.
 133    void set_scratch_buffer(char* p, size_t len) { _scratch = p; _scratch_len = len; }
 134 
 135    void dec_cr() { dec(); cr(); }
 136    void inc_cr() { inc(); cr(); }
 137 };
 138 
 139 // standard output
 140 // ANSI C++ name collision
 141 extern outputStream* tty;           // tty output
 142 
 143 class streamIndentor : public StackObj {
 144  private:
 145   outputStream* _str;
 146   int _amount;
 147 
 148  public:
 149   streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) {
 150     _str->inc(_amount);
 151   }
 152   ~streamIndentor() { _str->dec(_amount); }
 153 };
 154 


 245   virtual void write(const char* c, size_t len);
 246   void flush() {};
 247 };
 248 
 249 class logStream : public outputStream {
 250 private:
 251   stringStream _current_line;
 252   void (*_log_func)(const char* fmt, ...);
 253 public:
 254   void write(const char* s, size_t len);
 255   logStream(void (*log_func)(const char* fmt, ...)) : _log_func(log_func) {}
 256   ~logStream() {
 257     guarantee(_current_line.size() == 0, "Buffer not flushed. Missing call to print_cr()?");
 258   }
 259 };
 260 
 261 void ostream_init();
 262 void ostream_init_log();
 263 void ostream_exit();
 264 void ostream_abort();




















 265 
 266 // In the non-fixed buffer case an underlying buffer will be created and
 267 // managed in C heap. Not MT-safe.
 268 class bufferedStream : public outputStream {
 269  protected:
 270   char*  buffer;
 271   size_t buffer_pos;
 272   size_t buffer_max;
 273   size_t buffer_length;
 274   bool   buffer_fixed;
 275  public:
 276   bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10);
 277   bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10);
 278   ~bufferedStream();
 279   virtual void write(const char* c, size_t len);
 280   size_t      size() { return buffer_pos; }
 281   const char* base() { return buffer; }
 282   void  reset() { buffer_pos = 0; _precount = 0; _position = 0; }
 283   char* as_string();
 284 };


< prev index next >