jdk

Форк
0
/
logDecorations.cpp 
143 строки · 5.3 Кб
1
/*
2
 * Copyright (c) 2015, 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 "jvm.h"
26
#include "logging/logConfiguration.hpp"
27
#include "logging/logDecorations.hpp"
28
#include "runtime/atomic.hpp"
29
#include "runtime/javaThread.hpp"
30
#include "runtime/os.hpp"
31
#include "services/management.hpp"
32

33
const char* volatile LogDecorations::_host_name = nullptr;
34
const int LogDecorations::_pid = os::current_process_id(); // This is safe to call during dynamic initialization.
35

36
const char* LogDecorations::host_name() {
37
  const char* host_name = Atomic::load_acquire(&_host_name);
38
  if (host_name == nullptr) {
39
    char buffer[1024];
40
    if (os::get_host_name(buffer, sizeof(buffer))) {
41
      host_name = os::strdup_check_oom(buffer);
42
      const char* old_value = Atomic::cmpxchg(&_host_name, (const char*)nullptr, host_name);
43
      if (old_value != nullptr) {
44
        os::free((void *) host_name);
45
        host_name = old_value;
46
      }
47
    }
48
  }
49
  return host_name;
50
}
51

52
LogDecorations::LogDecorations(LogLevelType level, const LogTagSet &tagset, const LogDecorators &decorators) :
53
  // When constructing the LogDecorations we resolve values for the requested decorators.
54
  //
55
  // _millis: needed for "time", "utctime", "timemillis":
56
  _millis(
57
      (decorators.is_decorator(LogDecorators::time_decorator) ||
58
       decorators.is_decorator(LogDecorators::utctime_decorator) ||
59
       decorators.is_decorator(LogDecorators::timemillis_decorator)) ? os::javaTimeMillis() : 0),
60
  // _nanos: needed for "timenanos"
61
  _nanos(decorators.is_decorator(LogDecorators::timenanos_decorator) ? os::javaTimeNanos() : 0),
62
  // _elapsed_seconds: needed for "uptime", "uptimemillis", "uptimenanos"
63
  _elapsed_seconds(
64
      (decorators.is_decorator(LogDecorators::uptime_decorator) ||
65
       decorators.is_decorator(LogDecorators::uptimemillis_decorator) ||
66
       decorators.is_decorator(LogDecorators::uptimenanos_decorator)) ? os::elapsedTime() : 0),
67
  // tid
68
  _tid(decorators.is_decorator(LogDecorators::tid_decorator) ? os::current_thread_id() : 0),
69
  // the rest is handed down by the caller
70
  _level(level), _tagset(tagset)
71
#ifdef ASSERT
72
  , _decorators(decorators)
73
#endif
74
{
75
}
76

77
void LogDecorations::print_decoration(LogDecorators::Decorator decorator, outputStream* st) const {
78
  assert(_decorators.is_decorator(decorator), "decorator was not part of the decorator set specified at creation.");
79
  switch(decorator) {
80
#define DECORATOR(name, abbr) case LogDecorators:: name##_decorator: print_##name##_decoration(st); break;
81
  DECORATOR_LIST
82
#undef DECORATOR
83
    default: ShouldNotReachHere();
84
  }
85
}
86

87
const char* LogDecorations::decoration(LogDecorators::Decorator decorator, char* buf, size_t buflen) const {
88
  stringStream ss(buf, buflen);
89
  print_decoration(decorator, &ss);
90
  return buf;
91
}
92

93
void LogDecorations::print_time_decoration(outputStream* st) const {
94
  char buf[os::iso8601_timestamp_size];
95
  char* result = os::iso8601_time(_millis, buf, sizeof(buf), false);
96
  st->print_raw(result ? result : "");
97
}
98

99
void LogDecorations::print_utctime_decoration(outputStream* st) const {
100
  char buf[os::iso8601_timestamp_size];
101
  char* result = os::iso8601_time(_millis, buf, sizeof(buf), true);
102
  st->print_raw(result ? result : "");
103
}
104

105
void LogDecorations::print_uptime_decoration(outputStream* st) const {
106
  st->print("%.3fs", _elapsed_seconds);
107
}
108

109
void LogDecorations::print_timemillis_decoration(outputStream* st) const {
110
  st->print(INT64_FORMAT "ms", (int64_t)_millis);
111
}
112

113
void LogDecorations::print_uptimemillis_decoration(outputStream* st) const {
114
  st->print(INT64_FORMAT "ms", (int64_t)(_elapsed_seconds * MILLIUNITS));
115
}
116

117
void LogDecorations::print_timenanos_decoration(outputStream* st) const {
118
  st->print(INT64_FORMAT "ns", (int64_t)_nanos);
119
}
120

121
void LogDecorations::print_uptimenanos_decoration(outputStream* st) const {
122
  st->print(INT64_FORMAT "ns", (int64_t)(_elapsed_seconds * NANOUNITS));
123
}
124

125
void LogDecorations::print_pid_decoration(outputStream* st) const {
126
  st->print("%d", _pid);
127
}
128

129
void LogDecorations::print_tid_decoration(outputStream* st) const {
130
  st->print(INTX_FORMAT, _tid);
131
}
132

133
void LogDecorations::print_level_decoration(outputStream* st) const {
134
  st->print_raw(LogLevel::name(_level));
135
}
136

137
void LogDecorations::print_tags_decoration(outputStream* st) const {
138
  _tagset.label(st);
139
}
140

141
void LogDecorations::print_hostname_decoration(outputStream* st) const {
142
  st->print_raw(host_name());
143
}
144

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

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

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

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