< prev index next >

src/share/vm/logging/logStream.hpp

Print this page
rev 13265 : imported patch 8181917-refactor-ul-logstream


  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_LOGGING_LOGSTREAM_HPP
  26 #define SHARE_VM_LOGGING_LOGSTREAM_HPP
  27 
  28 #include "logging/log.hpp"
  29 #include "logging/logHandle.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "utilities/ostream.hpp"
  32 
  33 // The base class of an output stream that logs to the logging framework.
  34 template <class streamClass>
  35 class LogStreamBase : public outputStream {
  36   streamClass     _current_line;



















  37   LogTargetHandle _log_handle;
  38 




  39 public:
  40   // Constructor to support creation from a LogTarget instance.
  41   //
  42   // LogTarget(Debug, gc) log;
  43   // LogStreamBase(log) stream;
  44   template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
  45   LogStreamBase(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
  46       _log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
  47 
  48   // Constructor to support creation from typed (likely NULL) pointer. Mostly used by the logging framework.
  49   //
  50   // LogStreamBase stream(log.debug());
  51   //  or
  52   // LogStreamBase stream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL);
  53   template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
  54   LogStreamBase(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>* type_carrier) :
  55       _log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
  56 
  57   // Constructor to support creation from a LogTargetHandle.
  58   //
  59   // LogTarget(Debug, gc) log;
  60   // LogTargetHandle(log) handle;
  61   // LogStreamBase stream(handle);
  62   LogStreamBase(LogTargetHandle handle) : _log_handle(handle) {}
  63 
  64   // Constructor to support creation from a log level and tagset.
  65   //
  66   // LogStreamBase(level, tageset);
  67   LogStreamBase(LogLevelType level, LogTagSet* tagset) : _log_handle(level, tagset) {}
  68 
  69   ~LogStreamBase() {
  70     guarantee(_current_line.size() == 0, "Buffer not flushed. Missing call to print_cr()?");
  71   }
  72 
  73 public:
  74   void write(const char* s, size_t len);
  75 };
  76 
  77 // A stringStream with an embedded ResourceMark.
  78 class stringStreamWithResourceMark : outputStream {
  79  private:
  80   // The stringStream Resource allocate in the constructor,
  81   // so the order of the fields is important.
  82   ResourceMark _embedded_resource_mark;
  83   stringStream _stream;
  84 
  85  public:
  86   stringStreamWithResourceMark(size_t initial_bufsize = 256) :
  87       _embedded_resource_mark(),
  88       _stream(initial_bufsize) {}
  89 
  90   virtual void write(const char* c, size_t len) { _stream.write(c, len); }
  91   size_t      size()                            { return _stream.size(); }
  92   const char* base()                            { return _stream.base(); }
  93   void  reset()                                 { _stream.reset(); }
  94   char* as_string()                             { return _stream.as_string(); }
  95 };
  96 
  97 // An output stream that logs to the logging framework.
  98 //
  99 // The backing buffer is allocated in Resource memory.
 100 // The caller is required to have a ResourceMark on the stack.
 101 typedef LogStreamBase<stringStream> LogStreamNoResourceMark;
 102 
 103 // An output stream that logs to the logging framework.
 104 //
 105 // The backing buffer is allocated in CHeap memory.
 106 typedef LogStreamBase<bufferedStream> LogStreamCHeap;
 107 
 108 // An output stream that logs to the logging framework, and embeds a ResourceMark.
 109 //
 110 // The backing buffer is allocated in Resource memory.
 111 // The class is intended to be stack allocated.
 112 // The class provides its own ResourceMark,
 113 //  so care needs to be taken when nested ResourceMarks are used.
 114 typedef LogStreamBase<stringStreamWithResourceMark> LogStream;
 115 
 116 // Support creation of a LogStream without having to provide a LogTarget pointer.
 117 #define LogStreamHandle(level, ...) LogStreamTemplate<LogLevel::level, LOG_TAGS(__VA_ARGS__)>
 118 
 119 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
 120 class LogStreamTemplate : public LogStream {
 121 public:
 122   LogStreamTemplate() : LogStream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL) {}
 123 };
 124 
 125 #endif // SHARE_VM_LOGGING_LOGSTREAM_HPP


  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_LOGGING_LOGSTREAM_HPP
  26 #define SHARE_VM_LOGGING_LOGSTREAM_HPP
  27 
  28 #include "logging/log.hpp"
  29 #include "logging/logHandle.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "utilities/ostream.hpp"
  32 
  33 
  34 class LogStream : public outputStream {
  35   friend class LogStreamTest_TestLineBufferAllocation_test_vm_Test; // see test/native/logging/test_logStream.cpp
  36   friend class LogStreamTest_TestLineBufferAllocationCap_test_vm_Test; // see test/native/logging/test_logStream.cpp
  37 
  38   // Helper class, maintains the line buffer. For small line lengths,
  39   // we avoid malloc and use a fixed sized member char array. If LogStream
  40   // is allocated on the stack, this means small lines are assembled
  41   // directly on the stack.
  42   class LineBuffer {
  43     char _smallbuf[64];
  44     char* _buf;
  45     size_t _cap;
  46     size_t _pos;
  47     void try_ensure_cap(size_t cap);
  48   public:
  49     LineBuffer();
  50     ~LineBuffer();
  51     const char* ptr() const { return _buf; }
  52     void append(const char* s, size_t len);
  53     void reset();
  54   };
  55   LineBuffer  _current_line;
  56   LogTargetHandle _log_handle;
  57 
  58   // Prevent operator new for LogStream.
  59   static void* operator new (size_t);
  60   static void* operator new[] (size_t);
  61 
  62 public:
  63   // Constructor to support creation from a LogTarget instance.
  64   //
  65   // LogTarget(Debug, gc) log;
  66   // LogStreamBase(log) stream;
  67   template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
  68   LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
  69       _log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
  70 
  71   // Constructor to support creation from typed (likely NULL) pointer. Mostly used by the logging framework.
  72   //
  73   // LogStreamBase stream(log.debug());
  74   //  or
  75   // LogStreamBase stream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL);
  76   template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
  77   LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>* type_carrier) :
  78       _log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
  79 
  80   // Constructor to support creation from a LogTargetHandle.
  81   //
  82   // LogTarget(Debug, gc) log;
  83   // LogTargetHandle(log) handle;
  84   // LogStreamBase stream(handle);
  85   LogStream(LogTargetHandle handle) : _log_handle(handle) {}
  86 
  87   // Constructor to support creation from a log level and tagset.
  88   //
  89   // LogStreamBase(level, tageset);
  90   LogStream(LogLevelType level, LogTagSet* tagset) : _log_handle(level, tagset) {}
  91 





  92   void write(const char* s, size_t len);
  93 };







































  94 
  95 // Support creation of a LogStream without having to provide a LogTarget pointer.
  96 #define LogStreamHandle(level, ...) LogStreamTemplate<LogLevel::level, LOG_TAGS(__VA_ARGS__)>
  97 
  98 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
  99 class LogStreamTemplate : public LogStream {
 100 public:
 101   LogStreamTemplate() : LogStream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL) {}
 102 };
 103 
 104 #endif // SHARE_VM_LOGGING_LOGSTREAM_HPP
< prev index next >