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