Mercurial > hg > truffle
comparison src/share/vm/utilities/ostream.hpp @ 0:a61af66fc99e jdk7-b24
Initial load
author | duke |
---|---|
date | Sat, 01 Dec 2007 00:00:00 +0000 |
parents | |
children | c7c777385a15 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a61af66fc99e |
---|---|
1 /* | |
2 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. | |
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); | |
62 | |
63 // sizing | |
64 int width() const { return _width; } | |
65 int position() const { return _position; } | |
66 int newlines() const { return _newlines; } | |
67 julong count() const { return _precount + _position; } | |
68 void set_count(julong count) { _precount = count - _position; } | |
69 void set_position(int pos) { _position = pos; } | |
70 | |
71 // printing | |
72 void print(const char* format, ...); | |
73 void print_cr(const char* format, ...); | |
74 void vprint(const char *format, va_list argptr); | |
75 void vprint_cr(const char* format, va_list argptr); | |
76 void print_raw(const char* str) { write(str, strlen(str)); } | |
77 void print_raw(const char* str, int len) { write(str, len); } | |
78 void print_raw_cr(const char* str) { write(str, strlen(str)); cr(); } | |
79 void print_raw_cr(const char* str, int len){ write(str, len); cr(); } | |
80 void put(char ch); | |
81 void sp(); | |
82 void cr(); | |
83 void bol() { if (_position > 0) cr(); } | |
84 | |
85 // Time stamp | |
86 TimeStamp& time_stamp() { return _stamp; } | |
87 void stamp(); | |
88 // Date stamp | |
89 void date_stamp(bool guard, const char* prefix, const char* suffix); | |
90 // A simplified call that includes a suffix of ": " | |
91 void date_stamp(bool guard) { | |
92 date_stamp(guard, "", ": "); | |
93 } | |
94 | |
95 // portable printing of 64 bit integers | |
96 void print_jlong(jlong value); | |
97 void print_julong(julong value); | |
98 | |
99 // flushing | |
100 virtual void flush() {} | |
101 virtual void write(const char* str, size_t len) = 0; | |
102 virtual ~outputStream() {} // close properly on deletion | |
103 | |
104 void dec_cr() { dec(); cr(); } | |
105 void inc_cr() { inc(); cr(); } | |
106 }; | |
107 | |
108 // standard output | |
109 // ANSI C++ name collision | |
110 extern outputStream* tty; // tty output | |
111 extern outputStream* gclog_or_tty; // stream for gc log if -Xloggc:<f>, or tty | |
112 | |
113 // advisory locking for the shared tty stream: | |
114 class ttyLocker: StackObj { | |
115 private: | |
116 intx _holder; | |
117 | |
118 public: | |
119 static intx hold_tty(); // returns a "holder" token | |
120 static void release_tty(intx holder); // must witness same token | |
121 static void break_tty_lock_for_safepoint(intx holder); | |
122 | |
123 ttyLocker() { _holder = hold_tty(); } | |
124 ~ttyLocker() { release_tty(_holder); } | |
125 }; | |
126 | |
127 // for writing to strings; buffer will expand automatically | |
128 class stringStream : public outputStream { | |
129 protected: | |
130 char* buffer; | |
131 size_t buffer_pos; | |
132 size_t buffer_length; | |
133 bool buffer_fixed; | |
134 public: | |
135 stringStream(size_t initial_bufsize = 256); | |
136 stringStream(char* fixed_buffer, size_t fixed_buffer_size); | |
137 ~stringStream(); | |
138 virtual void write(const char* c, size_t len); | |
139 size_t size() { return buffer_pos; } | |
140 const char* base() { return buffer; } | |
141 void reset() { buffer_pos = 0; _precount = 0; _position = 0; } | |
142 char* as_string(); | |
143 }; | |
144 | |
145 class fileStream : public outputStream { | |
146 protected: | |
147 FILE* _file; | |
148 bool _need_close; | |
149 public: | |
150 fileStream(const char* file_name); | |
151 fileStream(FILE* file) { _file = file; _need_close = false; } | |
152 ~fileStream(); | |
153 bool is_open() const { return _file != NULL; } | |
154 virtual void write(const char* c, size_t len); | |
155 void flush(); | |
156 }; | |
157 | |
158 // unlike fileStream, fdStream does unbuffered I/O by calling | |
159 // open() and write() directly. It is async-safe, but output | |
160 // from multiple thread may be mixed together. Used by fatal | |
161 // error handler. | |
162 class fdStream : public outputStream { | |
163 protected: | |
164 int _fd; | |
165 bool _need_close; | |
166 public: | |
167 fdStream(const char* file_name); | |
168 fdStream(int fd = -1) { _fd = fd; _need_close = false; } | |
169 ~fdStream(); | |
170 bool is_open() const { return _fd != -1; } | |
171 void set_fd(int fd) { _fd = fd; _need_close = false; } | |
172 int fd() const { return _fd; } | |
173 virtual void write(const char* c, size_t len); | |
174 void flush() {}; | |
175 }; | |
176 | |
177 void ostream_init(); | |
178 void ostream_init_log(); | |
179 void ostream_exit(); | |
180 void ostream_abort(); | |
181 | |
182 // staticBufferStream uses a user-supplied buffer for all formatting. | |
183 // Used for safe formatting during fatal error handling. Not MT safe. | |
184 // Do not share the stream between multiple threads. | |
185 class staticBufferStream : public outputStream { | |
186 private: | |
187 char* _buffer; | |
188 size_t _buflen; | |
189 outputStream* _outer_stream; | |
190 public: | |
191 staticBufferStream(char* buffer, size_t buflen, | |
192 outputStream *outer_stream); | |
193 ~staticBufferStream() {}; | |
194 virtual void write(const char* c, size_t len); | |
195 void flush(); | |
196 void print(const char* format, ...); | |
197 void print_cr(const char* format, ...); | |
198 void vprint(const char *format, va_list argptr); | |
199 void vprint_cr(const char* format, va_list argptr); | |
200 }; | |
201 | |
202 // In the non-fixed buffer case an underlying buffer will be created and | |
203 // managed in C heap. Not MT-safe. | |
204 class bufferedStream : public outputStream { | |
205 protected: | |
206 char* buffer; | |
207 size_t buffer_pos; | |
208 size_t buffer_length; | |
209 bool buffer_fixed; | |
210 public: | |
211 bufferedStream(size_t initial_bufsize = 256); | |
212 bufferedStream(char* fixed_buffer, size_t fixed_buffer_size); | |
213 ~bufferedStream(); | |
214 virtual void write(const char* c, size_t len); | |
215 size_t size() { return buffer_pos; } | |
216 const char* base() { return buffer; } | |
217 void reset() { buffer_pos = 0; _precount = 0; _position = 0; } | |
218 char* as_string(); | |
219 }; | |
220 | |
221 #define O_BUFLEN 2000 // max size of output of individual print() methods | |
222 | |
223 #ifndef PRODUCT | |
224 | |
225 class networkStream : public bufferedStream { | |
226 | |
227 private: | |
228 int _socket; | |
229 | |
230 public: | |
231 networkStream(); | |
232 ~networkStream(); | |
233 | |
234 bool connect(const char *host, short port); | |
235 bool is_open() const { return _socket != -1; } | |
236 int read(char *buf, size_t len); | |
237 void close(); | |
238 virtual void flush(); | |
239 }; | |
240 | |
241 #endif |