Path: blob/master/src/hotspot/share/utilities/defaultStream.hpp
40949 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_UTILITIES_DEFAULTSTREAM_HPP25#define SHARE_UTILITIES_DEFAULTSTREAM_HPP2627#include "utilities/xmlstream.hpp"2829class defaultStream : public xmlTextStream {30friend void ostream_abort();31public:32enum { NO_WRITER = -1 };33private:34bool _inited;35fileStream* _log_file; // XML-formatted file shared by all threads36static int _output_fd;37static int _error_fd;38static FILE* _output_stream;39static FILE* _error_stream;4041void init();42void init_log();43fileStream* open_file(const char* log_name);44void start_log();45void finish_log();46void finish_log_on_error(char *buf, int buflen);47public:48// must defer time stamp due to the fact that os::init() hasn't49// yet been called and os::elapsed_counter() may not be valid50defaultStream() {51_log_file = NULL;52_inited = false;53_writer = -1;54_last_writer = -1;55}5657~defaultStream() {58if (has_log_file()) finish_log();59}6061static inline FILE* output_stream() {62return DisplayVMOutputToStderr ? _error_stream : _output_stream;63}64static inline FILE* error_stream() {65return DisplayVMOutputToStdout ? _output_stream : _error_stream;66}67static inline int output_fd() {68return DisplayVMOutputToStderr ? _error_fd : _output_fd;69}70static inline int error_fd() {71return DisplayVMOutputToStdout ? _output_fd : _error_fd;72}7374virtual void write(const char* s, size_t len);7576void flush() {77// once we can determine whether we are in a signal handler, we78// should add the following assert here:79// assert(xxxxxx, "can not flush buffer inside signal handler");80xmlTextStream::flush();81fflush(output_stream());82if (has_log_file()) _log_file->flush();83}8485// advisory lock/unlock of _writer field:86private:87intx _writer; // thread_id with current rights to output88intx _last_writer;89public:90intx hold(intx writer_id);91void release(intx holder);92intx writer() { return _writer; }93bool has_log_file();9495static defaultStream* instance; // sole instance96};9798#endif // SHARE_UTILITIES_DEFAULTSTREAM_HPP99100101