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