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