aoqi@0: /* aoqi@0: * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_UTILITIES_XMLSTREAM_HPP aoqi@0: #define SHARE_VM_UTILITIES_XMLSTREAM_HPP aoqi@0: aoqi@0: #include "runtime/handles.hpp" aoqi@0: #include "utilities/ostream.hpp" aoqi@0: aoqi@0: class xmlStream; aoqi@0: class defaultStream; aoqi@0: aoqi@0: // Sub-stream for writing quoted text, as opposed to markup. aoqi@0: // Characters written to this stream are subject to quoting, aoqi@0: // as '<' => "<", etc. aoqi@0: class xmlTextStream : public outputStream { aoqi@0: friend class xmlStream; aoqi@0: friend class defaultStream; // tty aoqi@0: private: aoqi@0: aoqi@0: xmlStream* _outer_xmlStream; aoqi@0: aoqi@0: xmlTextStream() { _outer_xmlStream = NULL; } aoqi@0: aoqi@0: public: aoqi@0: virtual void flush(); // _outer.flush(); aoqi@0: virtual void write(const char* str, size_t len); // _outer->write_text() aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // Output stream for writing XML-structured logs. aoqi@0: // To write markup, use special calls elem, head/tail, etc. aoqi@0: // Use the xmlStream::text() stream to write unmarked text. aoqi@0: // Text written that way will be quoted as necessary using '<', etc. aoqi@0: // Characters written directly to an xmlStream via print_cr, etc., aoqi@0: // are directly written to the encapsulated stream, xmlStream::out(). aoqi@0: // This can be used to produce markup directly, character by character. aoqi@0: // (Such writes are not checked for markup syntax errors.) aoqi@0: aoqi@0: class xmlStream : public outputStream { aoqi@0: friend class defaultStream; // tty aoqi@0: public: aoqi@0: enum MarkupState { BODY, // after end_head() call, in text aoqi@0: HEAD, // after begin_head() call, in attrs aoqi@0: ELEM }; // after begin_elem() call, in attrs aoqi@0: aoqi@0: protected: aoqi@0: outputStream* _out; // file stream by which it goes aoqi@0: julong _last_flush; // last position of flush aoqi@0: MarkupState _markup_state; // where in the elem/head/tail dance aoqi@0: outputStream* _text; // text stream aoqi@0: xmlTextStream _text_init; aoqi@0: aoqi@0: // for subclasses aoqi@0: xmlStream() {} aoqi@0: void initialize(outputStream* out); aoqi@0: aoqi@0: // protect this from public use: aoqi@0: outputStream* out() { return _out; } aoqi@0: aoqi@0: // helpers for writing XML elements aoqi@0: void va_tag(bool push, const char* format, va_list ap) ATTRIBUTE_PRINTF(3, 0); aoqi@0: virtual void see_tag(const char* tag, bool push) NOT_DEBUG({}); aoqi@0: virtual void pop_tag(const char* tag) NOT_DEBUG({}); aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: // in debug mode, we verify matching of opening and closing tags aoqi@0: int _element_depth; // number of unfinished elements aoqi@0: char* _element_close_stack_high; // upper limit of down-growing stack aoqi@0: char* _element_close_stack_low; // upper limit of down-growing stack aoqi@0: char* _element_close_stack_ptr; // pointer of down-growing stack aoqi@0: #endif aoqi@0: aoqi@0: public: aoqi@0: // creation aoqi@0: xmlStream(outputStream* out) { initialize(out); } aoqi@0: DEBUG_ONLY(virtual ~xmlStream();) aoqi@0: aoqi@0: bool is_open() { return _out != NULL; } aoqi@0: aoqi@0: // text output aoqi@0: bool inside_attrs() { return _markup_state != BODY; } aoqi@0: aoqi@0: // flushing aoqi@0: virtual void flush(); // flushes out, sets _last_flush = count() aoqi@0: virtual void write(const char* s, size_t len); aoqi@0: void write_text(const char* s, size_t len); // used by xmlTextStream aoqi@0: int unflushed_count() { return (int)(out()->count() - _last_flush); } aoqi@0: aoqi@0: // writing complete XML elements aoqi@0: void elem(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); aoqi@0: void begin_elem(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); aoqi@0: void end_elem(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); aoqi@0: void end_elem(); aoqi@0: void head(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); aoqi@0: void begin_head(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); aoqi@0: void end_head(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); aoqi@0: void end_head(); aoqi@0: void done(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); // xxx_done event, plus tail aoqi@0: void done_raw(const char * kind); aoqi@0: void tail(const char* kind); aoqi@0: aoqi@0: // va_list versions aoqi@0: void va_elem(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0); aoqi@0: void va_begin_elem(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0); aoqi@0: void va_head(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0); aoqi@0: void va_begin_head(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0); aoqi@0: void va_done(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0); aoqi@0: aoqi@0: // write text (with quoting of special XML characters <>&'" etc.) aoqi@0: outputStream* text() { return _text; } aoqi@0: void text(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); aoqi@0: void va_text(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0) { aoqi@0: text()->vprint(format, ap); aoqi@0: } aoqi@0: aoqi@0: // commonly used XML attributes aoqi@0: void stamp(); // stamp='1.234' aoqi@0: void method(methodHandle m); // method='k n s' ... aoqi@0: void klass(KlassHandle k); // klass='name' aoqi@0: void name(const Symbol* s); // name='name' aoqi@0: void object(const char* attr, Metadata* val); aoqi@0: void object(const char* attr, Handle val); aoqi@0: aoqi@0: // print the text alone (sans ''): aoqi@0: void method_text(methodHandle m); aoqi@0: void klass_text(KlassHandle k); // klass='name' aoqi@0: void name_text(const Symbol* s); // name='name' aoqi@0: void object_text(Metadata* x); aoqi@0: void object_text(Handle x); aoqi@0: aoqi@0: /* Example uses: aoqi@0: aoqi@0: // Empty element, simple case. aoqi@0: elem("X Y='Z'"); \n aoqi@0: aoqi@0: // Empty element, general case. aoqi@0: begin_elem("X Y='Z'"); aoqi@0: aoqi@0: // Compound element, simple case. aoqi@0: head("X Y='Z'"); \n aoqi@0: ...body... ...body... aoqi@0: tail("X"); \n aoqi@0: aoqi@0: // Compound element, general case. aoqi@0: begin_head("X Y='Z'"); \n aoqi@0: ...body... ...body... aoqi@0: tail("X"); \n aoqi@0: aoqi@0: // Printf-style formatting: aoqi@0: elem("X Y='%s'", "Z"); \n aoqi@0: aoqi@0: */ aoqi@0: aoqi@0: }; aoqi@0: aoqi@0: // Standard log file, null if no logging is happening. aoqi@0: extern xmlStream* xtty; aoqi@0: aoqi@0: // Note: If ::xtty != NULL, ::tty == ::xtty->text(). aoqi@0: aoqi@0: #endif // SHARE_VM_UTILITIES_XMLSTREAM_HPP