1 /*
   2  * Copyright (c) 2016, 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 #ifndef SHARE_VM_LOGGING_LOGMESSAGEBUFFER_HPP
  25 #define SHARE_VM_LOGGING_LOGMESSAGEBUFFER_HPP
  26 
  27 #include "logging/logDecorations.hpp"
  28 #include "logging/logLevel.hpp"
  29 #include "memory/allocation.hpp"
  30 
  31 class LogMessageBuffer : public StackObj {
  32   friend class LogMessageTest;
  33  protected:
  34   struct LogLine VALUE_OBJ_CLASS_SPEC {
  35     LogLevelType level;
  36     size_t message_offset;
  37   };
  38   static const size_t InitialLineCapacity = 10;
  39   static const size_t InitialMessageBufferCapacity = 1024;
  40   bool _c_heap_allocated;
  41 
  42   size_t _message_buffer_size;
  43   size_t _message_buffer_capacity;
  44   char* _message_buffer;
  45 
  46   size_t _line_count;
  47   size_t _line_capacity;
  48   LogLine* _lines;
  49 
  50   LogLevelType _least_detailed_level;
  51 
  52   size_t (*_prefix_fn)(char*, size_t);
  53 
  54   void initialize_buffers();
  55 
  56   LogMessageBuffer();
  57   ~LogMessageBuffer();
  58 
  59   // Forbidden copy assignment and copy constructor.
  60   void operator=(const LogMessageBuffer& ref) {}
  61   LogMessageBuffer(const LogMessageBuffer& ref) {}
  62 
  63  public:
  64 
  65   class Iterator {
  66    private:
  67     const LogMessageBuffer& _message;
  68     size_t _current_line_index;
  69     LogLevelType _level;
  70     LogDecorations &_decorations;
  71 
  72     void skip_messages_with_finer_level();
  73 
  74    public:
  75     Iterator(const LogMessageBuffer& message, LogLevelType level, LogDecorations& decorations)
  76         : _message(message), _level(level), _decorations(decorations), _current_line_index(0) {
  77       skip_messages_with_finer_level();
  78     }
  79 
  80     void operator++(int) {
  81       _current_line_index++;
  82       skip_messages_with_finer_level();
  83     }
  84 
  85     bool is_at_end() {
  86       return _current_line_index == _message._line_count;
  87     }
  88 
  89     const char* message() const {
  90       return _message._message_buffer + _message._lines[_current_line_index].message_offset;
  91     }
  92 
  93     const LogDecorations& decorations() {
  94       _decorations.set_level(_message._lines[_current_line_index].level);
  95       return _decorations;
  96     }
  97   };
  98 
  99   void clear();
 100 
 101   LogLevelType least_detailed_level() const {
 102     return _least_detailed_level;
 103   }
 104 
 105   Iterator iterator(LogLevelType level, LogDecorations& decorations) const {
 106     return Iterator(*this, level, decorations);
 107   }
 108 
 109   // Lines in LogMessageBuffers are not automatically prefixed based on tags
 110   // like regular simple messages (see LogPrefix.hpp for more about prefixes).
 111   // It is, however, possible to specify a prefix per LogMessageBuffer,
 112   // using set_prefix(). Lines added to the LogMessageBuffer after a prefix
 113   // function has been set will be prefixed automatically.
 114   // Setting this to NULL will disable prefixing.
 115   void set_prefix(size_t (*prefix_fn)(char*, size_t)) {
 116     _prefix_fn = prefix_fn;
 117   }
 118 
 119   ATTRIBUTE_PRINTF(3, 4)
 120   void write(LogLevelType level, const char* fmt, ...);
 121 
 122   ATTRIBUTE_PRINTF(3, 0)
 123   void vwrite(LogLevelType level, const char* fmt, va_list args);
 124 
 125 #define LOG_LEVEL(level, name) \
 126   LogMessageBuffer& v##name(const char* fmt, va_list args) ATTRIBUTE_PRINTF(2, 0); \
 127   LogMessageBuffer& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3);
 128   LOG_LEVEL_LIST
 129 #undef LOG_LEVEL
 130 };
 131 
 132 #endif // SHARE_VM_LOGGING_LOGMESSAGEBUFFER_HPP