/* * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
// Output streams for printing // // Printing guidelines: // Where possible, please use tty->print() and tty->print_cr(). // For product mode VM warnings use warning() which internally uses tty. // In places where tty is not initialized yet or too much overhead, // we may use jio_printf: // jio_fprintf(defaultStream::output_stream(), "Message"); // This allows for redirection via -XX:+DisplayVMOutputToStdout and // -XX:+DisplayVMOutputToStderr class outputStream : public CHeapObjBase { private:
NONCOPYABLE(outputStream);
protected: int _indentation; // current indentation int _position; // visual position on the current line
uint64_t _precount; // number of chars output, less than _position
TimeStamp _stamp; // for time stamps char* _scratch; // internal scratch buffer for printf
size_t _scratch_len; // size of internal scratch buffer
// Returns whether a newline was seen or not bool update_position(constchar* s, size_t len); staticconstchar* do_vsnprintf(char* buffer, size_t buflen, constchar* format, va_list ap, bool add_cr,
size_t& result_len) ATTRIBUTE_PRINTF(3, 0);
// calls do_vsnprintf and writes output to stream; uses an on-stack buffer. void do_vsnprintf_and_write_with_automatic_buffer(constchar* format, va_list ap, booladd_cr) ATTRIBUTE_PRINTF(2, 0); // calls do_vsnprintf and writes output to stream; uses the user-provided buffer; void do_vsnprintf_and_write_with_scratch_buffer(constchar* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0); // calls do_vsnprintf, then writes output to stream. void do_vsnprintf_and_write(constchar* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0);
// Caller may specify their own scratch buffer to use for printing; otherwise, // an automatic buffer on the stack (with O_BUFLEN len) is used. void set_scratch_buffer(char* p, size_t len) { _scratch = p; _scratch_len = len; }
// standard output // ANSI C++ name collision extern outputStream* tty; // tty output
class streamIndentor : public StackObj { private:
outputStream* _str; int _amount;
public:
streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) {
_str->inc(_amount);
}
~streamIndentor() { _str->dec(_amount); }
};
// advisory locking for the shared tty stream: class ttyLocker: StackObj { friendclass ttyUnlocker; private:
intx _holder;
public: static intx hold_tty(); // returns a "holder" token staticvoid release_tty(intx holder); // must witness same token staticbool release_tty_if_locked(); // returns true if lock was released staticvoid break_tty_lock_for_safepoint(intx holder);
// Release the tty lock if it's held and reacquire it if it was // locked. Used to avoid lock ordering problems. class ttyUnlocker: StackObj { private: bool _was_locked; public:
ttyUnlocker() {
_was_locked = ttyLocker::release_tty_if_locked();
}
~ttyUnlocker() { if (_was_locked) {
ttyLocker::hold_tty();
}
}
};
// for writing to strings; buffer will expand automatically. // Buffer will always be zero-terminated. class stringStream : public outputStream {
DEBUG_ONLY(bool _is_frozen = false); char* _buffer;
size_t _written; // Number of characters written, excluding termin. zero
size_t _capacity; constbool _is_fixed; char _small_buffer[48];
// Grow backing buffer to desired capacity. void grow(size_t new_capacity);
// zero terminate at buffer_pos. void zero_terminate();
public: // Create a stringStream using an internal buffer of initially initial_bufsize size; // will be enlarged on demand. There is no maximum cap.
stringStream(size_t initial_capacity = 0); // Creates a stringStream using a caller-provided buffer. Will truncate silently if // it overflows.
stringStream(char* fixed_buffer, size_t fixed_buffer_size);
~stringStream(); virtualvoid write(constchar* c, size_t len); // Return number of characters written into buffer, excluding terminating zero and // subject to truncation in static buffer mode.
size_t size() const { return _written; } // Returns internal buffer containing the accumulated string. // Returned buffer is only guaranteed to be valid as long as stream is not modified constchar* base() const { return _buffer; } // Freezes stringStream (no further modifications possible) and returns pointer to it. // No-op if stream is frozen already. // Returns the internal buffer containing the accumulated string. constchar* freeze() NOT_DEBUG(const) {
DEBUG_ONLY(_is_frozen = true); return _buffer;
}; void reset(); // Copy to a resource, or C-heap, array as requested char* as_string(bool c_heap = false) const;
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.