1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/utilities/defaultStream.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,97 @@ 1.4 +/* 1.5 + * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef SHARE_VM_UTILITIES_DEFAULTSTREAM_HPP 1.29 +#define SHARE_VM_UTILITIES_DEFAULTSTREAM_HPP 1.30 + 1.31 +#include "utilities/xmlstream.hpp" 1.32 + 1.33 +class defaultStream : public xmlTextStream { 1.34 + friend void ostream_abort(); 1.35 + public: 1.36 + enum { NO_WRITER = -1 }; 1.37 + private: 1.38 + bool _inited; 1.39 + fileStream* _log_file; // XML-formatted file shared by all threads 1.40 + static int _output_fd; 1.41 + static int _error_fd; 1.42 + static FILE* _output_stream; 1.43 + static FILE* _error_stream; 1.44 + 1.45 + void init(); 1.46 + void init_log(); 1.47 + void finish_log(); 1.48 + void finish_log_on_error(char *buf, int buflen); 1.49 + public: 1.50 + // must defer time stamp due to the fact that os::init() hasn't 1.51 + // yet been called and os::elapsed_counter() may not be valid 1.52 + defaultStream() { 1.53 + _log_file = NULL; 1.54 + _inited = false; 1.55 + _writer = -1; 1.56 + _last_writer = -1; 1.57 + } 1.58 + 1.59 + ~defaultStream() { 1.60 + if (has_log_file()) finish_log(); 1.61 + } 1.62 + 1.63 + static inline FILE* output_stream() { 1.64 + return DisplayVMOutputToStderr ? _error_stream : _output_stream; 1.65 + } 1.66 + static inline FILE* error_stream() { 1.67 + return DisplayVMOutputToStdout ? _output_stream : _error_stream; 1.68 + } 1.69 + static inline int output_fd() { 1.70 + return DisplayVMOutputToStderr ? _error_fd : _output_fd; 1.71 + } 1.72 + static inline int error_fd() { 1.73 + return DisplayVMOutputToStdout ? _output_fd : _error_fd; 1.74 + } 1.75 + 1.76 + virtual void write(const char* s, size_t len); 1.77 + 1.78 + void flush() { 1.79 + // once we can determine whether we are in a signal handler, we 1.80 + // should add the following assert here: 1.81 + // assert(xxxxxx, "can not flush buffer inside signal handler"); 1.82 + xmlTextStream::flush(); 1.83 + fflush(output_stream()); 1.84 + if (has_log_file()) _log_file->flush(); 1.85 + } 1.86 + 1.87 + // advisory lock/unlock of _writer field: 1.88 + private: 1.89 + intx _writer; // thread_id with current rights to output 1.90 + intx _last_writer; 1.91 + public: 1.92 + intx hold(intx writer_id); 1.93 + void release(intx holder); 1.94 + intx writer() { return _writer; } 1.95 + bool has_log_file(); 1.96 + 1.97 + static defaultStream* instance; // sole instance 1.98 +}; 1.99 + 1.100 +#endif // SHARE_VM_UTILITIES_DEFAULTSTREAM_HPP