1 /* 2 * Copyright (c) 2015, 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 "logging/logConfiguration.hpp" 26 #include "logging/logDecorations.hpp" 27 #include "runtime/os.inline.hpp" 28 #include "runtime/thread.inline.hpp" 29 #include "services/management.hpp" 30 31 jlong LogDecorations::_vm_start_time_millis = 0; 32 33 LogDecorations::LogDecorations(LogLevelType level, const LogTagSet &tagset, const LogDecorators &decorators) 34 : _level(level), _tagset(tagset), _millis(-1) { 35 create_decorations(decorators); 36 } 37 38 void LogDecorations::create_decorations(const LogDecorators &decorators) { 39 char* position = _decorations_buffer; 40 #define DECORATOR(full_name, abbr) \ 41 if (decorators.is_decorator(LogDecorators::full_name##_decorator)) { \ 42 _decoration_offset[LogDecorators::full_name##_decorator] = position; \ 43 position = create_##full_name##_decoration(position) + 1; \ 44 } 45 DECORATOR_LIST 46 #undef DECORATOR 47 } 48 49 jlong LogDecorations::java_millis() { 50 if (_millis < 0) { 51 _millis = os::javaTimeMillis(); 52 } 53 return _millis; 54 } 55 56 #define ASSERT_AND_RETURN(written, pos) \ 57 assert(written >= 0, "Decorations buffer overflow"); \ 58 return pos + written; 59 60 char* LogDecorations::create_time_decoration(char* pos) { 61 char* buf = os::iso8601_time(pos, 29); 62 int written = buf == NULL ? -1 : 29; 63 ASSERT_AND_RETURN(written, pos) 64 } 65 66 char * LogDecorations::create_uptime_decoration(char* pos) { 67 int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), "%.3fs", os::elapsedTime()); 68 ASSERT_AND_RETURN(written, pos) 69 } 70 71 char * LogDecorations::create_timemillis_decoration(char* pos) { 72 int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), INT64_FORMAT "ms", java_millis()); 73 ASSERT_AND_RETURN(written, pos) 74 } 75 76 char * LogDecorations::create_uptimemillis_decoration(char* pos) { 77 int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), 78 INT64_FORMAT " ms", java_millis() - _vm_start_time_millis); 79 ASSERT_AND_RETURN(written, pos) 80 } 81 82 char * LogDecorations::create_timenanos_decoration(char* pos) { 83 int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), INT64_FORMAT "ns", os::javaTimeNanos()); 84 ASSERT_AND_RETURN(written, pos) 85 } 86 87 char * LogDecorations::create_uptimenanos_decoration(char* pos) { 88 int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), INT64_FORMAT "ns", os::elapsed_counter()); 89 ASSERT_AND_RETURN(written, pos) 90 } 91 92 char * LogDecorations::create_pid_decoration(char* pos) { 93 int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), "%d", os::current_process_id()); 94 ASSERT_AND_RETURN(written, pos) 95 } 96 97 char * LogDecorations::create_tid_decoration(char* pos) { 98 int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), 99 INTX_FORMAT, Thread::current()->osthread()->thread_id()); 100 ASSERT_AND_RETURN(written, pos) 101 } 102 103 char* LogDecorations::create_level_decoration(char* pos) { 104 int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), "%s", LogLevel::name(_level)); 105 ASSERT_AND_RETURN(written, pos) 106 } 107 108 char* LogDecorations::create_tags_decoration(char* pos) { 109 int written = _tagset.label(pos, DecorationsBufferSize - (pos - _decorations_buffer)); 110 ASSERT_AND_RETURN(written, pos) 111 }