1 /* 2 * Copyright (c) 2015, 2017, 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/logDecorators.hpp" 27 #include "logging/logDecorations.hpp" 28 #include "logging/logFileStreamOutput.hpp" 29 #include "logging/logMessageBuffer.hpp" 30 #include "memory/allocation.inline.hpp" 31 32 static bool initialized; 33 static union { 34 char stdoutmem[sizeof(LogStdoutOutput)]; 35 jlong dummy; 36 } aligned_stdoutmem; 37 static union { 38 char stderrmem[sizeof(LogStderrOutput)]; 39 jlong dummy; 40 } aligned_stderrmem; 41 42 LogStdoutOutput &StdoutLog = reinterpret_cast<LogStdoutOutput&>(aligned_stdoutmem.stdoutmem); 43 LogStderrOutput &StderrLog = reinterpret_cast<LogStderrOutput&>(aligned_stderrmem.stderrmem); 44 45 LogFileStreamInitializer::LogFileStreamInitializer() { 46 if (!initialized) { 47 ::new (&StdoutLog) LogStdoutOutput(); 48 ::new (&StderrLog) LogStderrOutput(); 49 initialized = true; 50 } 51 } 52 53 int LogFileStreamOutput::write_decorations(const LogDecorations& decorations) { 54 int total_written = 0; 55 56 for (uint i = 0; i < LogDecorators::Count; i++) { 57 LogDecorators::Decorator decorator = static_cast<LogDecorators::Decorator>(i); 58 if (!_decorators.is_decorator(decorator)) { 59 continue; 60 } 61 62 int written = jio_fprintf(_stream, "[%-*s]", 63 _decorator_padding[decorator], 64 decorations.decoration(decorator)); 65 if (written <= 0) { 66 return -1; 67 } else if (static_cast<size_t>(written - 2) > _decorator_padding[decorator]) { 68 _decorator_padding[decorator] = written - 2; 69 } 70 total_written += written; 71 } 72 return total_written; 73 } 74 75 int LogFileStreamOutput::write(const LogDecorations& decorations, const char* msg) { 76 const bool use_decorations = !_decorators.is_empty(); 77 78 int written = 0; 79 os::flockfile(_stream); 80 if (use_decorations) { 81 written += write_decorations(decorations); 82 written += jio_fprintf(_stream, " "); 83 } 84 written += jio_fprintf(_stream, "%s\n", msg); 85 fflush(_stream); 86 os::funlockfile(_stream); 87 88 return written; 89 } 90 91 int LogFileStreamOutput::write(LogMessageBuffer::Iterator msg_iterator) { 92 const bool use_decorations = !_decorators.is_empty(); 93 94 int written = 0; 95 os::flockfile(_stream); 96 for (; !msg_iterator.is_at_end(); msg_iterator++) { 97 if (use_decorations) { 98 written += write_decorations(msg_iterator.decorations()); 99 written += jio_fprintf(_stream, " "); 100 } 101 written += jio_fprintf(_stream, "%s\n", msg_iterator.message()); 102 } 103 fflush(_stream); 104 os::funlockfile(_stream); 105 106 return written; 107 }