jdk

Форк
0
/
logMessageBuffer.cpp 
152 строки · 5.0 Кб
1
/*
2
 * Copyright (c) 2016, 2023, 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/javaThread.hpp"
28

29
template <typename T>
30
static void grow(T*& buffer, size_t& capacity, 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
  buffer = REALLOC_C_HEAP_ARRAY(T, buffer, new_size, mtLogging);
36
  capacity = new_size;
37
}
38

39
LogMessageBuffer::LogMessageBuffer() : _message_buffer_size(0),
40
                                       _message_buffer_capacity(0),
41
                                       _message_buffer(nullptr),
42
                                       _line_count(0),
43
                                       _line_capacity(0),
44
                                       _lines(nullptr),
45
                                       _allocated(false),
46
                                       _least_detailed_level(LogLevel::Off),
47
                                       _prefix_fn(nullptr) {
48
}
49

50
LogMessageBuffer::~LogMessageBuffer() {
51
  if (_allocated) {
52
    FREE_C_HEAP_ARRAY(char, _message_buffer);
53
    FREE_C_HEAP_ARRAY(LogLine, _lines);
54
  }
55
}
56

57
void LogMessageBuffer::reset() {
58
  _message_buffer_size = 0;
59
  _line_count = 0;
60
}
61

62
void LogMessageBuffer::initialize_buffers() {
63
  assert(!_allocated, "buffer already initialized/allocated");
64
  _allocated = true;
65
  _message_buffer = NEW_C_HEAP_ARRAY(char, InitialMessageBufferCapacity, mtLogging);
66
  _lines = NEW_C_HEAP_ARRAY(LogLine, InitialLineCapacity, mtLogging);
67
  _message_buffer_capacity = InitialMessageBufferCapacity;
68
  _line_capacity = InitialLineCapacity;
69
}
70

71
void LogMessageBuffer::Iterator::skip_messages_with_finer_level() {
72
  for (; _current_line_index < _message._line_count; _current_line_index++) {
73
    if (_message._lines[_current_line_index].level >= _level) {
74
      break;
75
    }
76
  }
77
}
78

79
void LogMessageBuffer::write(LogLevelType level, const char* fmt, ...) {
80
  va_list args;
81
  va_start(args, fmt);
82
  vwrite(level, fmt, args);
83
  va_end(args);
84
};
85

86
void LogMessageBuffer::vwrite(LogLevelType level, const char* fmt, va_list args) {
87
  if (!_allocated) {
88
    initialize_buffers();
89
  }
90

91
  if (level > _least_detailed_level) {
92
    _least_detailed_level = level;
93
  }
94

95
  size_t written;
96
  for (int attempts = 0; attempts < 2; attempts++) {
97
    written = 0;
98
    size_t remaining_buffer_length = _message_buffer_capacity - _message_buffer_size;
99
    char* current_buffer_position = _message_buffer + _message_buffer_size;
100

101
    if (_prefix_fn != nullptr) {
102
      written += _prefix_fn(current_buffer_position, remaining_buffer_length);
103
      current_buffer_position += written;
104
      if (remaining_buffer_length < written) {
105
        remaining_buffer_length = 0;
106
      } else {
107
        remaining_buffer_length -= written;
108
      }
109
    }
110

111
    va_list copy;
112
    va_copy(copy, args);
113
    int ret = os::vsnprintf(current_buffer_position, remaining_buffer_length, fmt, copy);
114
    va_end(copy);
115
    assert(ret >= 0, "Log message buffer issue");
116
    if (ret < 0) {
117
      this->write(level, "%s", "Log message buffer issue");
118
      return;
119
    }
120
    written += (size_t)ret + 1;
121
    if (written > _message_buffer_capacity - _message_buffer_size) {
122
      assert(attempts == 0, "Second attempt should always have a sufficiently large buffer (resized to fit).");
123
      grow(_message_buffer, _message_buffer_capacity, _message_buffer_size + written);
124
      continue;
125
    }
126
    break;
127
  }
128

129
  if (_line_count == _line_capacity) {
130
    grow(_lines, _line_capacity);
131
  }
132

133
  _lines[_line_count].level = level;
134
  _lines[_line_count].message_offset = _message_buffer_size;
135
  _message_buffer_size += written;
136
  _line_count++;
137
}
138

139
#define LOG_LEVEL(level, name) \
140
LogMessageBuffer& LogMessageBuffer::v##name(const char* fmt, va_list args) { \
141
  vwrite(LogLevel::level, fmt, args); \
142
  return *this; \
143
} \
144
LogMessageBuffer& LogMessageBuffer::name(const char* fmt, ...) { \
145
  va_list args; \
146
  va_start(args, fmt); \
147
  vwrite(LogLevel::level, fmt, args); \
148
  va_end(args); \
149
  return *this; \
150
}
151
LOG_LEVEL_LIST
152
#undef LOG_LEVEL
153

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.