logStream.inline.hpp revision 10768:31b311779a7b
1/*
2 * Copyright (c) 2016, 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#ifndef SHARE_VM_LOGGING_LOGSTREAM_INLINE_HPP
25#define SHARE_VM_LOGGING_LOGSTREAM_INLINE_HPP
26
27#include "logging/log.hpp"
28#include "logging/logStream.hpp"
29#include "memory/resourceArea.hpp"
30#include "utilities/ostream.hpp"
31
32inline void LogStreamNoResourceMark::write(const char* s, size_t len) {
33  if (len > 0 && s[len - 1] == '\n') {
34    _current_line.write(s, len - 1);
35    _tagset->write(_level, "%s", _current_line.as_string());
36    _current_line.reset();
37  } else {
38    _current_line.write(s, len);
39  }
40  update_position(s, len);
41}
42
43// An output stream that logs to the logging framework, and embeds a ResourceMark.
44//
45//  The class is intended to be stack allocated.
46//  Care needs to be taken when nested ResourceMarks are used.
47class LogStream : public outputStream {
48private:
49  ResourceMark            _embedded_resource_mark;
50  LogStreamNoResourceMark _stream;
51
52public:
53  // Constructor to support creation from a LogTarget instance.
54  //
55  // LogTarget(Debug, gc) log;
56  // LogStream(log) stream;
57  template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
58  LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
59      _embedded_resource_mark(),
60      _stream(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
61
62  // Constructor to support creation from typed (likely NULL) pointer. Mostly used by the logging framework.
63  //
64  // LogStream stream(log.debug());
65  // LogStream stream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL);
66  template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
67  LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>* type_carrier) :
68      _embedded_resource_mark(),
69      _stream(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
70
71  // Override of outputStream::write.
72  void write(const char* s, size_t len) { _stream.write(s, len); }
73};
74
75// Support creation of a LogStream without having to provide a LogTarget pointer.
76#define LogStreamHandle(level, ...) LogStreamTemplate<LogLevel::level, LOG_TAGS(__VA_ARGS__)>
77
78template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
79class LogStreamTemplate : public LogStream {
80public:
81  LogStreamTemplate() : LogStream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL) {}
82};
83
84#endif // SHARE_VM_LOGGING_LOGSTREAM_INLINE_HPP
85