1 /*
  2  * Copyright (c) 2015, 2021, 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 #include "utilities/defaultStream.hpp"
 32 
 33 const char* const LogFileStreamOutput::FoldMultilinesOptionKey = "foldmultilines";
 34 
 35 static bool initialized;
 36 static union {
 37   char stdoutmem[sizeof(LogStdoutOutput)];
 38   jlong dummy;
 39 } aligned_stdoutmem;
 40 static union {
 41   char stderrmem[sizeof(LogStderrOutput)];
 42   jlong dummy;
 43 } aligned_stderrmem;
 44 
 45 LogStdoutOutput &StdoutLog = reinterpret_cast<LogStdoutOutput&>(aligned_stdoutmem.stdoutmem);
 46 LogStderrOutput &StderrLog = reinterpret_cast<LogStderrOutput&>(aligned_stderrmem.stderrmem);
 47 
 48 LogFileStreamInitializer::LogFileStreamInitializer() {
 49   if (!initialized) {
 50     ::new (&StdoutLog) LogStdoutOutput();
 51     ::new (&StderrLog) LogStderrOutput();
 52     initialized = true;
 53   }
 54 }
 55 
 56 int LogFileStreamOutput::write_decorations(const LogDecorations& decorations) {
 57   int total_written = 0;
 58   char buf[LogDecorations::max_decoration_size + 1];
 59 
 60   for (uint i = 0; i < LogDecorators::Count; i++) {
 61     LogDecorators::Decorator decorator = static_cast<LogDecorators::Decorator>(i);
 62     if (!_decorators.is_decorator(decorator)) {
 63       continue;
 64     }
 65 
 66     int written = jio_fprintf(_stream, "[%-*s]",
 67                               _decorator_padding[decorator],
 68                               decorations.decoration(decorator, buf, sizeof(buf)));
 69     if (written <= 0) {
 70       return -1;
 71     } else if (static_cast<size_t>(written - 2) > _decorator_padding[decorator]) {
 72       _decorator_padding[decorator] = written - 2;
 73     }
 74     total_written += written;
 75   }
 76   return total_written;
 77 }
 78 
 79 bool LogFileStreamOutput::flush() {
 80   bool result = true;
 81   if (fflush(_stream) != 0) {
 82     if (!_write_error_is_shown) {
 83       jio_fprintf(defaultStream::error_stream(),
 84                   "Could not flush log: %s (%s (%d))\n", name(), os::strerror(errno), errno);
 85       jio_fprintf(_stream, "\nERROR: Could not flush log (%d)\n", errno);
 86       _write_error_is_shown = true;
 87     }
 88     result = false;
 89   }
 90   return result;
 91 }
 92 
 93 #define WRITE_LOG_WITH_RESULT_CHECK(op, total)                \
 94 {                                                             \
 95   int result = op;                                            \
 96   if (result < 0) {                                           \
 97     if (!_write_error_is_shown) {                             \
 98       jio_fprintf(defaultStream::error_stream(),              \
 99                   "Could not write log: %s\n", name());       \
100       jio_fprintf(_stream, "\nERROR: Could not write log\n"); \
101       _write_error_is_shown = true;                           \
102       return -1;                                              \
103     }                                                         \
104   }                                                           \
105   total += result;                                            \
106 }
107 
108 int LogFileStreamOutput::write_internal(const char* msg) {
109   int written = 0;
110   if (!_fold_multilines) {
111     WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, "%s\n", msg), written);
112   } else {
113     char *dupstr = os::strdup_check_oom(msg, mtLogging);
114     char *cur = dupstr;
115     char *next;
116     do {
117       next = strpbrk(cur, "\n\\");
118       if (next == NULL) {
119         WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, "%s\n", cur), written);
120       } else {
121         const char *found = (*next == '\n') ? "\\n" : "\\\\";
122         *next = '\0';
123         WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, "%s%s", cur, found), written);
124         cur = next + 1;
125       }
126     } while (next != NULL);
127     os::free(dupstr);
128   }
129   return written;
130 }
131 
132 int LogFileStreamOutput::write(const LogDecorations& decorations, const char* msg) {
133   const bool use_decorations = !_decorators.is_empty();
134 
135   int written = 0;
136   FileLocker flocker(_stream);
137   if (use_decorations) {
138     WRITE_LOG_WITH_RESULT_CHECK(write_decorations(decorations), written);
139     WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, " "), written);
140   }
141   written += write_internal(msg);
142 
143   return flush() ? written : -1;
144 }
145 
146 int LogFileStreamOutput::write(LogMessageBuffer::Iterator msg_iterator) {
147   const bool use_decorations = !_decorators.is_empty();
148 
149   int written = 0;
150   FileLocker flocker(_stream);
151   for (; !msg_iterator.is_at_end(); msg_iterator++) {
152     if (use_decorations) {
153       WRITE_LOG_WITH_RESULT_CHECK(write_decorations(msg_iterator.decorations()), written);
154       WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, " "), written);
155     }
156     written += write_internal(msg_iterator.message());
157   }
158 
159   return flush() ? written : -1;
160 }