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 #include "precompiled.hpp"
  25 #include "logging/logMessageBuffer.hpp"
  26 #include "memory/allocation.inline.hpp"
  27 #include "runtime/thread.inline.hpp"
  28 
  29 template <typename T>
  30 static void grow(T*& buffer, size_t& capacity, bool c_heap, size_t minimum_length = 0) {
  31   size_t new_size = capacity * 2;
  32   if (new_size < minimum_length) {
  33     new_size = minimum_length;
  34   }
  35   if (c_heap) {
  36     buffer = REALLOC_C_HEAP_ARRAY(T, buffer, new_size, mtLogging);
  37   } else {
  38     buffer = REALLOC_RESOURCE_ARRAY(T, buffer, capacity, new_size);
  39   }
  40   capacity = new_size;
  41 }
  42 
  43 LogMessageBuffer::LogMessageBuffer() : _message_buffer_size(0),
  44                                        _message_buffer_capacity(0),
  45                                        _message_buffer(NULL),
  46                                        _line_count(0),
  47                                        _line_capacity(0),
  48                                        _lines(NULL),
  49                                        _c_heap_allocated(Thread::current_or_null() == NULL),
  50                                        _least_detailed_level(LogLevel::Off),
  51                                        _prefix_fn(NULL) {
  52 }
  53 
  54 LogMessageBuffer::~LogMessageBuffer() {
  55   if (_c_heap_allocated) {
  56     FREE_C_HEAP_ARRAY(char, _message_buffer);
  57     FREE_C_HEAP_ARRAY(LogLine, _lines);
  58   } else {
  59     FREE_RESOURCE_ARRAY(char, _message_buffer, _message_buffer_capacity);
  60     FREE_RESOURCE_ARRAY(LogLine, _lines, _line_capacity);
  61   }
  62 }
  63 
  64 void LogMessageBuffer::clear() {
  65   _message_buffer_size = 0;
  66   _line_count = 0;
  67 }
  68 
  69 void LogMessageBuffer::initialize_buffers() {
  70   if (_c_heap_allocated) {
  71     _message_buffer = NEW_C_HEAP_ARRAY(char, InitialMessageBufferCapacity, mtLogging);
  72     _lines = NEW_C_HEAP_ARRAY(LogLine, InitialLineCapacity, mtLogging);
  73   } else {
  74     _message_buffer = NEW_RESOURCE_ARRAY(char, InitialMessageBufferCapacity);
  75     _lines = NEW_RESOURCE_ARRAY(LogLine, InitialLineCapacity);
  76   }
  77   _message_buffer_capacity = InitialMessageBufferCapacity;
  78   _line_capacity = InitialLineCapacity;
  79 }
  80 
  81 void LogMessageBuffer::Iterator::skip_messages_with_finer_level() {
  82   for (; _current_line_index < _message._line_count; _current_line_index++) {
  83     if (_message._lines[_current_line_index].level >= _level) {
  84       break;
  85     }
  86   }
  87 }
  88 
  89 void LogMessageBuffer::write(LogLevelType level, const char* fmt, ...) {
  90   va_list args;
  91   va_start(args, fmt);
  92   vwrite(level, fmt, args);
  93   va_end(args);
  94 };
  95 
  96 void LogMessageBuffer::vwrite(LogLevelType level, const char* fmt, va_list args) {
  97   if (_message_buffer_capacity == 0) {
  98     initialize_buffers();
  99   }
 100 
 101   if (level > _least_detailed_level) {
 102     _least_detailed_level = level;
 103   }
 104 
 105   size_t written;
 106   for (int attempts = 0; attempts < 2; attempts++) {
 107     written = 0;
 108     size_t remaining_buffer_length = _message_buffer_capacity - _message_buffer_size;
 109     char* current_buffer_position = _message_buffer + _message_buffer_size;
 110 
 111     if (_prefix_fn != NULL) {
 112       written += _prefix_fn(current_buffer_position, remaining_buffer_length);
 113       current_buffer_position += written;
 114       if (remaining_buffer_length < written) {
 115         remaining_buffer_length = 0;
 116       } else {
 117         remaining_buffer_length -= written;
 118       }
 119     }
 120 
 121     va_list copy;
 122     va_copy(copy, args);
 123     written += (size_t)os::log_vsnprintf(current_buffer_position, remaining_buffer_length, fmt, copy) + 1;
 124     va_end(copy);
 125     if (written > _message_buffer_capacity - _message_buffer_size) {
 126       assert(attempts == 0, "Second attempt should always have a sufficiently large buffer (resized to fit).");
 127       grow(_message_buffer, _message_buffer_capacity, _c_heap_allocated, _message_buffer_size + written);
 128       continue;
 129     }
 130     break;
 131   }
 132 
 133   if (_line_count == _line_capacity) {
 134     grow(_lines, _line_capacity, _c_heap_allocated);
 135   }
 136 
 137   _lines[_line_count].level = level;
 138   _lines[_line_count].message_offset = _message_buffer_size;
 139   _message_buffer_size += written;
 140   _line_count++;
 141 }
 142 
 143 #define LOG_LEVEL(level, name) \
 144 LogMessageBuffer& LogMessageBuffer::v##name(const char* fmt, va_list args) { \
 145   vwrite(LogLevel::level, fmt, args); \
 146   return *this; \
 147 } \
 148 LogMessageBuffer& LogMessageBuffer::name(const char* fmt, ...) { \
 149   va_list args; \
 150   va_start(args, fmt); \
 151   vwrite(LogLevel::level, fmt, args); \
 152   va_end(args); \
 153   return *this; \
 154 }
 155 LOG_LEVEL_LIST
 156 #undef LOG_LEVEL