1 /*
   2  * Copyright (c) 2015, 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_LOG_HPP
  25 #define SHARE_VM_LOGGING_LOG_HPP
  26 
  27 #include "logging/logLevel.hpp"
  28 #include "logging/logPrefix.hpp"
  29 #include "logging/logTagSet.hpp"
  30 #include "logging/logTag.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "runtime/os.hpp"
  33 #include "utilities/debug.hpp"
  34 
  35 class LogMessageBuffer;
  36 
  37 //
  38 // Logging macros
  39 //
  40 // Usage:
  41 //   log_<level>(<comma separated log tags>)(<printf-style log arguments>);
  42 // e.g.
  43 //   log_debug(logging)("message %d", i);
  44 //
  45 // Note that these macros will not evaluate the arguments unless the logging is enabled.
  46 //
  47 #define log_error(...)   (!log_is_enabled(Error, __VA_ARGS__))   ? (void)0 : LogImpl<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Error>
  48 #define log_warning(...) (!log_is_enabled(Warning, __VA_ARGS__)) ? (void)0 : LogImpl<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Warning>
  49 #define log_info(...)    (!log_is_enabled(Info, __VA_ARGS__))    ? (void)0 : LogImpl<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Info>
  50 #define log_debug(...)   (!log_is_enabled(Debug, __VA_ARGS__))   ? (void)0 : LogImpl<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Debug>
  51 #define log_trace(...)   (!log_is_enabled(Trace, __VA_ARGS__))   ? (void)0 : LogImpl<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Trace>
  52 
  53 // Macros for logging that should be excluded in product builds.
  54 // Available for levels Info, Debug and Trace. Includes test macro that
  55 // evaluates to false in product builds.
  56 #ifndef PRODUCT
  57 #define log_develop_info(...)  (!log_is_enabled(Info, __VA_ARGS__))   ? (void)0 : LogImpl<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Info>
  58 #define log_develop_debug(...) (!log_is_enabled(Debug, __VA_ARGS__)) ? (void)0 : LogImpl<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Debug>
  59 #define log_develop_trace(...) (!log_is_enabled(Trace, __VA_ARGS__))  ? (void)0 : LogImpl<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Trace>
  60 #define log_develop_is_enabled(level, ...)  log_is_enabled(level, __VA_ARGS__)
  61 #else
  62 #define DUMMY_ARGUMENT_CONSUMER(...)
  63 #define log_develop_info(...)  DUMMY_ARGUMENT_CONSUMER
  64 #define log_develop_debug(...) DUMMY_ARGUMENT_CONSUMER
  65 #define log_develop_trace(...) DUMMY_ARGUMENT_CONSUMER
  66 #define log_develop_is_enabled(...)  false
  67 #endif
  68 
  69 // Convenience macro to test if the logging is enabled on the specified level for given tags.
  70 #define log_is_enabled(level, ...) (LogImpl<LOG_TAGS(__VA_ARGS__)>::is_level(LogLevel::level))
  71 
  72 //
  73 // Log class for more advanced logging scenarios.
  74 // Has printf-style member functions for each log level (trace(), debug(), etc).
  75 //
  76 // Also has outputStream compatible API for the different log-levels.
  77 // The streams are resource allocated when requested and are accessed through
  78 // calls to <level>_stream() functions (trace_stream(), debug_stream(), etc).
  79 //
  80 // Example usage:
  81 //   Log(logging) log;
  82 //   if (log.is_debug()) {
  83 //     ...
  84 //     log.debug("result = %d", result).trace(" tracing info");
  85 //     obj->print_on(log.debug_stream());
  86 //   }
  87 //
  88 #define Log(...)  LogImpl<LOG_TAGS(__VA_ARGS__)>
  89 
  90 //
  91 // Log class that embeds both log tags and a log level.
  92 //
  93 // The class provides a way to write the tags and log level once,
  94 // so that redundant specification of tags or levels can be avoided.
  95 //
  96 // Example usage:
  97 //   LogTarget(Debug, gc) out;
  98 //   if (out.is_enabled()) {
  99 //     ...
 100 //     out.print("Worker: %u", i);
 101 //     out.print(" data: %d", x);
 102 //     ...
 103 //     print_stats(out.stream());
 104 //   }
 105 //
 106 #define LogTarget(level, ...) LogTargetImpl<LogLevel::level, LOG_TAGS(__VA_ARGS__)>
 107 
 108 // Forward declaration to decouple this file from the outputStream API.
 109 class outputStream;
 110 outputStream* create_log_stream(LogLevelType level, LogTagSet* tagset);
 111 
 112 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
 113 class LogTargetImpl;
 114 
 115 template <LogTagType T0, LogTagType T1 = LogTag::__NO_TAG, LogTagType T2 = LogTag::__NO_TAG, LogTagType T3 = LogTag::__NO_TAG,
 116           LogTagType T4 = LogTag::__NO_TAG, LogTagType GuardTag = LogTag::__NO_TAG>
 117 class LogImpl VALUE_OBJ_CLASS_SPEC {
 118  private:
 119   static const size_t LogBufferSize = 512;
 120  public:
 121   // Make sure no more than the maximum number of tags have been given.
 122   // The GuardTag allows this to be detected if/when it happens. If the GuardTag
 123   // is not __NO_TAG, the number of tags given exceeds the maximum allowed.
 124   STATIC_ASSERT(GuardTag == LogTag::__NO_TAG); // Number of logging tags exceeds maximum supported!
 125 
 126   // Empty constructor to avoid warnings on MSVC about unused variables
 127   // when the log instance is only used for static functions.
 128   LogImpl() {
 129   }
 130 
 131   static bool is_level(LogLevelType level) {
 132     return LogTagSetMapping<T0, T1, T2, T3, T4>::tagset().is_level(level);
 133   }
 134 
 135   ATTRIBUTE_PRINTF(2, 3)
 136   static void write(LogLevelType level, const char* fmt, ...) {
 137     va_list args;
 138     va_start(args, fmt);
 139     vwrite(level, fmt, args);
 140     va_end(args);
 141   }
 142 
 143   static void write(const LogMessageBuffer& msg) {
 144     LogTagSetMapping<T0, T1, T2, T3, T4>::tagset().log(msg);
 145   };
 146 
 147   template <LogLevelType Level>
 148   ATTRIBUTE_PRINTF(1, 2)
 149   static void write(const char* fmt, ...) {
 150     va_list args;
 151     va_start(args, fmt);
 152     vwrite(Level, fmt, args);
 153     va_end(args);
 154   }
 155 
 156   ATTRIBUTE_PRINTF(2, 0)
 157   static void vwrite(LogLevelType level, const char* fmt, va_list args) {
 158     LogTagSetMapping<T0, T1, T2, T3, T4>::tagset().vwrite(level, fmt, args);
 159   }
 160 
 161 #define LOG_LEVEL(level, name) ATTRIBUTE_PRINTF(2, 0) \
 162   LogImpl& v##name(const char* fmt, va_list args) { \
 163     vwrite(LogLevel::level, fmt, args); \
 164     return *this; \
 165   } \
 166   LogImpl& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) { \
 167     va_list args; \
 168     va_start(args, fmt); \
 169     vwrite(LogLevel::level, fmt, args); \
 170     va_end(args); \
 171     return *this; \
 172   } \
 173   static bool is_##name() { \
 174     return is_level(LogLevel::level); \
 175   } \
 176   static outputStream* name##_stream() { \
 177     return create_log_stream(LogLevel::level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()); \
 178   } \
 179   static LogTargetImpl<LogLevel::level, T0, T1, T2, T3, T4, GuardTag>* name() { \
 180     return (LogTargetImpl<LogLevel::level, T0, T1, T2, T3, T4, GuardTag>*)NULL; \
 181   }
 182   LOG_LEVEL_LIST
 183 #undef LOG_LEVEL
 184 };
 185 
 186 // Combines logging tags and a logging level.
 187 template <LogLevelType level, LogTagType T0, LogTagType T1 = LogTag::__NO_TAG, LogTagType T2 = LogTag::__NO_TAG,
 188           LogTagType T3 = LogTag::__NO_TAG, LogTagType T4 = LogTag::__NO_TAG, LogTagType GuardTag = LogTag::__NO_TAG>
 189 class LogTargetImpl {
 190 public:
 191   // Empty constructor to avoid warnings on MSVC about unused variables
 192   // when the log instance is only used for static functions.
 193   LogTargetImpl() {
 194   }
 195 
 196   static bool is_enabled() {
 197     return LogImpl<T0, T1, T2, T3, T4, GuardTag>::is_level(level);
 198   }
 199 
 200   static void print(const char* fmt, ...) ATTRIBUTE_PRINTF(1, 2) {
 201     va_list args;
 202     va_start(args, fmt);
 203     LogImpl<T0, T1, T2, T3, T4, GuardTag>::vwrite(level, fmt, args);
 204     va_end(args);
 205   }
 206 
 207   static outputStream* stream() {
 208     return create_log_stream(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset());
 209   }
 210 };
 211 
 212 #endif // SHARE_VM_LOGGING_LOG_HPP