src/hotspot/share/utilities/defaultStream.hpp
changeset 47216 71c04702a3d5
parent 28509 56efbcb555fb
child 53244 9807daeb47c4
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2003, 2010, 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 
       
    25 #ifndef SHARE_VM_UTILITIES_DEFAULTSTREAM_HPP
       
    26 #define SHARE_VM_UTILITIES_DEFAULTSTREAM_HPP
       
    27 
       
    28 #include "utilities/xmlstream.hpp"
       
    29 
       
    30 class defaultStream : public xmlTextStream {
       
    31   friend void ostream_abort();
       
    32  public:
       
    33   enum { NO_WRITER = -1 };
       
    34  private:
       
    35   bool         _inited;
       
    36   fileStream*  _log_file;  // XML-formatted file shared by all threads
       
    37   static int   _output_fd;
       
    38   static int   _error_fd;
       
    39   static FILE* _output_stream;
       
    40   static FILE* _error_stream;
       
    41 
       
    42   void init();
       
    43   void init_log();
       
    44   fileStream* open_file(const char* log_name);
       
    45   void start_log();
       
    46   void finish_log();
       
    47   void finish_log_on_error(char *buf, int buflen);
       
    48  public:
       
    49   // must defer time stamp due to the fact that os::init() hasn't
       
    50   // yet been called and os::elapsed_counter() may not be valid
       
    51   defaultStream() {
       
    52     _log_file = NULL;
       
    53     _inited = false;
       
    54     _writer = -1;
       
    55     _last_writer = -1;
       
    56   }
       
    57 
       
    58   ~defaultStream() {
       
    59     if (has_log_file())  finish_log();
       
    60   }
       
    61 
       
    62   static inline FILE* output_stream() {
       
    63     return DisplayVMOutputToStderr ? _error_stream : _output_stream;
       
    64   }
       
    65   static inline FILE* error_stream() {
       
    66     return DisplayVMOutputToStdout ? _output_stream : _error_stream;
       
    67   }
       
    68   static inline int output_fd() {
       
    69     return DisplayVMOutputToStderr ? _error_fd : _output_fd;
       
    70   }
       
    71   static inline int error_fd() {
       
    72     return DisplayVMOutputToStdout ? _output_fd : _error_fd;
       
    73   }
       
    74 
       
    75   virtual void write(const char* s, size_t len);
       
    76 
       
    77   void flush() {
       
    78     // once we can determine whether we are in a signal handler, we
       
    79     // should add the following assert here:
       
    80     // assert(xxxxxx, "can not flush buffer inside signal handler");
       
    81     xmlTextStream::flush();
       
    82     fflush(output_stream());
       
    83     if (has_log_file()) _log_file->flush();
       
    84   }
       
    85 
       
    86   // advisory lock/unlock of _writer field:
       
    87  private:
       
    88   intx _writer;    // thread_id with current rights to output
       
    89   intx _last_writer;
       
    90  public:
       
    91   intx hold(intx writer_id);
       
    92   void release(intx holder);
       
    93   intx writer() { return _writer; }
       
    94   bool has_log_file();
       
    95 
       
    96   static defaultStream* instance;  // sole instance
       
    97 };
       
    98 
       
    99 #endif // SHARE_VM_UTILITIES_DEFAULTSTREAM_HPP