src/share/vm/utilities/ostream.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_UTILITIES_OSTREAM_HPP
aoqi@0 26 #define SHARE_VM_UTILITIES_OSTREAM_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "runtime/timer.hpp"
aoqi@0 30
aoqi@0 31 DEBUG_ONLY(class ResourceMark;)
aoqi@0 32
aoqi@0 33 // Output streams for printing
aoqi@0 34 //
aoqi@0 35 // Printing guidelines:
aoqi@0 36 // Where possible, please use tty->print() and tty->print_cr().
aoqi@0 37 // For product mode VM warnings use warning() which internally uses tty.
aoqi@0 38 // In places where tty is not initialized yet or too much overhead,
aoqi@0 39 // we may use jio_printf:
aoqi@0 40 // jio_fprintf(defaultStream::output_stream(), "Message");
aoqi@0 41 // This allows for redirection via -XX:+DisplayVMOutputToStdout and
aoqi@0 42 // -XX:+DisplayVMOutputToStderr
aoqi@0 43 class outputStream : public ResourceObj {
aoqi@0 44 protected:
aoqi@0 45 int _indentation; // current indentation
aoqi@0 46 int _width; // width of the page
aoqi@0 47 int _position; // position on the current line
aoqi@0 48 int _newlines; // number of '\n' output so far
aoqi@0 49 julong _precount; // number of chars output, less _position
aoqi@0 50 TimeStamp _stamp; // for time stamps
aoqi@0 51
aoqi@0 52 void update_position(const char* s, size_t len);
aoqi@0 53 static const char* do_vsnprintf(char* buffer, size_t buflen,
aoqi@0 54 const char* format, va_list ap,
aoqi@0 55 bool add_cr,
aoqi@0 56 size_t& result_len) ATTRIBUTE_PRINTF(3, 0);
aoqi@0 57
aoqi@0 58 public:
aoqi@0 59 // creation
aoqi@0 60 outputStream(int width = 80);
aoqi@0 61 outputStream(int width, bool has_time_stamps);
aoqi@0 62
aoqi@0 63 // indentation
aoqi@0 64 outputStream& indent();
aoqi@0 65 void inc() { _indentation++; };
aoqi@0 66 void dec() { _indentation--; };
aoqi@0 67 void inc(int n) { _indentation += n; };
aoqi@0 68 void dec(int n) { _indentation -= n; };
aoqi@0 69 int indentation() const { return _indentation; }
aoqi@0 70 void set_indentation(int i) { _indentation = i; }
aoqi@0 71 void fill_to(int col);
aoqi@0 72 void move_to(int col, int slop = 6, int min_space = 2);
aoqi@0 73
aoqi@0 74 // sizing
aoqi@0 75 int width() const { return _width; }
aoqi@0 76 int position() const { return _position; }
aoqi@0 77 int newlines() const { return _newlines; }
aoqi@0 78 julong count() const { return _precount + _position; }
aoqi@0 79 void set_count(julong count) { _precount = count - _position; }
aoqi@0 80 void set_position(int pos) { _position = pos; }
aoqi@0 81
aoqi@0 82 // printing
aoqi@0 83 void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
aoqi@0 84 void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
aoqi@0 85 void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
aoqi@0 86 void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
aoqi@0 87 void print_raw(const char* str) { write(str, strlen(str)); }
aoqi@0 88 void print_raw(const char* str, int len) { write(str, len); }
aoqi@0 89 void print_raw_cr(const char* str) { write(str, strlen(str)); cr(); }
aoqi@0 90 void print_raw_cr(const char* str, int len){ write(str, len); cr(); }
aoqi@0 91 void print_data(void* data, size_t len, bool with_ascii);
aoqi@0 92 void put(char ch);
aoqi@0 93 void sp(int count = 1);
aoqi@0 94 void cr();
aoqi@0 95 void bol() { if (_position > 0) cr(); }
aoqi@0 96
aoqi@0 97 // Time stamp
aoqi@0 98 TimeStamp& time_stamp() { return _stamp; }
aoqi@0 99 void stamp();
aoqi@0 100 void stamp(bool guard, const char* prefix, const char* suffix);
aoqi@0 101 void stamp(bool guard) {
aoqi@0 102 stamp(guard, "", ": ");
aoqi@0 103 }
aoqi@0 104 // Date stamp
aoqi@0 105 void date_stamp(bool guard, const char* prefix, const char* suffix);
aoqi@0 106 // A simplified call that includes a suffix of ": "
aoqi@0 107 void date_stamp(bool guard) {
aoqi@0 108 date_stamp(guard, "", ": ");
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 // portable printing of 64 bit integers
aoqi@0 112 void print_jlong(jlong value);
aoqi@0 113 void print_julong(julong value);
aoqi@0 114
aoqi@0 115 // flushing
aoqi@0 116 virtual void flush() {}
aoqi@0 117 virtual void write(const char* str, size_t len) = 0;
aoqi@0 118 virtual void rotate_log(bool force, outputStream* out = NULL) {} // GC log rotation
aoqi@0 119 virtual ~outputStream() {} // close properly on deletion
aoqi@0 120
aoqi@0 121 void dec_cr() { dec(); cr(); }
aoqi@0 122 void inc_cr() { inc(); cr(); }
aoqi@0 123 };
aoqi@0 124
aoqi@0 125 // standard output
aoqi@0 126 // ANSI C++ name collision
aoqi@0 127 extern outputStream* tty; // tty output
aoqi@0 128 extern outputStream* gclog_or_tty; // stream for gc log if -Xloggc:<f>, or tty
aoqi@0 129
aoqi@0 130 class streamIndentor : public StackObj {
aoqi@0 131 private:
aoqi@0 132 outputStream* _str;
aoqi@0 133 int _amount;
aoqi@0 134
aoqi@0 135 public:
aoqi@0 136 streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) {
aoqi@0 137 _str->inc(_amount);
aoqi@0 138 }
aoqi@0 139 ~streamIndentor() { _str->dec(_amount); }
aoqi@0 140 };
aoqi@0 141
aoqi@0 142
aoqi@0 143 // advisory locking for the shared tty stream:
aoqi@0 144 class ttyLocker: StackObj {
aoqi@0 145 friend class ttyUnlocker;
aoqi@0 146 private:
aoqi@0 147 intx _holder;
aoqi@0 148
aoqi@0 149 public:
aoqi@0 150 static intx hold_tty(); // returns a "holder" token
aoqi@0 151 static void release_tty(intx holder); // must witness same token
aoqi@0 152 static bool release_tty_if_locked(); // returns true if lock was released
aoqi@0 153 static void break_tty_lock_for_safepoint(intx holder);
aoqi@0 154
aoqi@0 155 ttyLocker() { _holder = hold_tty(); }
aoqi@0 156 ~ttyLocker() { release_tty(_holder); }
aoqi@0 157 };
aoqi@0 158
aoqi@0 159 // Release the tty lock if it's held and reacquire it if it was
aoqi@0 160 // locked. Used to avoid lock ordering problems.
aoqi@0 161 class ttyUnlocker: StackObj {
aoqi@0 162 private:
aoqi@0 163 bool _was_locked;
aoqi@0 164 public:
aoqi@0 165 ttyUnlocker() {
aoqi@0 166 _was_locked = ttyLocker::release_tty_if_locked();
aoqi@0 167 }
aoqi@0 168 ~ttyUnlocker() {
aoqi@0 169 if (_was_locked) {
aoqi@0 170 ttyLocker::hold_tty();
aoqi@0 171 }
aoqi@0 172 }
aoqi@0 173 };
aoqi@0 174
aoqi@0 175 // for writing to strings; buffer will expand automatically
aoqi@0 176 class stringStream : public outputStream {
aoqi@0 177 protected:
aoqi@0 178 char* buffer;
aoqi@0 179 size_t buffer_pos;
aoqi@0 180 size_t buffer_length;
aoqi@0 181 bool buffer_fixed;
aoqi@0 182 DEBUG_ONLY(ResourceMark* rm;)
aoqi@0 183 public:
aoqi@0 184 stringStream(size_t initial_bufsize = 256);
aoqi@0 185 stringStream(char* fixed_buffer, size_t fixed_buffer_size);
aoqi@0 186 ~stringStream();
aoqi@0 187 virtual void write(const char* c, size_t len);
aoqi@0 188 size_t size() { return buffer_pos; }
aoqi@0 189 const char* base() { return buffer; }
aoqi@0 190 void reset() { buffer_pos = 0; _precount = 0; _position = 0; }
aoqi@0 191 char* as_string();
aoqi@0 192 };
aoqi@0 193
aoqi@0 194 class fileStream : public outputStream {
aoqi@0 195 protected:
aoqi@0 196 FILE* _file;
aoqi@0 197 bool _need_close;
aoqi@0 198 public:
aoqi@0 199 fileStream() { _file = NULL; _need_close = false; }
aoqi@0 200 fileStream(const char* file_name);
aoqi@0 201 fileStream(const char* file_name, const char* opentype);
aoqi@0 202 fileStream(FILE* file, bool need_close = false) { _file = file; _need_close = need_close; }
aoqi@0 203 ~fileStream();
aoqi@0 204 bool is_open() const { return _file != NULL; }
aoqi@0 205 void set_need_close(bool b) { _need_close = b;}
aoqi@0 206 virtual void write(const char* c, size_t len);
aoqi@0 207 size_t read(void *data, size_t size, size_t count) { return ::fread(data, size, count, _file); }
aoqi@0 208 char* readln(char *data, int count);
aoqi@0 209 int eof() { return feof(_file); }
aoqi@0 210 long fileSize();
aoqi@0 211 void rewind() { ::rewind(_file); }
aoqi@0 212 void flush();
aoqi@0 213 };
aoqi@0 214
aoqi@0 215 // unlike fileStream, fdStream does unbuffered I/O by calling
aoqi@0 216 // open() and write() directly. It is async-safe, but output
aoqi@0 217 // from multiple thread may be mixed together. Used by fatal
aoqi@0 218 // error handler.
aoqi@0 219 class fdStream : public outputStream {
aoqi@0 220 protected:
aoqi@0 221 int _fd;
aoqi@0 222 bool _need_close;
aoqi@0 223 public:
aoqi@0 224 fdStream(const char* file_name);
aoqi@0 225 fdStream(int fd = -1) { _fd = fd; _need_close = false; }
aoqi@0 226 ~fdStream();
aoqi@0 227 bool is_open() const { return _fd != -1; }
aoqi@0 228 void set_fd(int fd) { _fd = fd; _need_close = false; }
aoqi@0 229 int fd() const { return _fd; }
aoqi@0 230 virtual void write(const char* c, size_t len);
aoqi@0 231 void flush() {};
aoqi@0 232 };
aoqi@0 233
aoqi@0 234 class gcLogFileStream : public fileStream {
aoqi@0 235 protected:
aoqi@0 236 const char* _file_name;
aoqi@0 237 jlong _bytes_written;
aoqi@0 238 uintx _cur_file_num; // current logfile rotation number, from 0 to NumberOfGCLogFiles-1
aoqi@0 239 public:
aoqi@0 240 gcLogFileStream(const char* file_name);
aoqi@0 241 ~gcLogFileStream();
aoqi@0 242 virtual void write(const char* c, size_t len);
aoqi@0 243 virtual void rotate_log(bool force, outputStream* out = NULL);
aoqi@0 244 void dump_loggc_header();
aoqi@0 245
aoqi@0 246 /* If "force" sets true, force log file rotation from outside JVM */
aoqi@0 247 bool should_rotate(bool force) {
aoqi@0 248 return force ||
aoqi@0 249 ((GCLogFileSize != 0) && ((uintx)_bytes_written >= GCLogFileSize));
aoqi@0 250 }
aoqi@0 251
aoqi@0 252 };
aoqi@0 253
aoqi@0 254 #ifndef PRODUCT
aoqi@0 255 // unit test for checking -Xloggc:<filename> parsing result
aoqi@0 256 void test_loggc_filename();
aoqi@0 257 #endif
aoqi@0 258
aoqi@0 259 void ostream_init();
aoqi@0 260 void ostream_init_log();
aoqi@0 261 void ostream_exit();
aoqi@0 262 void ostream_abort();
aoqi@0 263
aoqi@0 264 // staticBufferStream uses a user-supplied buffer for all formatting.
aoqi@0 265 // Used for safe formatting during fatal error handling. Not MT safe.
aoqi@0 266 // Do not share the stream between multiple threads.
aoqi@0 267 class staticBufferStream : public outputStream {
aoqi@0 268 private:
aoqi@0 269 char* _buffer;
aoqi@0 270 size_t _buflen;
aoqi@0 271 outputStream* _outer_stream;
aoqi@0 272 public:
aoqi@0 273 staticBufferStream(char* buffer, size_t buflen,
aoqi@0 274 outputStream *outer_stream);
aoqi@0 275 ~staticBufferStream() {};
aoqi@0 276 virtual void write(const char* c, size_t len);
aoqi@0 277 void flush();
aoqi@0 278 void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
aoqi@0 279 void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
aoqi@0 280 void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
aoqi@0 281 void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
aoqi@0 282 };
aoqi@0 283
aoqi@0 284 // In the non-fixed buffer case an underlying buffer will be created and
aoqi@0 285 // managed in C heap. Not MT-safe.
aoqi@0 286 class bufferedStream : public outputStream {
aoqi@0 287 protected:
aoqi@0 288 char* buffer;
aoqi@0 289 size_t buffer_pos;
aoqi@0 290 size_t buffer_max;
aoqi@0 291 size_t buffer_length;
aoqi@0 292 bool buffer_fixed;
aoqi@0 293 public:
aoqi@0 294 bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10);
aoqi@0 295 bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10);
aoqi@0 296 ~bufferedStream();
aoqi@0 297 virtual void write(const char* c, size_t len);
aoqi@0 298 size_t size() { return buffer_pos; }
aoqi@0 299 const char* base() { return buffer; }
aoqi@0 300 void reset() { buffer_pos = 0; _precount = 0; _position = 0; }
aoqi@0 301 char* as_string();
aoqi@0 302 };
aoqi@0 303
aoqi@0 304 #define O_BUFLEN 2000 // max size of output of individual print() methods
aoqi@0 305
aoqi@0 306 #ifndef PRODUCT
aoqi@0 307
aoqi@0 308 class networkStream : public bufferedStream {
aoqi@0 309
aoqi@0 310 private:
aoqi@0 311 int _socket;
aoqi@0 312
aoqi@0 313 public:
aoqi@0 314 networkStream();
aoqi@0 315 ~networkStream();
aoqi@0 316
aoqi@0 317 bool connect(const char *host, short port);
aoqi@0 318 bool is_open() const { return _socket != -1; }
aoqi@0 319 int read(char *buf, size_t len);
aoqi@0 320 void close();
aoqi@0 321 virtual void flush();
aoqi@0 322 };
aoqi@0 323
aoqi@0 324 #endif
aoqi@0 325
aoqi@0 326 #endif // SHARE_VM_UTILITIES_OSTREAM_HPP

mercurial