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 "memory/allocation.inline.hpp"
  33 #include "runtime/os.hpp"
  34 #include "utilities/debug.hpp"
  35 #include "utilities/ostream.hpp"
  36 
  37 class LogMessageBuffer;
  38 
  39 //
  40 // Logging macros
  41 //
  42 // Usage:
  43 //   log_<level>(<comma separated log tags>)(<printf-style log arguments>);
  44 // e.g.
  45 //   log_debug(logging)("message %d", i);
  46 //
  47 // Note that these macros will not evaluate the arguments unless the logging is enabled.
  48 //
  49 #define log_error(...)   (!log_is_enabled(Error, __VA_ARGS__))   ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Error>
  50 #define log_warning(...) (!log_is_enabled(Warning, __VA_ARGS__)) ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Warning>
  51 #define log_info(...)    (!log_is_enabled(Info, __VA_ARGS__))    ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Info>
  52 #define log_debug(...)   (!log_is_enabled(Debug, __VA_ARGS__))   ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Debug>
  53 #define log_trace(...)   (!log_is_enabled(Trace, __VA_ARGS__))   ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Trace>
  54 
  55 // Macros for logging that should be excluded in product builds.
  56 // Available for levels Info, Debug and Trace. Includes test macro that
  57 // evaluates to false in product builds.
  58 #ifndef PRODUCT
  59 #define log_develop_info(...)  (!log_is_enabled(Info, __VA_ARGS__))   ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Info>
  60 #define log_develop_debug(...) (!log_is_enabled(Debug, __VA_ARGS__)) ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Debug>
  61 #define log_develop_trace(...) (!log_is_enabled(Trace, __VA_ARGS__))  ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Trace>
  62 #define log_develop_is_enabled(level, ...)  log_is_enabled(level, __VA_ARGS__)
  63 #else
  64 #define DUMMY_ARGUMENT_CONSUMER(...)
  65 #define log_develop_info(...)  DUMMY_ARGUMENT_CONSUMER
  66 #define log_develop_debug(...) DUMMY_ARGUMENT_CONSUMER
  67 #define log_develop_trace(...) DUMMY_ARGUMENT_CONSUMER
  68 #define log_develop_is_enabled(...)  false
  69 #endif
  70 
  71 // Convenience macro to test if the logging is enabled on the specified level for given tags.
  72 #define log_is_enabled(level, ...) (Log<LOG_TAGS(__VA_ARGS__)>::is_level(LogLevel::level))
  73 
  74 //
  75 // Log class for more advanced logging scenarios.
  76 // Has printf-style member functions for each log level (trace(), debug(), etc).
  77 //
  78 // Also has outputStream compatible API for the different log-levels.
  79 // The streams are resource allocated when requested and are accessed through
  80 // calls to <level>_stream() functions (trace_stream(), debug_stream(), etc).
  81 //
  82 // Example usage:
  83 //   LogHandle(logging) log;
  84 //   if (log.is_debug()) {
  85 //     ...
  86 //     log.debug("result = %d", result).trace(" tracing info");
  87 //     obj->print_on(log.debug_stream());
  88 //   }
  89 //
  90 #define LogHandle(...)  Log<LOG_TAGS(__VA_ARGS__)>
  91 
  92 template <LogTagType T0, LogTagType T1 = LogTag::__NO_TAG, LogTagType T2 = LogTag::__NO_TAG, LogTagType T3 = LogTag::__NO_TAG,
  93           LogTagType T4 = LogTag::__NO_TAG, LogTagType GuardTag = LogTag::__NO_TAG>
  94 class Log VALUE_OBJ_CLASS_SPEC {
  95  private:
  96   static const size_t LogBufferSize = 512;
  97  public:
  98   // Make sure no more than the maximum number of tags have been given.
  99   // The GuardTag allows this to be detected if/when it happens. If the GuardTag
 100   // is not __NO_TAG, the number of tags given exceeds the maximum allowed.
 101   STATIC_ASSERT(GuardTag == LogTag::__NO_TAG); // Number of logging tags exceeds maximum supported!
 102 
 103   // Empty constructor to avoid warnings on MSVC about unused variables
 104   // when the log instance is only used for static functions.
 105   Log() {
 106   }
 107 
 108   static bool is_level(LogLevelType level) {
 109     return LogTagSetMapping<T0, T1, T2, T3, T4>::tagset().is_level(level);
 110   }
 111 
 112   static void write(const LogMessageBuffer& msg) {
 113     LogTagSetMapping<T0, T1, T2, T3, T4>::tagset().log(msg);
 114   };
 115 
 116   template <LogLevelType Level>
 117   ATTRIBUTE_PRINTF(1, 2)
 118   static void write(const char* fmt, ...) {
 119     va_list args;
 120     va_start(args, fmt);
 121     vwrite<Level>(fmt, args);
 122     va_end(args);
 123   };
 124 
 125   template <LogLevelType Level>
 126   ATTRIBUTE_PRINTF(1, 0)
 127   static void vwrite(const char* fmt, va_list args) {
 128     char buf[LogBufferSize];
 129     va_list saved_args;         // For re-format on buf overflow.
 130     va_copy(saved_args, args);
 131     size_t prefix_len = LogPrefix<T0, T1, T2, T3, T4>::prefix(buf, sizeof(buf));
 132     // Check that string fits in buffer; resize buffer if necessary
 133     int ret = os::log_vsnprintf(buf + prefix_len, sizeof(buf) - prefix_len, fmt, args);
 134     assert(ret >= 0, "Log message buffer issue");
 135     if ((size_t)ret >= sizeof(buf)) {
 136       size_t newbuf_len = prefix_len + ret + 1;
 137       char* newbuf = NEW_C_HEAP_ARRAY(char, newbuf_len, mtLogging);
 138       prefix_len = LogPrefix<T0, T1, T2, T3, T4>::prefix(newbuf, newbuf_len);
 139       ret = os::log_vsnprintf(newbuf + prefix_len, newbuf_len - prefix_len, fmt, saved_args);
 140       assert(ret >= 0, "Log message buffer issue");
 141       puts<Level>(newbuf);
 142       FREE_C_HEAP_ARRAY(char, newbuf);
 143     } else {
 144       puts<Level>(buf);
 145     }
 146     va_end(saved_args);
 147   }
 148 
 149   template <LogLevelType Level>
 150   static void puts(const char* string) {
 151     LogTagSetMapping<T0, T1, T2, T3, T4>::tagset().log(Level, string);
 152   }
 153 
 154 #define LOG_LEVEL(level, name) ATTRIBUTE_PRINTF(2, 0) \
 155   Log& v##name(const char* fmt, va_list args) { \
 156     vwrite<LogLevel::level>(fmt, args); \
 157     return *this; \
 158   } \
 159   Log& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) { \
 160     va_list args; \
 161     va_start(args, fmt); \
 162     vwrite<LogLevel::level>(fmt, args); \
 163     va_end(args); \
 164     return *this; \
 165   } \
 166   static bool is_##name() { \
 167     return is_level(LogLevel::level); \
 168   } \
 169   static outputStream* name##_stream() { \
 170     return new logStream(write<LogLevel::level>); \
 171   }
 172   LOG_LEVEL_LIST
 173 #undef LOG_LEVEL
 174 };
 175 
 176 #endif // SHARE_VM_LOGGING_LOG_HPP