Path: blob/master/src/hotspot/share/logging/logFileStreamOutput.cpp
40930 views
/*1* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/23#include "precompiled.hpp"24#include "jvm.h"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#include "utilities/defaultStream.hpp"3132static bool initialized;33static union {34char stdoutmem[sizeof(LogStdoutOutput)];35jlong dummy;36} aligned_stdoutmem;37static union {38char stderrmem[sizeof(LogStderrOutput)];39jlong dummy;40} aligned_stderrmem;4142LogStdoutOutput &StdoutLog = reinterpret_cast<LogStdoutOutput&>(aligned_stdoutmem.stdoutmem);43LogStderrOutput &StderrLog = reinterpret_cast<LogStderrOutput&>(aligned_stderrmem.stderrmem);4445LogFileStreamInitializer::LogFileStreamInitializer() {46if (!initialized) {47::new (&StdoutLog) LogStdoutOutput();48::new (&StderrLog) LogStderrOutput();49initialized = true;50}51}5253int LogFileStreamOutput::write_decorations(const LogDecorations& decorations) {54int total_written = 0;55char buf[LogDecorations::max_decoration_size + 1];5657for (uint i = 0; i < LogDecorators::Count; i++) {58LogDecorators::Decorator decorator = static_cast<LogDecorators::Decorator>(i);59if (!_decorators.is_decorator(decorator)) {60continue;61}6263int written = jio_fprintf(_stream, "[%-*s]",64_decorator_padding[decorator],65decorations.decoration(decorator, buf, sizeof(buf)));66if (written <= 0) {67return -1;68} else if (static_cast<size_t>(written - 2) > _decorator_padding[decorator]) {69_decorator_padding[decorator] = written - 2;70}71total_written += written;72}73return total_written;74}7576class FileLocker : public StackObj {77private:78FILE *_file;7980public:81FileLocker(FILE *file) : _file(file) {82os::flockfile(_file);83}8485~FileLocker() {86os::funlockfile(_file);87}88};8990bool LogFileStreamOutput::flush() {91bool result = true;92if (fflush(_stream) != 0) {93if (!_write_error_is_shown) {94jio_fprintf(defaultStream::error_stream(),95"Could not flush log: %s (%s (%d))\n", name(), os::strerror(errno), errno);96jio_fprintf(_stream, "\nERROR: Could not flush log (%d)\n", errno);97_write_error_is_shown = true;98}99result = false;100}101return result;102}103104#define WRITE_LOG_WITH_RESULT_CHECK(op, total) \105{ \106int result = op; \107if (result < 0) { \108if (!_write_error_is_shown) { \109jio_fprintf(defaultStream::error_stream(), \110"Could not write log: %s\n", name()); \111jio_fprintf(_stream, "\nERROR: Could not write log\n"); \112_write_error_is_shown = true; \113return -1; \114} \115} \116total += result; \117}118119int LogFileStreamOutput::write(const LogDecorations& decorations, const char* msg) {120const bool use_decorations = !_decorators.is_empty();121122int written = 0;123FileLocker flocker(_stream);124if (use_decorations) {125WRITE_LOG_WITH_RESULT_CHECK(write_decorations(decorations), written);126WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, " "), written);127}128WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, "%s\n", msg), written);129130return flush() ? written : -1;131}132133int LogFileStreamOutput::write(LogMessageBuffer::Iterator msg_iterator) {134const bool use_decorations = !_decorators.is_empty();135136int written = 0;137FileLocker flocker(_stream);138for (; !msg_iterator.is_at_end(); msg_iterator++) {139if (use_decorations) {140WRITE_LOG_WITH_RESULT_CHECK(write_decorations(msg_iterator.decorations()), written);141WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, " "), written);142}143WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, "%s\n", msg_iterator.message()), written);144}145146return flush() ? written : -1;147}148149150