ostream.hpp revision 10720:36a42531fbe9
1226584Sdim/* 2226584Sdim * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 3353358Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4353358Sdim * 5353358Sdim * This code is free software; you can redistribute it and/or modify it 6226584Sdim * under the terms of the GNU General Public License version 2 only, as 7226584Sdim * published by the Free Software Foundation. 8226584Sdim * 9226584Sdim * This code is distributed in the hope that it will be useful, but WITHOUT 10226584Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11226584Sdim * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12226584Sdim * version 2 for more details (a copy is included in the LICENSE file that 13226584Sdim * accompanied this code). 14226584Sdim * 15226584Sdim * You should have received a copy of the GNU General Public License version 16226584Sdim * 2 along with this work; if not, write to the Free Software Foundation, 17226584Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18321369Sdim * 19327952Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20327952Sdim * or visit www.oracle.com if you need additional information or have any 21327952Sdim * questions. 22360784Sdim * 23249423Sdim */ 24249423Sdim 25280031Sdim#ifndef SHARE_VM_UTILITIES_OSTREAM_HPP 26226584Sdim#define SHARE_VM_UTILITIES_OSTREAM_HPP 27226584Sdim 28276479Sdim#include "memory/allocation.hpp" 29276479Sdim#include "runtime/timer.hpp" 30226584Sdim#include "utilities/globalDefinitions.hpp" 31226584Sdim 32226584SdimDEBUG_ONLY(class ResourceMark;) 33226584Sdim 34226584Sdim// Output streams for printing 35226584Sdim// 36226584Sdim// Printing guidelines: 37226584Sdim// Where possible, please use tty->print() and tty->print_cr(). 38226584Sdim// For product mode VM warnings use warning() which internally uses tty. 39226584Sdim// In places where tty is not initialized yet or too much overhead, 40276479Sdim// we may use jio_printf: 41226584Sdim// jio_fprintf(defaultStream::output_stream(), "Message"); 42226584Sdim// This allows for redirection via -XX:+DisplayVMOutputToStdout and 43226584Sdim// -XX:+DisplayVMOutputToStderr 44226584Sdimclass outputStream : public ResourceObj { 45226584Sdim protected: 46226584Sdim int _indentation; // current indentation 47226584Sdim int _width; // width of the page 48276479Sdim int _position; // position on the current line 49226584Sdim int _newlines; // number of '\n' output so far 50226584Sdim julong _precount; // number of chars output, less _position 51226584Sdim TimeStamp _stamp; // for time stamps 52226584Sdim char* _scratch; // internal scratch buffer for printf 53226584Sdim size_t _scratch_len; // size of internal scratch buffer 54309124Sdim 55226584Sdim void update_position(const char* s, size_t len); 56226584Sdim static const char* do_vsnprintf(char* buffer, size_t buflen, 57226584Sdim const char* format, va_list ap, 58226584Sdim bool add_cr, 59234353Sdim size_t& result_len) ATTRIBUTE_PRINTF(3, 0); 60226584Sdim 61321369Sdim // calls do_vsnprintf and writes output to stream; uses an on-stack buffer. 62234353Sdim void do_vsnprintf_and_write_with_automatic_buffer(const char* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0); 63226584Sdim // calls do_vsnprintf and writes output to stream; uses the user-provided buffer; 64309124Sdim void do_vsnprintf_and_write_with_scratch_buffer(const char* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0); 65309124Sdim // calls do_vsnprintf, then writes output to stream. 66226584Sdim void do_vsnprintf_and_write(const char* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0); 67309124Sdim 68226584Sdim public: 69226584Sdim // creation 70226584Sdim outputStream(int width = 80); 71309124Sdim outputStream(int width, bool has_time_stamps); 72309124Sdim 73309124Sdim // indentation 74226584Sdim outputStream& indent(); 75226584Sdim void inc() { _indentation++; }; 76226584Sdim void dec() { _indentation--; }; 77226584Sdim void inc(int n) { _indentation += n; }; 78226584Sdim void dec(int n) { _indentation -= n; }; 79226584Sdim int indentation() const { return _indentation; } 80226584Sdim void set_indentation(int i) { _indentation = i; } 81226584Sdim void fill_to(int col); 82226584Sdim void move_to(int col, int slop = 6, int min_space = 2); 83360784Sdim 84360784Sdim // sizing 85226584Sdim int width() const { return _width; } 86226584Sdim int position() const { return _position; } 87226584Sdim int newlines() const { return _newlines; } 88226584Sdim julong count() const { return _precount + _position; } 89360784Sdim void set_count(julong count) { _precount = count - _position; } 90226584Sdim void set_position(int pos) { _position = pos; } 91360784Sdim 92226584Sdim // printing 93360784Sdim void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 94226584Sdim void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 95226584Sdim void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); 96341825Sdim void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); 97226584Sdim void print_raw(const char* str) { write(str, strlen(str)); } 98249423Sdim void print_raw(const char* str, int len) { write(str, len); } 99249423Sdim void print_raw_cr(const char* str) { write(str, strlen(str)); cr(); } 100344779Sdim void print_raw_cr(const char* str, int len){ write(str, len); cr(); } 101344779Sdim void print_data(void* data, size_t len, bool with_ascii); 102341825Sdim void put(char ch); 103249423Sdim void sp(int count = 1); 104249423Sdim void cr(); 105249423Sdim void bol() { if (_position > 0) cr(); } 106226584Sdim 107261991Sdim // Time stamp 108226584Sdim TimeStamp& time_stamp() { return _stamp; } 109327952Sdim void stamp(); 110327952Sdim void stamp(bool guard, const char* prefix, const char* suffix); 111226584Sdim void stamp(bool guard) { 112226584Sdim stamp(guard, "", ": "); 113226584Sdim } 114226584Sdim // Date stamp 115341825Sdim void date_stamp(bool guard, const char* prefix, const char* suffix); 116226584Sdim // A simplified call that includes a suffix of ": " 117226584Sdim void date_stamp(bool guard) { 118341825Sdim date_stamp(guard, "", ": "); 119226584Sdim } 120226584Sdim 121226584Sdim // portable printing of 64 bit integers 122239462Sdim void print_jlong(jlong value); 123239462Sdim void print_julong(julong value); 124239462Sdim 125239462Sdim // flushing 126239462Sdim virtual void flush() {} 127341825Sdim virtual void write(const char* str, size_t len) = 0; 128226584Sdim virtual void rotate_log(bool force, outputStream* out = NULL) {} // GC log rotation 129226584Sdim virtual ~outputStream() {} // close properly on deletion 130341825Sdim 131226584Sdim // Caller may specify their own scratch buffer to use for printing; otherwise, 132226584Sdim // an automatic buffer on the stack (with O_BUFLEN len) is used. 133226584Sdim void set_scratch_buffer(char* p, size_t len) { _scratch = p; _scratch_len = len; } 134226584Sdim 135226584Sdim void dec_cr() { dec(); cr(); } 136249423Sdim void inc_cr() { inc(); cr(); } 137249423Sdim}; 138341825Sdim 139249423Sdim// standard output 140341825Sdim// ANSI C++ name collision 141249423Sdimextern outputStream* tty; // tty output 142249423Sdim 143249423Sdimclass streamIndentor : public StackObj { 144226584Sdim private: 145226584Sdim outputStream* _str; 146226584Sdim int _amount; 147321369Sdim 148321369Sdim public: 149341825Sdim streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) { 150341825Sdim _str->inc(_amount); 151226584Sdim } 152226584Sdim ~streamIndentor() { _str->dec(_amount); } 153249423Sdim}; 154226584Sdim 155226584Sdim 156226584Sdim// advisory locking for the shared tty stream: 157341825Sdimclass ttyLocker: StackObj { 158226584Sdim friend class ttyUnlocker; 159226584Sdim private: 160226584Sdim intx _holder; 161226584Sdim 162226584Sdim public: 163226584Sdim static intx hold_tty(); // returns a "holder" token 164226584Sdim static void release_tty(intx holder); // must witness same token 165341825Sdim static bool release_tty_if_locked(); // returns true if lock was released 166226584Sdim static void break_tty_lock_for_safepoint(intx holder); 167226584Sdim 168226584Sdim ttyLocker() { _holder = hold_tty(); } 169226584Sdim ~ttyLocker() { release_tty(_holder); } 170309124Sdim}; 171341825Sdim 172226584Sdim// Release the tty lock if it's held and reacquire it if it was 173226584Sdim// locked. Used to avoid lock ordering problems. 174226584Sdimclass ttyUnlocker: StackObj { 175226584Sdim private: 176226584Sdim bool _was_locked; 177226584Sdim public: 178226584Sdim ttyUnlocker() { 179226584Sdim _was_locked = ttyLocker::release_tty_if_locked(); 180226584Sdim } 181226584Sdim ~ttyUnlocker() { 182226584Sdim if (_was_locked) { 183341825Sdim ttyLocker::hold_tty(); 184341825Sdim } 185341825Sdim } 186280031Sdim}; 187280031Sdim 188226584Sdim// for writing to strings; buffer will expand automatically 189226584Sdimclass stringStream : public outputStream { 190226584Sdim protected: 191226584Sdim char* buffer; 192226584Sdim size_t buffer_pos; 193226584Sdim size_t buffer_length; 194226584Sdim bool buffer_fixed; 195309124Sdim DEBUG_ONLY(ResourceMark* rm;) 196226584Sdim public: 197226584Sdim stringStream(size_t initial_bufsize = 256); 198226584Sdim stringStream(char* fixed_buffer, size_t fixed_buffer_size); 199226584Sdim ~stringStream(); 200309124Sdim virtual void write(const char* c, size_t len); 201226584Sdim size_t size() { return buffer_pos; } 202226584Sdim const char* base() { return buffer; } 203226584Sdim void reset() { buffer_pos = 0; _precount = 0; _position = 0; } 204226584Sdim char* as_string(); 205226584Sdim}; 206226584Sdim 207226584Sdimclass fileStream : public outputStream { 208226584Sdim protected: 209226584Sdim FILE* _file; 210309124Sdim bool _need_close; 211226584Sdim public: 212309124Sdim fileStream() { _file = NULL; _need_close = false; } 213226584Sdim fileStream(const char* file_name); 214226584Sdim fileStream(const char* file_name, const char* opentype); 215309124Sdim fileStream(FILE* file, bool need_close = false) { _file = file; _need_close = need_close; } 216226584Sdim ~fileStream(); 217226584Sdim bool is_open() const { return _file != NULL; } 218226584Sdim void set_need_close(bool b) { _need_close = b;} 219226584Sdim virtual void write(const char* c, size_t len); 220226584Sdim size_t read(void *data, size_t size, size_t count) { return ::fread(data, size, count, _file); } 221226584Sdim char* readln(char *data, int count); 222226584Sdim int eof() { return feof(_file); } 223226584Sdim long fileSize(); 224226584Sdim void rewind() { ::rewind(_file); } 225226584Sdim void flush(); 226226584Sdim}; 227226584Sdim 228CDS_ONLY(extern fileStream* classlist_file;) 229 230// unlike fileStream, fdStream does unbuffered I/O by calling 231// open() and write() directly. It is async-safe, but output 232// from multiple thread may be mixed together. Used by fatal 233// error handler. 234class fdStream : public outputStream { 235 protected: 236 int _fd; 237 bool _need_close; 238 public: 239 fdStream(const char* file_name); 240 fdStream(int fd = -1) { _fd = fd; _need_close = false; } 241 ~fdStream(); 242 bool is_open() const { return _fd != -1; } 243 void set_fd(int fd) { _fd = fd; _need_close = false; } 244 int fd() const { return _fd; } 245 virtual void write(const char* c, size_t len); 246 void flush() {}; 247}; 248 249class logStream : public outputStream { 250private: 251 stringStream _current_line; 252 void (*_log_func)(const char* fmt, ...) ATTRIBUTE_PRINTF(1, 2); 253public: 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 261void ostream_init(); 262void ostream_init_log(); 263void ostream_exit(); 264void 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. 268class 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}; 285 286#define O_BUFLEN 2000 // max size of output of individual print() methods 287 288#ifndef PRODUCT 289 290class networkStream : public bufferedStream { 291 292 private: 293 int _socket; 294 295 public: 296 networkStream(); 297 ~networkStream(); 298 299 bool connect(const char *host, short port); 300 bool is_open() const { return _socket != -1; } 301 int read(char *buf, size_t len); 302 void close(); 303 virtual void flush(); 304}; 305 306#endif 307 308#endif // SHARE_VM_UTILITIES_OSTREAM_HPP 309