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