Wed, 03 Jul 2019 20:42:37 +0800
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #ifndef SHARE_VM_UTILITIES_XMLSTREAM_HPP |
aoqi@0 | 26 | #define SHARE_VM_UTILITIES_XMLSTREAM_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | #include "runtime/handles.hpp" |
aoqi@0 | 29 | #include "utilities/ostream.hpp" |
aoqi@0 | 30 | |
aoqi@0 | 31 | class xmlStream; |
aoqi@0 | 32 | class defaultStream; |
aoqi@0 | 33 | |
aoqi@0 | 34 | // Sub-stream for writing quoted text, as opposed to markup. |
aoqi@0 | 35 | // Characters written to this stream are subject to quoting, |
aoqi@0 | 36 | // as '<' => "<", etc. |
aoqi@0 | 37 | class xmlTextStream : public outputStream { |
aoqi@0 | 38 | friend class xmlStream; |
aoqi@0 | 39 | friend class defaultStream; // tty |
aoqi@0 | 40 | private: |
aoqi@0 | 41 | |
aoqi@0 | 42 | xmlStream* _outer_xmlStream; |
aoqi@0 | 43 | |
aoqi@0 | 44 | xmlTextStream() { _outer_xmlStream = NULL; } |
aoqi@0 | 45 | |
aoqi@0 | 46 | public: |
aoqi@0 | 47 | virtual void flush(); // _outer.flush(); |
aoqi@0 | 48 | virtual void write(const char* str, size_t len); // _outer->write_text() |
aoqi@0 | 49 | }; |
aoqi@0 | 50 | |
aoqi@0 | 51 | |
aoqi@0 | 52 | // Output stream for writing XML-structured logs. |
aoqi@0 | 53 | // To write markup, use special calls elem, head/tail, etc. |
aoqi@0 | 54 | // Use the xmlStream::text() stream to write unmarked text. |
aoqi@0 | 55 | // Text written that way will be quoted as necessary using '<', etc. |
aoqi@0 | 56 | // Characters written directly to an xmlStream via print_cr, etc., |
aoqi@0 | 57 | // are directly written to the encapsulated stream, xmlStream::out(). |
aoqi@0 | 58 | // This can be used to produce markup directly, character by character. |
aoqi@0 | 59 | // (Such writes are not checked for markup syntax errors.) |
aoqi@0 | 60 | |
aoqi@0 | 61 | class xmlStream : public outputStream { |
aoqi@0 | 62 | friend class defaultStream; // tty |
aoqi@0 | 63 | public: |
aoqi@0 | 64 | enum MarkupState { BODY, // after end_head() call, in text |
aoqi@0 | 65 | HEAD, // after begin_head() call, in attrs |
aoqi@0 | 66 | ELEM }; // after begin_elem() call, in attrs |
aoqi@0 | 67 | |
aoqi@0 | 68 | protected: |
aoqi@0 | 69 | outputStream* _out; // file stream by which it goes |
aoqi@0 | 70 | julong _last_flush; // last position of flush |
aoqi@0 | 71 | MarkupState _markup_state; // where in the elem/head/tail dance |
aoqi@0 | 72 | outputStream* _text; // text stream |
aoqi@0 | 73 | xmlTextStream _text_init; |
aoqi@0 | 74 | |
aoqi@0 | 75 | // for subclasses |
aoqi@0 | 76 | xmlStream() {} |
aoqi@0 | 77 | void initialize(outputStream* out); |
aoqi@0 | 78 | |
aoqi@0 | 79 | // protect this from public use: |
aoqi@0 | 80 | outputStream* out() { return _out; } |
aoqi@0 | 81 | |
aoqi@0 | 82 | // helpers for writing XML elements |
aoqi@0 | 83 | void va_tag(bool push, const char* format, va_list ap) ATTRIBUTE_PRINTF(3, 0); |
aoqi@0 | 84 | virtual void see_tag(const char* tag, bool push) NOT_DEBUG({}); |
aoqi@0 | 85 | virtual void pop_tag(const char* tag) NOT_DEBUG({}); |
aoqi@0 | 86 | |
aoqi@0 | 87 | #ifdef ASSERT |
aoqi@0 | 88 | // in debug mode, we verify matching of opening and closing tags |
aoqi@0 | 89 | int _element_depth; // number of unfinished elements |
aoqi@0 | 90 | char* _element_close_stack_high; // upper limit of down-growing stack |
aoqi@0 | 91 | char* _element_close_stack_low; // upper limit of down-growing stack |
aoqi@0 | 92 | char* _element_close_stack_ptr; // pointer of down-growing stack |
aoqi@0 | 93 | #endif |
aoqi@0 | 94 | |
aoqi@0 | 95 | public: |
aoqi@0 | 96 | // creation |
aoqi@0 | 97 | xmlStream(outputStream* out) { initialize(out); } |
aoqi@0 | 98 | DEBUG_ONLY(virtual ~xmlStream();) |
aoqi@0 | 99 | |
aoqi@0 | 100 | bool is_open() { return _out != NULL; } |
aoqi@0 | 101 | |
aoqi@0 | 102 | // text output |
aoqi@0 | 103 | bool inside_attrs() { return _markup_state != BODY; } |
aoqi@0 | 104 | |
aoqi@0 | 105 | // flushing |
aoqi@0 | 106 | virtual void flush(); // flushes out, sets _last_flush = count() |
aoqi@0 | 107 | virtual void write(const char* s, size_t len); |
aoqi@0 | 108 | void write_text(const char* s, size_t len); // used by xmlTextStream |
aoqi@0 | 109 | int unflushed_count() { return (int)(out()->count() - _last_flush); } |
aoqi@0 | 110 | |
aoqi@0 | 111 | // writing complete XML elements |
aoqi@0 | 112 | void elem(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); |
aoqi@0 | 113 | void begin_elem(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); |
aoqi@0 | 114 | void end_elem(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); |
aoqi@0 | 115 | void end_elem(); |
aoqi@0 | 116 | void head(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); |
aoqi@0 | 117 | void begin_head(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); |
aoqi@0 | 118 | void end_head(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); |
aoqi@0 | 119 | void end_head(); |
aoqi@0 | 120 | void done(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); // xxx_done event, plus tail |
aoqi@0 | 121 | void done_raw(const char * kind); |
aoqi@0 | 122 | void tail(const char* kind); |
aoqi@0 | 123 | |
aoqi@0 | 124 | // va_list versions |
aoqi@0 | 125 | void va_elem(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0); |
aoqi@0 | 126 | void va_begin_elem(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0); |
aoqi@0 | 127 | void va_head(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0); |
aoqi@0 | 128 | void va_begin_head(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0); |
aoqi@0 | 129 | void va_done(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0); |
aoqi@0 | 130 | |
aoqi@0 | 131 | // write text (with quoting of special XML characters <>&'" etc.) |
aoqi@0 | 132 | outputStream* text() { return _text; } |
aoqi@0 | 133 | void text(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); |
aoqi@0 | 134 | void va_text(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0) { |
aoqi@0 | 135 | text()->vprint(format, ap); |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | // commonly used XML attributes |
aoqi@0 | 139 | void stamp(); // stamp='1.234' |
aoqi@0 | 140 | void method(methodHandle m); // method='k n s' ... |
aoqi@0 | 141 | void klass(KlassHandle k); // klass='name' |
aoqi@0 | 142 | void name(const Symbol* s); // name='name' |
aoqi@0 | 143 | void object(const char* attr, Metadata* val); |
aoqi@0 | 144 | void object(const char* attr, Handle val); |
aoqi@0 | 145 | |
aoqi@0 | 146 | // print the text alone (sans ''): |
aoqi@0 | 147 | void method_text(methodHandle m); |
aoqi@0 | 148 | void klass_text(KlassHandle k); // klass='name' |
aoqi@0 | 149 | void name_text(const Symbol* s); // name='name' |
aoqi@0 | 150 | void object_text(Metadata* x); |
aoqi@0 | 151 | void object_text(Handle x); |
aoqi@0 | 152 | |
aoqi@0 | 153 | /* Example uses: |
aoqi@0 | 154 | |
aoqi@0 | 155 | // Empty element, simple case. |
aoqi@0 | 156 | elem("X Y='Z'"); <X Y='Z'/> \n |
aoqi@0 | 157 | |
aoqi@0 | 158 | // Empty element, general case. |
aoqi@0 | 159 | begin_elem("X Y='Z'"); <X Y='Z' |
aoqi@0 | 160 | ...attrs... ...attrs... |
aoqi@0 | 161 | end_elem(); /> |
aoqi@0 | 162 | |
aoqi@0 | 163 | // Compound element, simple case. |
aoqi@0 | 164 | head("X Y='Z'"); <X Y='Z'> \n |
aoqi@0 | 165 | ...body... ...body... |
aoqi@0 | 166 | tail("X"); </X> \n |
aoqi@0 | 167 | |
aoqi@0 | 168 | // Compound element, general case. |
aoqi@0 | 169 | begin_head("X Y='Z'"); <X Y='Z' |
aoqi@0 | 170 | ...attrs... ...attrs... |
aoqi@0 | 171 | end_head(); > \n |
aoqi@0 | 172 | ...body... ...body... |
aoqi@0 | 173 | tail("X"); </X> \n |
aoqi@0 | 174 | |
aoqi@0 | 175 | // Printf-style formatting: |
aoqi@0 | 176 | elem("X Y='%s'", "Z"); <X Y='Z'/> \n |
aoqi@0 | 177 | |
aoqi@0 | 178 | */ |
aoqi@0 | 179 | |
aoqi@0 | 180 | }; |
aoqi@0 | 181 | |
aoqi@0 | 182 | // Standard log file, null if no logging is happening. |
aoqi@0 | 183 | extern xmlStream* xtty; |
aoqi@0 | 184 | |
aoqi@0 | 185 | // Note: If ::xtty != NULL, ::tty == ::xtty->text(). |
aoqi@0 | 186 | |
aoqi@0 | 187 | #endif // SHARE_VM_UTILITIES_XMLSTREAM_HPP |