duke@435: /* xdono@631: * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // Output streams for printing duke@435: // duke@435: // Printing guidelines: duke@435: // Where possible, please use tty->print() and tty->print_cr(). duke@435: // For product mode VM warnings use warning() which internally uses tty. duke@435: // In places where tty is not initialized yet or too much overhead, duke@435: // we may use jio_printf: duke@435: // jio_fprintf(defaultStream::output_stream(), "Message"); duke@435: // This allows for redirection via -XX:+DisplayVMOutputToStdout and duke@435: // -XX:+DisplayVMOutputToStderr duke@435: class outputStream : public ResourceObj { duke@435: protected: duke@435: int _indentation; // current indentation duke@435: int _width; // width of the page duke@435: int _position; // position on the current line duke@435: int _newlines; // number of '\n' output so far duke@435: julong _precount; // number of chars output, less _position duke@435: TimeStamp _stamp; // for time stamps duke@435: duke@435: void update_position(const char* s, size_t len); duke@435: static const char* do_vsnprintf(char* buffer, size_t buflen, duke@435: const char* format, va_list ap, duke@435: bool add_cr, duke@435: size_t& result_len); duke@435: duke@435: public: duke@435: // creation duke@435: outputStream(int width = 80); duke@435: outputStream(int width, bool has_time_stamps); duke@435: duke@435: // indentation duke@435: void indent(); duke@435: void inc() { _indentation++; }; duke@435: void dec() { _indentation--; }; duke@435: int indentation() const { return _indentation; } duke@435: void set_indentation(int i) { _indentation = i; } duke@435: void fill_to(int col); jrose@535: void move_to(int col, int slop = 6, int min_space = 2); duke@435: duke@435: // sizing duke@435: int width() const { return _width; } duke@435: int position() const { return _position; } duke@435: int newlines() const { return _newlines; } duke@435: julong count() const { return _precount + _position; } duke@435: void set_count(julong count) { _precount = count - _position; } duke@435: void set_position(int pos) { _position = pos; } duke@435: duke@435: // printing duke@435: void print(const char* format, ...); duke@435: void print_cr(const char* format, ...); duke@435: void vprint(const char *format, va_list argptr); duke@435: void vprint_cr(const char* format, va_list argptr); duke@435: void print_raw(const char* str) { write(str, strlen(str)); } duke@435: void print_raw(const char* str, int len) { write(str, len); } duke@435: void print_raw_cr(const char* str) { write(str, strlen(str)); cr(); } duke@435: void print_raw_cr(const char* str, int len){ write(str, len); cr(); } duke@435: void put(char ch); jrose@535: void sp(int count = 1); duke@435: void cr(); duke@435: void bol() { if (_position > 0) cr(); } duke@435: duke@435: // Time stamp duke@435: TimeStamp& time_stamp() { return _stamp; } duke@435: void stamp(); ysr@777: void stamp(bool guard, const char* prefix, const char* suffix); ysr@777: void stamp(bool guard) { ysr@777: stamp(guard, "", ": "); ysr@777: } duke@435: // Date stamp duke@435: void date_stamp(bool guard, const char* prefix, const char* suffix); duke@435: // A simplified call that includes a suffix of ": " duke@435: void date_stamp(bool guard) { duke@435: date_stamp(guard, "", ": "); duke@435: } duke@435: duke@435: // portable printing of 64 bit integers duke@435: void print_jlong(jlong value); duke@435: void print_julong(julong value); duke@435: duke@435: // flushing duke@435: virtual void flush() {} duke@435: virtual void write(const char* str, size_t len) = 0; duke@435: virtual ~outputStream() {} // close properly on deletion duke@435: duke@435: void dec_cr() { dec(); cr(); } duke@435: void inc_cr() { inc(); cr(); } duke@435: }; duke@435: duke@435: // standard output duke@435: // ANSI C++ name collision duke@435: extern outputStream* tty; // tty output duke@435: extern outputStream* gclog_or_tty; // stream for gc log if -Xloggc:, or tty duke@435: duke@435: // advisory locking for the shared tty stream: duke@435: class ttyLocker: StackObj { duke@435: private: duke@435: intx _holder; duke@435: duke@435: public: duke@435: static intx hold_tty(); // returns a "holder" token duke@435: static void release_tty(intx holder); // must witness same token duke@435: static void break_tty_lock_for_safepoint(intx holder); duke@435: duke@435: ttyLocker() { _holder = hold_tty(); } duke@435: ~ttyLocker() { release_tty(_holder); } duke@435: }; duke@435: duke@435: // for writing to strings; buffer will expand automatically duke@435: class stringStream : public outputStream { duke@435: protected: duke@435: char* buffer; duke@435: size_t buffer_pos; duke@435: size_t buffer_length; duke@435: bool buffer_fixed; duke@435: public: duke@435: stringStream(size_t initial_bufsize = 256); duke@435: stringStream(char* fixed_buffer, size_t fixed_buffer_size); duke@435: ~stringStream(); duke@435: virtual void write(const char* c, size_t len); duke@435: size_t size() { return buffer_pos; } duke@435: const char* base() { return buffer; } duke@435: void reset() { buffer_pos = 0; _precount = 0; _position = 0; } duke@435: char* as_string(); duke@435: }; duke@435: duke@435: class fileStream : public outputStream { duke@435: protected: duke@435: FILE* _file; duke@435: bool _need_close; duke@435: public: duke@435: fileStream(const char* file_name); duke@435: fileStream(FILE* file) { _file = file; _need_close = false; } duke@435: ~fileStream(); duke@435: bool is_open() const { return _file != NULL; } duke@435: virtual void write(const char* c, size_t len); duke@435: void flush(); duke@435: }; duke@435: duke@435: // unlike fileStream, fdStream does unbuffered I/O by calling duke@435: // open() and write() directly. It is async-safe, but output duke@435: // from multiple thread may be mixed together. Used by fatal duke@435: // error handler. duke@435: class fdStream : public outputStream { duke@435: protected: duke@435: int _fd; duke@435: bool _need_close; duke@435: public: duke@435: fdStream(const char* file_name); duke@435: fdStream(int fd = -1) { _fd = fd; _need_close = false; } duke@435: ~fdStream(); duke@435: bool is_open() const { return _fd != -1; } duke@435: void set_fd(int fd) { _fd = fd; _need_close = false; } duke@435: int fd() const { return _fd; } duke@435: virtual void write(const char* c, size_t len); duke@435: void flush() {}; duke@435: }; duke@435: duke@435: void ostream_init(); duke@435: void ostream_init_log(); duke@435: void ostream_exit(); duke@435: void ostream_abort(); duke@435: duke@435: // staticBufferStream uses a user-supplied buffer for all formatting. duke@435: // Used for safe formatting during fatal error handling. Not MT safe. duke@435: // Do not share the stream between multiple threads. duke@435: class staticBufferStream : public outputStream { duke@435: private: duke@435: char* _buffer; duke@435: size_t _buflen; duke@435: outputStream* _outer_stream; duke@435: public: duke@435: staticBufferStream(char* buffer, size_t buflen, duke@435: outputStream *outer_stream); duke@435: ~staticBufferStream() {}; duke@435: virtual void write(const char* c, size_t len); duke@435: void flush(); duke@435: void print(const char* format, ...); duke@435: void print_cr(const char* format, ...); duke@435: void vprint(const char *format, va_list argptr); duke@435: void vprint_cr(const char* format, va_list argptr); duke@435: }; duke@435: duke@435: // In the non-fixed buffer case an underlying buffer will be created and duke@435: // managed in C heap. Not MT-safe. duke@435: class bufferedStream : public outputStream { duke@435: protected: duke@435: char* buffer; duke@435: size_t buffer_pos; never@657: size_t buffer_max; duke@435: size_t buffer_length; duke@435: bool buffer_fixed; duke@435: public: never@657: bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10); never@657: bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10); duke@435: ~bufferedStream(); duke@435: virtual void write(const char* c, size_t len); duke@435: size_t size() { return buffer_pos; } duke@435: const char* base() { return buffer; } duke@435: void reset() { buffer_pos = 0; _precount = 0; _position = 0; } duke@435: char* as_string(); duke@435: }; duke@435: duke@435: #define O_BUFLEN 2000 // max size of output of individual print() methods duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: class networkStream : public bufferedStream { duke@435: duke@435: private: duke@435: int _socket; duke@435: duke@435: public: duke@435: networkStream(); duke@435: ~networkStream(); duke@435: duke@435: bool connect(const char *host, short port); duke@435: bool is_open() const { return _socket != -1; } duke@435: int read(char *buf, size_t len); duke@435: void close(); duke@435: virtual void flush(); duke@435: }; duke@435: duke@435: #endif