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