1 /*
   2  * Copyright (c) 2015, 2019, 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/os.inline.hpp"
  30 #include "runtime/thread.inline.hpp"
  31 #include "services/management.hpp"
  32 
  33 jlong LogDecorations::_vm_start_time_millis = 0;
  34 const char* volatile LogDecorations::_host_name = NULL;
  35 
  36 LogDecorations::LogDecorations(LogLevelType level, const LogTagSet &tagset, const LogDecorators &decorators)
  37     : _level(level), _tagset(tagset), _millis(-1) {
  38   create_decorations(decorators);
  39 }
  40 
  41 void LogDecorations::initialize(jlong vm_start_time) {
  42   _vm_start_time_millis = vm_start_time;
  43 }
  44 
  45 const char* LogDecorations::host_name() {
  46   const char* host_name = Atomic::load_acquire(&_host_name);
  47   if (host_name == NULL) {
  48     char buffer[1024];
  49     if (os::get_host_name(buffer, sizeof(buffer))) {
  50       host_name = os::strdup_check_oom(buffer);
  51       const char* old_value = Atomic::cmpxchg(&_host_name, (const char*)NULL, host_name);
  52       if (old_value != NULL) {
  53         os::free((void *) host_name);
  54         host_name = old_value;
  55       }
  56     }
  57   }
  58   return host_name;
  59 }
  60 
  61 void LogDecorations::create_decorations(const LogDecorators &decorators) {
  62   char* position = _decorations_buffer;
  63   #define DECORATOR(full_name, abbr) \
  64   if (decorators.is_decorator(LogDecorators::full_name##_decorator)) { \
  65     _decoration_offset[LogDecorators::full_name##_decorator] = position; \
  66     position = create_##full_name##_decoration(position) + 1; \
  67   } else { \
  68     _decoration_offset[LogDecorators::full_name##_decorator] = NULL; \
  69   }
  70   DECORATOR_LIST
  71 #undef DECORATOR
  72 }
  73 
  74 jlong LogDecorations::java_millis() {
  75   if (_millis < 0) {
  76     _millis = os::javaTimeMillis();
  77   }
  78   return _millis;
  79 }
  80 
  81 #define ASSERT_AND_RETURN(written, pos) \
  82     assert(written >= 0, "Decorations buffer overflow"); \
  83     return pos + written;
  84 
  85 char* LogDecorations::create_time_decoration(char* pos) {
  86   char* buf = os::iso8601_time(pos, 29);
  87   int written = buf == NULL ? -1 : 29;
  88   ASSERT_AND_RETURN(written, pos)
  89 }
  90 
  91 char* LogDecorations::create_utctime_decoration(char* pos) {
  92   char* buf = os::iso8601_time(pos, 29, true);
  93   int written = buf == NULL ? -1 : 29;
  94   ASSERT_AND_RETURN(written, pos)
  95 }
  96 
  97 char * LogDecorations::create_uptime_decoration(char* pos) {
  98   int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), "%.3fs", os::elapsedTime());
  99   ASSERT_AND_RETURN(written, pos)
 100 }
 101 
 102 char * LogDecorations::create_timemillis_decoration(char* pos) {
 103   int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), INT64_FORMAT "ms", java_millis());
 104   ASSERT_AND_RETURN(written, pos)
 105 }
 106 
 107 char * LogDecorations::create_uptimemillis_decoration(char* pos) {
 108   int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer),
 109                              INT64_FORMAT "ms", java_millis() - _vm_start_time_millis);
 110   ASSERT_AND_RETURN(written, pos)
 111 }
 112 
 113 char * LogDecorations::create_timenanos_decoration(char* pos) {
 114   int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), INT64_FORMAT "ns", os::javaTimeNanos());
 115   ASSERT_AND_RETURN(written, pos)
 116 }
 117 
 118 char * LogDecorations::create_uptimenanos_decoration(char* pos) {
 119   int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), INT64_FORMAT "ns", os::elapsed_counter());
 120   ASSERT_AND_RETURN(written, pos)
 121 }
 122 
 123 char * LogDecorations::create_pid_decoration(char* pos) {
 124   int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), "%d", os::current_process_id());
 125   ASSERT_AND_RETURN(written, pos)
 126 }
 127 
 128 char * LogDecorations::create_tid_decoration(char* pos) {
 129   int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer),
 130                              INTX_FORMAT, os::current_thread_id());
 131   ASSERT_AND_RETURN(written, pos)
 132 }
 133 
 134 char* LogDecorations::create_level_decoration(char* pos) {
 135   // Avoid generating the level decoration because it may change.
 136   // The decoration() method has a special case for level decorations.
 137   return pos;
 138 }
 139 
 140 char* LogDecorations::create_tags_decoration(char* pos) {
 141   int written = _tagset.label(pos, DecorationsBufferSize - (pos - _decorations_buffer));
 142   ASSERT_AND_RETURN(written, pos)
 143 }
 144 
 145 char* LogDecorations::create_hostname_decoration(char* pos) {
 146   int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), "%s", host_name());
 147   ASSERT_AND_RETURN(written, pos)
 148 }