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