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.
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.
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).
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.
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
24
#include "precompiled.hpp"
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"
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.
36
const char* LogDecorations::host_name() {
37
const char* host_name = Atomic::load_acquire(&_host_name);
38
if (host_name == nullptr) {
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;
52
LogDecorations::LogDecorations(LogLevelType level, const LogTagSet &tagset, const LogDecorators &decorators) :
53
// When constructing the LogDecorations we resolve values for the requested decorators.
55
// _millis: needed for "time", "utctime", "timemillis":
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"
64
(decorators.is_decorator(LogDecorators::uptime_decorator) ||
65
decorators.is_decorator(LogDecorators::uptimemillis_decorator) ||
66
decorators.is_decorator(LogDecorators::uptimenanos_decorator)) ? os::elapsedTime() : 0),
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)
72
, _decorators(decorators)
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.");
80
#define DECORATOR(name, abbr) case LogDecorators:: name##_decorator: print_##name##_decoration(st); break;
83
default: ShouldNotReachHere();
87
const char* LogDecorations::decoration(LogDecorators::Decorator decorator, char* buf, size_t buflen) const {
88
stringStream ss(buf, buflen);
89
print_decoration(decorator, &ss);
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 : "");
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 : "");
105
void LogDecorations::print_uptime_decoration(outputStream* st) const {
106
st->print("%.3fs", _elapsed_seconds);
109
void LogDecorations::print_timemillis_decoration(outputStream* st) const {
110
st->print(INT64_FORMAT "ms", (int64_t)_millis);
113
void LogDecorations::print_uptimemillis_decoration(outputStream* st) const {
114
st->print(INT64_FORMAT "ms", (int64_t)(_elapsed_seconds * MILLIUNITS));
117
void LogDecorations::print_timenanos_decoration(outputStream* st) const {
118
st->print(INT64_FORMAT "ns", (int64_t)_nanos);
121
void LogDecorations::print_uptimenanos_decoration(outputStream* st) const {
122
st->print(INT64_FORMAT "ns", (int64_t)(_elapsed_seconds * NANOUNITS));
125
void LogDecorations::print_pid_decoration(outputStream* st) const {
126
st->print("%d", _pid);
129
void LogDecorations::print_tid_decoration(outputStream* st) const {
130
st->print(INTX_FORMAT, _tid);
133
void LogDecorations::print_level_decoration(outputStream* st) const {
134
st->print_raw(LogLevel::name(_level));
137
void LogDecorations::print_tags_decoration(outputStream* st) const {
141
void LogDecorations::print_hostname_decoration(outputStream* st) const {
142
st->print_raw(host_name());