1 /* 2 * Copyright (c) 1997, 2010, 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 */ 24 25 #ifndef SHARE_VM_UTILITIES_OSTREAM_HPP 26 #define SHARE_VM_UTILITIES_OSTREAM_HPP 27 28 #include "memory/allocation.hpp" 29 #include "runtime/timer.hpp" 30 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 49 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); 55 56 public: 57 // creation 58 outputStream(int width = 80); 59 outputStream(int width, bool has_time_stamps); 60 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); 69 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; } 77 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(); } 91 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 } 105 106 // portable printing of 64 bit integers 107 void print_jlong(jlong value); 108 void print_julong(julong value); 109 110 // flushing 111 virtual void flush() {} 112 virtual void write(const char* str, size_t len) = 0; 113 virtual ~outputStream() {} // close properly on deletion 114 115 void dec_cr() { dec(); cr(); } 116 void inc_cr() { inc(); cr(); } 117 }; 118 119 // standard output 120 // ANSI C++ name collision 121 extern outputStream* tty; // tty output 122 extern outputStream* gclog_or_tty; // stream for gc log if -Xloggc:<f>, or tty 123 124 // advisory locking for the shared tty stream: 125 class ttyLocker: StackObj { 126 private: 127 intx _holder; 128 129 public: 130 static intx hold_tty(); // returns a "holder" token 131 static void release_tty(intx holder); // must witness same token 132 static void break_tty_lock_for_safepoint(intx holder); 133 134 ttyLocker() { _holder = hold_tty(); } 135 ~ttyLocker() { release_tty(_holder); } 136 }; 137 138 // for writing to strings; buffer will expand automatically 139 class stringStream : public outputStream { 140 protected: 141 char* buffer; 142 size_t buffer_pos; 143 size_t buffer_length; 144 bool buffer_fixed; 145 public: 146 stringStream(size_t initial_bufsize = 256); 147 stringStream(char* fixed_buffer, size_t fixed_buffer_size); 148 ~stringStream(); 149 virtual void write(const char* c, size_t len); 150 size_t size() { return buffer_pos; } 151 const char* base() { return buffer; } 152 void reset() { buffer_pos = 0; _precount = 0; _position = 0; } 153 char* as_string(); 154 }; 155 156 class fileStream : public outputStream { 157 protected: 158 FILE* _file; 159 bool _need_close; 160 public: 161 fileStream(const char* file_name); 162 fileStream(const char* file_name, const char* opentype); 163 fileStream(FILE* file) { _file = file; _need_close = false; } 164 ~fileStream(); 165 bool is_open() const { return _file != NULL; } 166 void set_need_close(bool b) { _need_close = b;} 167 virtual void write(const char* c, size_t len); 168 size_t read(void *data, size_t size, size_t count) { return ::fread(data, size, count, _file); } 169 char* readln(char *data, int count); 170 int eof() { return feof(_file); } 171 long fileSize(); 172 void rewind() { ::rewind(_file); } 173 void flush(); 174 }; 175 176 // unlike fileStream, fdStream does unbuffered I/O by calling 177 // open() and write() directly. It is async-safe, but output 178 // from multiple thread may be mixed together. Used by fatal 179 // error handler. 180 class fdStream : public outputStream { 181 protected: 182 int _fd; 183 bool _need_close; 184 public: 185 fdStream(const char* file_name); 186 fdStream(int fd = -1) { _fd = fd; _need_close = false; } 187 ~fdStream(); 188 bool is_open() const { return _fd != -1; } 189 void set_fd(int fd) { _fd = fd; _need_close = false; } 190 int fd() const { return _fd; } 191 virtual void write(const char* c, size_t len); 192 void flush() {}; 193 }; 194 195 void ostream_init(); 196 void ostream_init_log(); 197 void ostream_exit(); 198 void ostream_abort(); 199 200 // staticBufferStream uses a user-supplied buffer for all formatting. 201 // Used for safe formatting during fatal error handling. Not MT safe. 202 // Do not share the stream between multiple threads. 203 class staticBufferStream : public outputStream { 204 private: 205 char* _buffer; 206 size_t _buflen; 207 outputStream* _outer_stream; 208 public: 209 staticBufferStream(char* buffer, size_t buflen, 210 outputStream *outer_stream); 211 ~staticBufferStream() {}; 212 virtual void write(const char* c, size_t len); 213 void flush(); 214 void print(const char* format, ...); 215 void print_cr(const char* format, ...); 216 void vprint(const char *format, va_list argptr); 217 void vprint_cr(const char* format, va_list argptr); 218 }; 219 220 // In the non-fixed buffer case an underlying buffer will be created and 221 // managed in C heap. Not MT-safe. 222 class bufferedStream : public outputStream { 223 protected: 224 char* buffer; 225 size_t buffer_pos; 226 size_t buffer_max; 227 size_t buffer_length; 228 bool buffer_fixed; 229 public: 230 bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10); 231 bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10); 232 ~bufferedStream(); 233 virtual void write(const char* c, size_t len); 234 size_t size() { return buffer_pos; } 235 const char* base() { return buffer; } 236 void reset() { buffer_pos = 0; _precount = 0; _position = 0; } 237 char* as_string(); 238 }; 239 240 #define O_BUFLEN 2000 // max size of output of individual print() methods 241 242 #ifndef PRODUCT 243 244 class networkStream : public bufferedStream { 245 246 private: 247 int _socket; 248 249 public: 250 networkStream(); 251 ~networkStream(); 252 253 bool connect(const char *host, short port); 254 bool is_open() const { return _socket != -1; } 255 int read(char *buf, size_t len); 256 void close(); 257 virtual void flush(); 258 }; 259 260 #endif 261 262 #endif // SHARE_VM_UTILITIES_OSTREAM_HPP