1 /* |
1 /* |
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
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. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
23 */ |
23 */ |
24 #include "precompiled.hpp" |
24 #include "precompiled.hpp" |
25 #include "jvm.h" |
25 #include "jvm.h" |
26 #include "logging/logConfiguration.hpp" |
26 #include "logging/logConfiguration.hpp" |
27 #include "logging/logDecorations.hpp" |
27 #include "logging/logDecorations.hpp" |
|
28 #include "runtime/atomic.hpp" |
|
29 #include "runtime/orderAccess.hpp" |
28 #include "runtime/os.inline.hpp" |
30 #include "runtime/os.inline.hpp" |
29 #include "runtime/thread.inline.hpp" |
31 #include "runtime/thread.inline.hpp" |
30 #include "services/management.hpp" |
32 #include "services/management.hpp" |
31 |
33 |
32 jlong LogDecorations::_vm_start_time_millis = 0; |
34 jlong LogDecorations::_vm_start_time_millis = 0; |
33 const char* LogDecorations::_host_name = ""; |
35 const char* volatile LogDecorations::_host_name = NULL; |
34 |
36 |
35 LogDecorations::LogDecorations(LogLevelType level, const LogTagSet &tagset, const LogDecorators &decorators) |
37 LogDecorations::LogDecorations(LogLevelType level, const LogTagSet &tagset, const LogDecorators &decorators) |
36 : _level(level), _tagset(tagset), _millis(-1) { |
38 : _level(level), _tagset(tagset), _millis(-1) { |
37 create_decorations(decorators); |
39 create_decorations(decorators); |
38 } |
40 } |
39 |
41 |
40 void LogDecorations::initialize(jlong vm_start_time) { |
42 void LogDecorations::initialize(jlong vm_start_time) { |
41 char buffer[1024]; |
43 _vm_start_time_millis = vm_start_time; |
42 if (os::get_host_name(buffer, sizeof(buffer))){ |
44 } |
43 _host_name = os::strdup_check_oom(buffer); |
45 |
|
46 const char* LogDecorations::host_name() { |
|
47 const char* host_name = OrderAccess::load_acquire(&_host_name); |
|
48 if (host_name == NULL) { |
|
49 char buffer[1024]; |
|
50 if (os::get_host_name(buffer, sizeof(buffer))) { |
|
51 host_name = os::strdup_check_oom(buffer); |
|
52 const char* old_value = Atomic::cmpxchg(host_name, &_host_name, (const char*)NULL); |
|
53 if (old_value != NULL) { |
|
54 os::free((void *) host_name); |
|
55 host_name = old_value; |
|
56 } |
|
57 } |
44 } |
58 } |
45 _vm_start_time_millis = vm_start_time; |
59 return host_name; |
46 } |
60 } |
47 |
61 |
48 void LogDecorations::create_decorations(const LogDecorators &decorators) { |
62 void LogDecorations::create_decorations(const LogDecorators &decorators) { |
49 char* position = _decorations_buffer; |
63 char* position = _decorations_buffer; |
50 #define DECORATOR(full_name, abbr) \ |
64 #define DECORATOR(full_name, abbr) \ |
126 int written = _tagset.label(pos, DecorationsBufferSize - (pos - _decorations_buffer)); |
140 int written = _tagset.label(pos, DecorationsBufferSize - (pos - _decorations_buffer)); |
127 ASSERT_AND_RETURN(written, pos) |
141 ASSERT_AND_RETURN(written, pos) |
128 } |
142 } |
129 |
143 |
130 char* LogDecorations::create_hostname_decoration(char* pos) { |
144 char* LogDecorations::create_hostname_decoration(char* pos) { |
131 int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), "%s", _host_name); |
145 int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), "%s", host_name()); |
132 ASSERT_AND_RETURN(written, pos) |
146 ASSERT_AND_RETURN(written, pos) |
133 } |
147 } |
134 |
148 |