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