ostream.hpp revision 222:2a1a77d3458f
1/* 2 * Copyright 1997-2005 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 */ 24 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 35class 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 43 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); 49 50 public: 51 // creation 52 outputStream(int width = 80); 53 outputStream(int width, bool has_time_stamps); 54 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); 63 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; } 71 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(); } 85 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 } 95 96 // portable printing of 64 bit integers 97 void print_jlong(jlong value); 98 void print_julong(julong value); 99 100 // flushing 101 virtual void flush() {} 102 virtual void write(const char* str, size_t len) = 0; 103 virtual ~outputStream() {} // close properly on deletion 104 105 void dec_cr() { dec(); cr(); } 106 void inc_cr() { inc(); cr(); } 107}; 108 109// standard output 110 // ANSI C++ name collision 111extern outputStream* tty; // tty output 112extern outputStream* gclog_or_tty; // stream for gc log if -Xloggc:<f>, or tty 113 114// advisory locking for the shared tty stream: 115class ttyLocker: StackObj { 116 private: 117 intx _holder; 118 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); 123 124 ttyLocker() { _holder = hold_tty(); } 125 ~ttyLocker() { release_tty(_holder); } 126}; 127 128// for writing to strings; buffer will expand automatically 129class 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}; 145 146class 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}; 158 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. 163class 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}; 177 178void ostream_init(); 179void ostream_init_log(); 180void ostream_exit(); 181void ostream_abort(); 182 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. 186class 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}; 202 203// In the non-fixed buffer case an underlying buffer will be created and 204// managed in C heap. Not MT-safe. 205class bufferedStream : public outputStream { 206 protected: 207 char* buffer; 208 size_t buffer_pos; 209 size_t buffer_max; 210 size_t buffer_length; 211 bool buffer_fixed; 212 public: 213 bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10); 214 bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10); 215 ~bufferedStream(); 216 virtual void write(const char* c, size_t len); 217 size_t size() { return buffer_pos; } 218 const char* base() { return buffer; } 219 void reset() { buffer_pos = 0; _precount = 0; _position = 0; } 220 char* as_string(); 221}; 222 223#define O_BUFLEN 2000 // max size of output of individual print() methods 224 225#ifndef PRODUCT 226 227class networkStream : public bufferedStream { 228 229 private: 230 int _socket; 231 232 public: 233 networkStream(); 234 ~networkStream(); 235 236 bool connect(const char *host, short port); 237 bool is_open() const { return _socket != -1; } 238 int read(char *buf, size_t len); 239 void close(); 240 virtual void flush(); 241}; 242 243#endif 244