Path: blob/master/src/hotspot/share/logging/logDecorations.cpp
40930 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/23#include "precompiled.hpp"24#include "jvm.h"25#include "logging/logConfiguration.hpp"26#include "logging/logDecorations.hpp"27#include "runtime/atomic.hpp"28#include "runtime/os.hpp"29#include "runtime/thread.inline.hpp"30#include "services/management.hpp"3132const char* volatile LogDecorations::_host_name = NULL;33const int LogDecorations::_pid = os::current_process_id(); // This is safe to call during dynamic initialization.3435const char* LogDecorations::host_name() {36const char* host_name = Atomic::load_acquire(&_host_name);37if (host_name == NULL) {38char buffer[1024];39if (os::get_host_name(buffer, sizeof(buffer))) {40host_name = os::strdup_check_oom(buffer);41const char* old_value = Atomic::cmpxchg(&_host_name, (const char*)NULL, host_name);42if (old_value != NULL) {43os::free((void *) host_name);44host_name = old_value;45}46}47}48return host_name;49}5051LogDecorations::LogDecorations(LogLevelType level, const LogTagSet &tagset, const LogDecorators &decorators) :52// When constructing the LogDecorations we resolve values for the requested decorators.53//54// _millis: needed for "time", "utctime", "timemillis":55_millis(56(decorators.is_decorator(LogDecorators::time_decorator) ||57decorators.is_decorator(LogDecorators::utctime_decorator) ||58decorators.is_decorator(LogDecorators::timemillis_decorator)) ? os::javaTimeMillis() : 0),59// _nanos: needed for "timenanos"60_nanos(decorators.is_decorator(LogDecorators::timenanos_decorator) ? os::javaTimeNanos() : 0),61// _elapsed_seconds: needed for "uptime", "uptimemillis", "uptimenanos"62_elapsed_seconds(63(decorators.is_decorator(LogDecorators::uptime_decorator) ||64decorators.is_decorator(LogDecorators::uptimemillis_decorator) ||65decorators.is_decorator(LogDecorators::uptimenanos_decorator)) ? os::elapsedTime() : 0),66// tid67_tid(decorators.is_decorator(LogDecorators::tid_decorator) ? os::current_thread_id() : 0),68// the rest is handed down by the caller69_level(level), _tagset(tagset)70#ifdef ASSERT71, _decorators(decorators)72#endif73{74}7576void LogDecorations::print_decoration(LogDecorators::Decorator decorator, outputStream* st) const {77assert(_decorators.is_decorator(decorator), "decorator was not part of the decorator set specified at creation.");78switch(decorator) {79#define DECORATOR(name, abbr) case LogDecorators:: name##_decorator: print_##name##_decoration(st); break;80DECORATOR_LIST81#undef DECORATOR82default: ShouldNotReachHere();83}84}8586const char* LogDecorations::decoration(LogDecorators::Decorator decorator, char* buf, size_t buflen) const {87stringStream ss(buf, buflen);88print_decoration(decorator, &ss);89return buf;90}9192void LogDecorations::print_time_decoration(outputStream* st) const {93char buf[os::iso8601_timestamp_size];94char* result = os::iso8601_time(_millis, buf, sizeof(buf), false);95st->print_raw(result ? result : "");96}9798void LogDecorations::print_utctime_decoration(outputStream* st) const {99char buf[os::iso8601_timestamp_size];100char* result = os::iso8601_time(_millis, buf, sizeof(buf), true);101st->print_raw(result ? result : "");102}103104void LogDecorations::print_uptime_decoration(outputStream* st) const {105st->print("%.3fs", _elapsed_seconds);106}107108void LogDecorations::print_timemillis_decoration(outputStream* st) const {109st->print(INT64_FORMAT "ms", (int64_t)_millis);110}111112void LogDecorations::print_uptimemillis_decoration(outputStream* st) const {113st->print(INT64_FORMAT "ms", (int64_t)(_elapsed_seconds * MILLIUNITS));114}115116void LogDecorations::print_timenanos_decoration(outputStream* st) const {117st->print(INT64_FORMAT "ns", (int64_t)_nanos);118}119120void LogDecorations::print_uptimenanos_decoration(outputStream* st) const {121st->print(INT64_FORMAT "ns", (int64_t)(_elapsed_seconds * NANOUNITS));122}123124void LogDecorations::print_pid_decoration(outputStream* st) const {125st->print("%d", _pid);126}127128void LogDecorations::print_tid_decoration(outputStream* st) const {129st->print(INTX_FORMAT, _tid);130}131132void LogDecorations::print_level_decoration(outputStream* st) const {133st->print_raw(LogLevel::name(_level));134}135136void LogDecorations::print_tags_decoration(outputStream* st) const {137_tagset.label(st);138}139140void LogDecorations::print_hostname_decoration(outputStream* st) const {141st->print_raw(host_name());142}143144145