defaultStream.hpp revision 1472:c18cbe5936b8
1/* 2 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25class defaultStream : public xmlTextStream { 26 friend void ostream_abort(); 27 public: 28 enum { NO_WRITER = -1 }; 29 private: 30 bool _inited; 31 fileStream* _log_file; // XML-formatted file shared by all threads 32 static int _output_fd; 33 static int _error_fd; 34 static FILE* _output_stream; 35 static FILE* _error_stream; 36 37 void init(); 38 void init_log(); 39 void finish_log(); 40 void finish_log_on_error(char *buf, int buflen); 41 public: 42 // must defer time stamp due to the fact that os::init() hasn't 43 // yet been called and os::elapsed_counter() may not be valid 44 defaultStream() { 45 _log_file = NULL; 46 _inited = false; 47 _writer = -1; 48 _last_writer = -1; 49 } 50 51 ~defaultStream() { 52 if (has_log_file()) finish_log(); 53 } 54 55 static inline FILE* output_stream() { 56 return DisplayVMOutputToStderr ? _error_stream : _output_stream; 57 } 58 static inline FILE* error_stream() { 59 return DisplayVMOutputToStdout ? _output_stream : _error_stream; 60 } 61 static inline int output_fd() { 62 return DisplayVMOutputToStderr ? _error_fd : _output_fd; 63 } 64 static inline int error_fd() { 65 return DisplayVMOutputToStdout ? _output_fd : _error_fd; 66 } 67 68 virtual void write(const char* s, size_t len); 69 70 void flush() { 71 // once we can determine whether we are in a signal handler, we 72 // should add the following assert here: 73 // assert(xxxxxx, "can not flush buffer inside signal handler"); 74 xmlTextStream::flush(); 75 fflush(output_stream()); 76 if (has_log_file()) _log_file->flush(); 77 } 78 79 // advisory lock/unlock of _writer field: 80 private: 81 intx _writer; // thread_id with current rights to output 82 intx _last_writer; 83 public: 84 intx hold(intx writer_id); 85 void release(intx holder); 86 intx writer() { return _writer; } 87 bool has_log_file(); 88 89 static defaultStream* instance; // sole instance 90}; 91