src/hotspot/share/logging/logMessage.hpp
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 38263 hotspot/src/share/vm/logging/logMessage.hpp@a7488329ad27
child 53244 9807daeb47c4
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38263
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     1
/*
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     4
 *
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     8
 *
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    13
 * accompanied this code).
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    14
 *
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    18
 *
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    21
 * questions.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    22
 *
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    23
 */
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    24
#ifndef SHARE_VM_LOGGING_LOGMESSAGE_HPP
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    25
#define SHARE_VM_LOGGING_LOGMESSAGE_HPP
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    26
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    27
#include "logging/log.hpp"
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    28
#include "logging/logMessageBuffer.hpp"
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    29
#include "logging/logPrefix.hpp"
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    30
#include "logging/logTag.hpp"
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    31
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    32
// The LogMessage class represents a multi-part/multi-line message
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    33
// that is guaranteed to be sent and written to the log outputs
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    34
// in a way that prevents interleaving by other log messages.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    35
//
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    36
// The interface of LogMessage is very similar to the Log class,
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    37
// with printf functions for each level (trace(), debug(), etc).
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    38
// The difference is that these functions will append/write to the
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    39
// LogMessage, which only buffers the message-parts until the whole
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    40
// message is sent to a log (using Log::write). Internal buffers
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    41
// are C heap allocated lazily on first write. LogMessages are
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    42
// automatically written when they go out of scope.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    43
//
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    44
// Example usage:
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    45
//
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    46
// {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    47
//   LogMessage(logging) msg;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    48
//   if (msg.is_debug()) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    49
//     msg.debug("debug message");
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    50
//     msg.trace("additional trace information");
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    51
//   }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    52
// }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    53
//
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    54
// Log outputs on trace level will see both of the messages above,
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    55
// and the trace line will immediately follow the debug line.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    56
// They will have identical decorations (apart from level).
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    57
// Log outputs on debug level will see the debug message,
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    58
// but not the trace message.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    59
//
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    60
#define LogMessage(...) LogMessageImpl<LOG_TAGS(__VA_ARGS__)>
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    61
template <LogTagType T0, LogTagType T1 = LogTag::__NO_TAG, LogTagType T2 = LogTag::__NO_TAG,
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    62
          LogTagType T3 = LogTag::__NO_TAG, LogTagType T4 = LogTag::__NO_TAG, LogTagType GuardTag = LogTag::__NO_TAG>
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    63
class LogMessageImpl : public LogMessageBuffer {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    64
 private:
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    65
  LogImpl<T0, T1, T2, T3, T4, GuardTag> _log;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    66
  bool _has_content;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    67
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    68
 public:
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    69
  LogMessageImpl() : _has_content(false) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    70
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    71
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    72
  ~LogMessageImpl() {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    73
    if (_has_content) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    74
      flush();
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    75
    }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    76
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    77
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    78
  void flush() {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    79
    _log.write(*this);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    80
    reset();
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    81
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    82
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    83
  void reset() {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    84
    _has_content = false;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    85
    LogMessageBuffer::reset();
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    86
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    87
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    88
  ATTRIBUTE_PRINTF(3, 0)
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    89
  void vwrite(LogLevelType level, const char* fmt, va_list args) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    90
    if (!_has_content) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    91
      _has_content = true;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    92
      set_prefix(LogPrefix<T0, T1, T2, T3, T4>::prefix);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    93
    }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    94
    LogMessageBuffer::vwrite(level, fmt, args);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    95
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    96
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    97
#define LOG_LEVEL(level, name) \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    98
  bool is_##name() const { \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
    99
    return _log.is_level(LogLevel::level); \
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   100
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   101
  LOG_LEVEL_LIST
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   102
#undef LOG_LEVEL
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   103
};
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   104
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents:
diff changeset
   105
#endif // SHARE_VM_LOGGING_LOGMESSAGE_HPP