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 class FileLocker : public StackObj {
 80 private:
 81   FILE *_file;
 82 
 83 public:
 84   FileLocker(FILE *file) : _file(file) {
 85     os::flockfile(_file);
 86   }
 87 
 88   ~FileLocker() {
 89     os::funlockfile(_file);
 90   }
 91 };
 92 
 93 bool LogFileStreamOutput::flush() {
 94   bool result = true;
 95   if (fflush(_stream) != 0) {
 96     if (!_write_error_is_shown) {
 97       jio_fprintf(defaultStream::error_stream(),
 98                   "Could not flush log: %s (%s (%d))\n", name(), os::strerror(errno), errno);
 99       jio_fprintf(_stream, "\nERROR: Could not flush log (%d)\n", errno);
100       _write_error_is_shown = true;
101     }
102     result = false;
103   }
104   return result;
105 }
106 
107 #define WRITE_LOG_WITH_RESULT_CHECK(op, total)                \
108 {                                                             \
109   int result = op;                                            \
110   if (result < 0) {                                           \
111     if (!_write_error_is_shown) {                             \
112       jio_fprintf(defaultStream::error_stream(),              \
113                   "Could not write log: %s\n", name());       \
114       jio_fprintf(_stream, "\nERROR: Could not write log\n"); \
115       _write_error_is_shown = true;                           \
116       return -1;                                              \
117     }                                                         \
118   }                                                           \
119   total += result;                                            \
120 }
121 
122 int LogFileStreamOutput::write_internal(const char* msg) {
123   int written = 0;
124   if (!_fold_multilines) {
125     WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, "%s\n", msg), written);
126   } else {
127     char *dupstr = os::strdup_check_oom(msg, mtLogging);
128     char *cur = dupstr;
129     char *next;
130     do {
131       next = strpbrk(cur, "\n\\");
132       if (next == NULL) {
133         WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, "%s\n", cur), written);
134       } else {
135         const char *found = (*next == '\n') ? "\\n" : "\\\\";
136         *next = '\0';
137         WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, "%s%s", cur, found), written);
138         cur = next + 1;
139       }
140     } while (next != NULL);
141     os::free(dupstr);
142   }
143   return written;
144 }
145 
146 int LogFileStreamOutput::write(const LogDecorations& decorations, const char* msg) {
147   const bool use_decorations = !_decorators.is_empty();
148 
149   int written = 0;
150   FileLocker flocker(_stream);
151   if (use_decorations) {
152     WRITE_LOG_WITH_RESULT_CHECK(write_decorations(decorations), written);
153     WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, " "), written);
154   }
155   written += write_internal(msg);
156 
157   return flush() ? written : -1;
158 }
159 
160 int LogFileStreamOutput::write(LogMessageBuffer::Iterator msg_iterator) {
161   const bool use_decorations = !_decorators.is_empty();
162 
163   int written = 0;
164   FileLocker flocker(_stream);
165   for (; !msg_iterator.is_at_end(); msg_iterator++) {
166     if (use_decorations) {
167       WRITE_LOG_WITH_RESULT_CHECK(write_decorations(msg_iterator.decorations()), written);
168       WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, " "), written);
169     }
170     written += write_internal(msg_iterator.message());
171   }
172 
173   return flush() ? written : -1;
174 }