src/share/vm/utilities/ostream.hpp

Wed, 02 Apr 2008 12:09:59 -0700

author
jrose
date
Wed, 02 Apr 2008 12:09:59 -0700
changeset 535
c7c777385a15
parent 435
a61af66fc99e
child 631
d1605aabd0a1
child 657
2a1a77d3458f
child 777
37f87013dfd8
permissions
-rw-r--r--

6667042: PrintAssembly option does not work without special plugin
Summary: remove old private plugin interface, simplify, rework old plugin to use unchanged Gnu sources
Reviewed-by: kvn, rasbold

duke@435 1 /*
duke@435 2 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 // Output streams for printing
duke@435 26 //
duke@435 27 // Printing guidelines:
duke@435 28 // Where possible, please use tty->print() and tty->print_cr().
duke@435 29 // For product mode VM warnings use warning() which internally uses tty.
duke@435 30 // In places where tty is not initialized yet or too much overhead,
duke@435 31 // we may use jio_printf:
duke@435 32 // jio_fprintf(defaultStream::output_stream(), "Message");
duke@435 33 // This allows for redirection via -XX:+DisplayVMOutputToStdout and
duke@435 34 // -XX:+DisplayVMOutputToStderr
duke@435 35 class outputStream : public ResourceObj {
duke@435 36 protected:
duke@435 37 int _indentation; // current indentation
duke@435 38 int _width; // width of the page
duke@435 39 int _position; // position on the current line
duke@435 40 int _newlines; // number of '\n' output so far
duke@435 41 julong _precount; // number of chars output, less _position
duke@435 42 TimeStamp _stamp; // for time stamps
duke@435 43
duke@435 44 void update_position(const char* s, size_t len);
duke@435 45 static const char* do_vsnprintf(char* buffer, size_t buflen,
duke@435 46 const char* format, va_list ap,
duke@435 47 bool add_cr,
duke@435 48 size_t& result_len);
duke@435 49
duke@435 50 public:
duke@435 51 // creation
duke@435 52 outputStream(int width = 80);
duke@435 53 outputStream(int width, bool has_time_stamps);
duke@435 54
duke@435 55 // indentation
duke@435 56 void indent();
duke@435 57 void inc() { _indentation++; };
duke@435 58 void dec() { _indentation--; };
duke@435 59 int indentation() const { return _indentation; }
duke@435 60 void set_indentation(int i) { _indentation = i; }
duke@435 61 void fill_to(int col);
jrose@535 62 void move_to(int col, int slop = 6, int min_space = 2);
duke@435 63
duke@435 64 // sizing
duke@435 65 int width() const { return _width; }
duke@435 66 int position() const { return _position; }
duke@435 67 int newlines() const { return _newlines; }
duke@435 68 julong count() const { return _precount + _position; }
duke@435 69 void set_count(julong count) { _precount = count - _position; }
duke@435 70 void set_position(int pos) { _position = pos; }
duke@435 71
duke@435 72 // printing
duke@435 73 void print(const char* format, ...);
duke@435 74 void print_cr(const char* format, ...);
duke@435 75 void vprint(const char *format, va_list argptr);
duke@435 76 void vprint_cr(const char* format, va_list argptr);
duke@435 77 void print_raw(const char* str) { write(str, strlen(str)); }
duke@435 78 void print_raw(const char* str, int len) { write(str, len); }
duke@435 79 void print_raw_cr(const char* str) { write(str, strlen(str)); cr(); }
duke@435 80 void print_raw_cr(const char* str, int len){ write(str, len); cr(); }
duke@435 81 void put(char ch);
jrose@535 82 void sp(int count = 1);
duke@435 83 void cr();
duke@435 84 void bol() { if (_position > 0) cr(); }
duke@435 85
duke@435 86 // Time stamp
duke@435 87 TimeStamp& time_stamp() { return _stamp; }
duke@435 88 void stamp();
duke@435 89 // Date stamp
duke@435 90 void date_stamp(bool guard, const char* prefix, const char* suffix);
duke@435 91 // A simplified call that includes a suffix of ": "
duke@435 92 void date_stamp(bool guard) {
duke@435 93 date_stamp(guard, "", ": ");
duke@435 94 }
duke@435 95
duke@435 96 // portable printing of 64 bit integers
duke@435 97 void print_jlong(jlong value);
duke@435 98 void print_julong(julong value);
duke@435 99
duke@435 100 // flushing
duke@435 101 virtual void flush() {}
duke@435 102 virtual void write(const char* str, size_t len) = 0;
duke@435 103 virtual ~outputStream() {} // close properly on deletion
duke@435 104
duke@435 105 void dec_cr() { dec(); cr(); }
duke@435 106 void inc_cr() { inc(); cr(); }
duke@435 107 };
duke@435 108
duke@435 109 // standard output
duke@435 110 // ANSI C++ name collision
duke@435 111 extern outputStream* tty; // tty output
duke@435 112 extern outputStream* gclog_or_tty; // stream for gc log if -Xloggc:<f>, or tty
duke@435 113
duke@435 114 // advisory locking for the shared tty stream:
duke@435 115 class ttyLocker: StackObj {
duke@435 116 private:
duke@435 117 intx _holder;
duke@435 118
duke@435 119 public:
duke@435 120 static intx hold_tty(); // returns a "holder" token
duke@435 121 static void release_tty(intx holder); // must witness same token
duke@435 122 static void break_tty_lock_for_safepoint(intx holder);
duke@435 123
duke@435 124 ttyLocker() { _holder = hold_tty(); }
duke@435 125 ~ttyLocker() { release_tty(_holder); }
duke@435 126 };
duke@435 127
duke@435 128 // for writing to strings; buffer will expand automatically
duke@435 129 class stringStream : public outputStream {
duke@435 130 protected:
duke@435 131 char* buffer;
duke@435 132 size_t buffer_pos;
duke@435 133 size_t buffer_length;
duke@435 134 bool buffer_fixed;
duke@435 135 public:
duke@435 136 stringStream(size_t initial_bufsize = 256);
duke@435 137 stringStream(char* fixed_buffer, size_t fixed_buffer_size);
duke@435 138 ~stringStream();
duke@435 139 virtual void write(const char* c, size_t len);
duke@435 140 size_t size() { return buffer_pos; }
duke@435 141 const char* base() { return buffer; }
duke@435 142 void reset() { buffer_pos = 0; _precount = 0; _position = 0; }
duke@435 143 char* as_string();
duke@435 144 };
duke@435 145
duke@435 146 class fileStream : public outputStream {
duke@435 147 protected:
duke@435 148 FILE* _file;
duke@435 149 bool _need_close;
duke@435 150 public:
duke@435 151 fileStream(const char* file_name);
duke@435 152 fileStream(FILE* file) { _file = file; _need_close = false; }
duke@435 153 ~fileStream();
duke@435 154 bool is_open() const { return _file != NULL; }
duke@435 155 virtual void write(const char* c, size_t len);
duke@435 156 void flush();
duke@435 157 };
duke@435 158
duke@435 159 // unlike fileStream, fdStream does unbuffered I/O by calling
duke@435 160 // open() and write() directly. It is async-safe, but output
duke@435 161 // from multiple thread may be mixed together. Used by fatal
duke@435 162 // error handler.
duke@435 163 class fdStream : public outputStream {
duke@435 164 protected:
duke@435 165 int _fd;
duke@435 166 bool _need_close;
duke@435 167 public:
duke@435 168 fdStream(const char* file_name);
duke@435 169 fdStream(int fd = -1) { _fd = fd; _need_close = false; }
duke@435 170 ~fdStream();
duke@435 171 bool is_open() const { return _fd != -1; }
duke@435 172 void set_fd(int fd) { _fd = fd; _need_close = false; }
duke@435 173 int fd() const { return _fd; }
duke@435 174 virtual void write(const char* c, size_t len);
duke@435 175 void flush() {};
duke@435 176 };
duke@435 177
duke@435 178 void ostream_init();
duke@435 179 void ostream_init_log();
duke@435 180 void ostream_exit();
duke@435 181 void ostream_abort();
duke@435 182
duke@435 183 // staticBufferStream uses a user-supplied buffer for all formatting.
duke@435 184 // Used for safe formatting during fatal error handling. Not MT safe.
duke@435 185 // Do not share the stream between multiple threads.
duke@435 186 class staticBufferStream : public outputStream {
duke@435 187 private:
duke@435 188 char* _buffer;
duke@435 189 size_t _buflen;
duke@435 190 outputStream* _outer_stream;
duke@435 191 public:
duke@435 192 staticBufferStream(char* buffer, size_t buflen,
duke@435 193 outputStream *outer_stream);
duke@435 194 ~staticBufferStream() {};
duke@435 195 virtual void write(const char* c, size_t len);
duke@435 196 void flush();
duke@435 197 void print(const char* format, ...);
duke@435 198 void print_cr(const char* format, ...);
duke@435 199 void vprint(const char *format, va_list argptr);
duke@435 200 void vprint_cr(const char* format, va_list argptr);
duke@435 201 };
duke@435 202
duke@435 203 // In the non-fixed buffer case an underlying buffer will be created and
duke@435 204 // managed in C heap. Not MT-safe.
duke@435 205 class bufferedStream : public outputStream {
duke@435 206 protected:
duke@435 207 char* buffer;
duke@435 208 size_t buffer_pos;
duke@435 209 size_t buffer_length;
duke@435 210 bool buffer_fixed;
duke@435 211 public:
duke@435 212 bufferedStream(size_t initial_bufsize = 256);
duke@435 213 bufferedStream(char* fixed_buffer, size_t fixed_buffer_size);
duke@435 214 ~bufferedStream();
duke@435 215 virtual void write(const char* c, size_t len);
duke@435 216 size_t size() { return buffer_pos; }
duke@435 217 const char* base() { return buffer; }
duke@435 218 void reset() { buffer_pos = 0; _precount = 0; _position = 0; }
duke@435 219 char* as_string();
duke@435 220 };
duke@435 221
duke@435 222 #define O_BUFLEN 2000 // max size of output of individual print() methods
duke@435 223
duke@435 224 #ifndef PRODUCT
duke@435 225
duke@435 226 class networkStream : public bufferedStream {
duke@435 227
duke@435 228 private:
duke@435 229 int _socket;
duke@435 230
duke@435 231 public:
duke@435 232 networkStream();
duke@435 233 ~networkStream();
duke@435 234
duke@435 235 bool connect(const char *host, short port);
duke@435 236 bool is_open() const { return _socket != -1; }
duke@435 237 int read(char *buf, size_t len);
duke@435 238 void close();
duke@435 239 virtual void flush();
duke@435 240 };
duke@435 241
duke@435 242 #endif

mercurial