src/hotspot/share/logging/logStream.hpp
author naoto
Tue, 09 Jul 2019 08:05:38 -0700
changeset 55627 9c1885fb2a42
parent 53244 9807daeb47c4
permissions -rw-r--r--
8227127: Era designator not displayed correctly using the COMPAT provider Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     1
/*
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 49961
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     4
 *
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     8
 *
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    13
 * accompanied this code).
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    14
 *
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    18
 *
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    21
 * questions.
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    22
 *
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    23
 */
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    24
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 49961
diff changeset
    25
#ifndef SHARE_LOGGING_LOGSTREAM_HPP
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 49961
diff changeset
    26
#define SHARE_LOGGING_LOGSTREAM_HPP
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    27
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    28
#include "logging/log.hpp"
37461
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    29
#include "logging/logHandle.hpp"
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    30
#include "memory/resourceArea.hpp"
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    31
#include "utilities/ostream.hpp"
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    32
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    33
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    34
class LogStream : public outputStream {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    35
  friend class LogStreamTest_TestLineBufferAllocation_test_vm_Test; // see test/native/logging/test_logStream.cpp
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    36
  friend class LogStreamTest_TestLineBufferAllocationCap_test_vm_Test; // see test/native/logging/test_logStream.cpp
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    37
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    38
  // Helper class, maintains the line buffer. For small line lengths,
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    39
  // we avoid malloc and use a fixed sized member char array. If LogStream
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    40
  // is allocated on the stack, this means small lines are assembled
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    41
  // directly on the stack.
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    42
  class LineBuffer {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    43
    char _smallbuf[64];
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    44
    char* _buf;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    45
    size_t _cap;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    46
    size_t _pos;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    47
    void try_ensure_cap(size_t cap);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    48
  public:
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    49
    LineBuffer();
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    50
    ~LineBuffer();
49961
7379e6f906ae 8202303: LogStream should autoflush on destruction
stuefe
parents: 47216
diff changeset
    51
    bool is_empty() const { return _pos == 0; }
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    52
    const char* buffer() const { return _buf; }
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    53
    void append(const char* s, size_t len);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    54
    void reset();
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    55
  };
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    56
  LineBuffer  _current_line;
37461
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    57
  LogTargetHandle _log_handle;
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    58
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    59
  // Prevent operator new for LogStream.
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    60
  static void* operator new (size_t);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    61
  static void* operator new[] (size_t);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    62
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    63
public:
37461
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    64
  // Constructor to support creation from a LogTarget instance.
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    65
  //
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    66
  // LogTarget(Debug, gc) log;
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    67
  // LogStreamBase(log) stream;
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    68
  template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    69
  LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
37461
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    70
      _log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    71
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    72
  // Constructor to support creation from typed (likely NULL) pointer. Mostly used by the logging framework.
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    73
  //
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    74
  // LogStreamBase stream(log.debug());
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    75
  //  or
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    76
  // LogStreamBase stream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL);
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    77
  template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    78
  LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>* type_carrier) :
37461
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    79
      _log_handle(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    80
49961
7379e6f906ae 8202303: LogStream should autoflush on destruction
stuefe
parents: 47216
diff changeset
    81
  // Destructor writes any unfinished output left in the line buffer.
7379e6f906ae 8202303: LogStream should autoflush on destruction
stuefe
parents: 47216
diff changeset
    82
  ~LogStream();
7379e6f906ae 8202303: LogStream should autoflush on destruction
stuefe
parents: 47216
diff changeset
    83
37461
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    84
  // Constructor to support creation from a LogTargetHandle.
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    85
  //
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    86
  // LogTarget(Debug, gc) log;
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    87
  // LogTargetHandle(log) handle;
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    88
  // LogStreamBase stream(handle);
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    89
  LogStream(LogTargetHandle handle) : _log_handle(handle) {}
37461
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    90
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    91
  // Constructor to support creation from a log level and tagset.
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    92
  //
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    93
  // LogStreamBase(level, tageset);
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    94
  LogStream(LogLevelType level, LogTagSet* tagset) : _log_handle(level, tagset) {}
37461
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    95
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    96
  void write(const char* s, size_t len);
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    97
};
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    98
37461
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
    99
// Support creation of a LogStream without having to provide a LogTarget pointer.
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
   100
#define LogStreamHandle(level, ...) LogStreamTemplate<LogLevel::level, LOG_TAGS(__VA_ARGS__)>
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
   101
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
   102
template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
   103
class LogStreamTemplate : public LogStream {
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
   104
public:
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
   105
  LogStreamTemplate() : LogStream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL) {}
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
   106
};
b5c955296b75 8153659: Create a CHeap backed LogStream class
stefank
parents: 37254
diff changeset
   107
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 49961
diff changeset
   108
#endif // SHARE_LOGGING_LOGSTREAM_HPP