duke@435: /* stefank@2314: * Copyright (c) 2003, 2010, 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_DEFAULTSTREAM_HPP stefank@2314: #define SHARE_VM_UTILITIES_DEFAULTSTREAM_HPP stefank@2314: stefank@2314: #include "utilities/xmlstream.hpp" stefank@2314: duke@435: class defaultStream : public xmlTextStream { duke@435: friend void ostream_abort(); duke@435: public: duke@435: enum { NO_WRITER = -1 }; duke@435: private: duke@435: bool _inited; duke@435: fileStream* _log_file; // XML-formatted file shared by all threads duke@435: static int _output_fd; duke@435: static int _error_fd; duke@435: static FILE* _output_stream; duke@435: static FILE* _error_stream; duke@435: duke@435: void init(); duke@435: void init_log(); duke@435: void finish_log(); duke@435: void finish_log_on_error(char *buf, int buflen); duke@435: public: duke@435: // must defer time stamp due to the fact that os::init() hasn't duke@435: // yet been called and os::elapsed_counter() may not be valid duke@435: defaultStream() { duke@435: _log_file = NULL; duke@435: _inited = false; duke@435: _writer = -1; duke@435: _last_writer = -1; duke@435: } duke@435: duke@435: ~defaultStream() { duke@435: if (has_log_file()) finish_log(); duke@435: } duke@435: duke@435: static inline FILE* output_stream() { duke@435: return DisplayVMOutputToStderr ? _error_stream : _output_stream; duke@435: } duke@435: static inline FILE* error_stream() { duke@435: return DisplayVMOutputToStdout ? _output_stream : _error_stream; duke@435: } duke@435: static inline int output_fd() { duke@435: return DisplayVMOutputToStderr ? _error_fd : _output_fd; duke@435: } duke@435: static inline int error_fd() { duke@435: return DisplayVMOutputToStdout ? _output_fd : _error_fd; duke@435: } duke@435: duke@435: virtual void write(const char* s, size_t len); duke@435: duke@435: void flush() { duke@435: // once we can determine whether we are in a signal handler, we duke@435: // should add the following assert here: duke@435: // assert(xxxxxx, "can not flush buffer inside signal handler"); duke@435: xmlTextStream::flush(); duke@435: fflush(output_stream()); duke@435: if (has_log_file()) _log_file->flush(); duke@435: } duke@435: duke@435: // advisory lock/unlock of _writer field: duke@435: private: duke@435: intx _writer; // thread_id with current rights to output duke@435: intx _last_writer; duke@435: public: duke@435: intx hold(intx writer_id); duke@435: void release(intx holder); duke@435: intx writer() { return _writer; } duke@435: bool has_log_file(); duke@435: duke@435: static defaultStream* instance; // sole instance duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_UTILITIES_DEFAULTSTREAM_HPP