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 "logging/logDecorators.hpp"
26 #include "logging/logDecorations.hpp"
27 #include "logging/logFileStreamOutput.hpp"
28 #include "memory/allocation.inline.hpp"
29
30 LogStdoutOutput LogStdoutOutput::_instance;
31 LogStderrOutput LogStderrOutput::_instance;
32
33 int LogFileStreamOutput::write(const LogDecorations& decorations, const char* msg) {
34 char decoration_buf[LogDecorations::DecorationsBufferSize];
35 char* position = decoration_buf;
36 int total_written = 0;
37
38 for (uint i = 0; i < LogDecorators::Count; i++) {
39 LogDecorators::Decorator decorator = static_cast<LogDecorators::Decorator>(i);
40 if (!_decorators.is_decorator(decorator)) {
41 continue;
42 }
43 int written = jio_snprintf(position, sizeof(decoration_buf) - total_written, "[%-*s]",
44 _decorator_padding[decorator],
45 decorations.decoration(decorator));
46 if (written <= 0) {
47 return -1;
48 } else if (static_cast<size_t>(written - 2) > _decorator_padding[decorator]) {
49 _decorator_padding[decorator] = written - 2;
50 }
51 position += written;
52 total_written += written;
53 }
54
55 if (total_written == 0) {
56 total_written = jio_fprintf(_stream, "%s\n", msg);
57 } else {
58 total_written = jio_fprintf(_stream, "%s %s\n", decoration_buf, msg);
59 }
60 fflush(_stream);
61 return total_written;
62 }
|
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 "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
31 LogStdoutOutput LogStdoutOutput::_instance;
32 LogStderrOutput LogStderrOutput::_instance;
33
34 int LogFileStreamOutput::write_decorations(char* buffer,
35 size_t buffer_length,
36 const LogDecorations& decorations) {
37 int total_written = 0;
38 char* position = buffer;
39
40 for (uint i = 0; i < LogDecorators::Count; i++) {
41 LogDecorators::Decorator decorator = static_cast<LogDecorators::Decorator>(i);
42 if (!_decorators.is_decorator(decorator)) {
43 continue;
44 }
45
46 int written = jio_snprintf(position, buffer_length - total_written, "[%-*s]",
47 _decorator_padding[decorator], decorations.decoration(decorator));
48 if (written <= 0) {
49 return -1;
50 } else if (static_cast<size_t>(written - 2) > _decorator_padding[decorator]) {
51 _decorator_padding[decorator] = written - 2;
52 }
53 position += written;
54 total_written += written;
55 }
56 return total_written;
57 }
58
59 int LogFileStreamOutput::write(const LogDecorations& decorations, const char* msg) {
60 const bool use_decorations = !_decorators.is_empty();
61
62 int written;
63 if (use_decorations) {
64 char decoration_buf[LogDecorations::DecorationsBufferSize];
65 write_decorations(decoration_buf, sizeof(decoration_buf), decorations);
66 written = jio_fprintf(_stream, "%s %s\n", decoration_buf, msg);
67 } else {
68 written = jio_fprintf(_stream, "%s\n", msg);
69 }
70 fflush(_stream);
71
72 return written;
73 }
74
75 int LogFileStreamOutput::write(LogMessageBuffer::Iterator msg_iterator) {
76 const bool use_decorations = !_decorators.is_empty();
77 char decoration_buf[LogDecorations::DecorationsBufferSize];
78
79 int written = 0;
80 os::flockfile(_stream);
81 LogLevelType previous_level = LogLevel::Invalid;
82 for (; !msg_iterator.is_at_end(); msg_iterator++) {
83 if (use_decorations) {
84 const LogDecorations& decorations = msg_iterator.decorations();
85 // Write decorators the first time, and re-write them if the level changed.
86 if (written == 0 || decorations.level() != previous_level) {
87 write_decorations(decoration_buf, sizeof(decoration_buf), decorations);
88 previous_level = decorations.level();
89 }
90 written += jio_fprintf(_stream, "%s %s\n", decoration_buf, msg_iterator.message());
91 } else {
92 written += jio_fprintf(_stream, "%s\n", msg_iterator.message());
93 }
94 }
95 fflush(_stream);
96 os::funlockfile(_stream);
97
98 return written;
99 }
|