1 /*
   2  * Copyright (c) 2015, 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 //
  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 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Error>
  48 #define log_warning(...) (!log_is_enabled(Warning, __VA_ARGS__)) ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Warning>
  49 #define log_info(...)    (!log_is_enabled(Info, __VA_ARGS__))    ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Info>
  50 #define log_debug(...)   (!log_is_enabled(Debug, __VA_ARGS__))   ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Debug>
  51 #define log_trace(...)   (!log_is_enabled(Trace, __VA_ARGS__))   ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Trace>
  52 #ifndef PRODUCT
  53 #define log_develop(...) (!log_is_enabled(Develop, __VA_ARGS__)) ? (void)0 : Log<LOG_TAGS(__VA_ARGS__)>::write<LogLevel::Develop>
  54 #else
  55 #define DUMMY_ARGUMENT_CONSUMER(...)
  56 #define log_develop(...) DUMMY_ARGUMENT_CONSUMER
  57 #endif
  58 
  59 // Convenience macro to test if the logging is enabled on the specified level for given tags.
  60 #define log_is_enabled(level, ...) (Log<LOG_TAGS(__VA_ARGS__)>::is_level(LogLevel::level))
  61 
  62 //
  63 // Log class for more advanced logging scenarios.
  64 // Has printf-style member functions for each log level (trace(), debug(), etc).
  65 //
  66 // Also has outputStream compatible API for the different log-levels.
  67 // The streams are resource allocated when requested and are accessed through
  68 // calls to <level>_stream() functions (trace_stream(), debug_stream(), etc).
  69 //
  70 // Example usage:
  71 //   LogHandle(logging) log;
  72 //   if (log.is_debug()) {
  73 //     ...
  74 //     log.debug("result = %d", result).trace(" tracing info");
  75 //     obj->print_on(log.debug_stream());
  76 //   }
  77 //
  78 #define LogHandle(...)  Log<LOG_TAGS(__VA_ARGS__)>
  79 
  80 template <LogTagType T0, LogTagType T1 = LogTag::__NO_TAG, LogTagType T2 = LogTag::__NO_TAG, LogTagType T3 = LogTag::__NO_TAG,
  81           LogTagType T4 = LogTag::__NO_TAG, LogTagType GuardTag = LogTag::__NO_TAG>
  82 class Log VALUE_OBJ_CLASS_SPEC {
  83  private:
  84   static const size_t LogBufferSize = 512;
  85  public:
  86   // Make sure no more than the maximum number of tags have been given.
  87   // The GuardTag allows this to be detected if/when it happens. If the GuardTag
  88   // is not __NO_TAG, the number of tags given exceeds the maximum allowed.
  89   STATIC_ASSERT(GuardTag == LogTag::__NO_TAG); // Number of logging tags exceeds maximum supported!
  90 
  91   Log() {}
  92 
  93   static bool is_level(LogLevelType level) {
  94     return LogTagSetMapping<T0, T1, T2, T3, T4>::tagset().is_level(level);
  95   }
  96 
  97   template <LogLevelType Level>
  98   ATTRIBUTE_PRINTF(1, 2)
  99   static void write(const char* fmt, ...) {
 100     va_list args;
 101     va_start(args, fmt);
 102     vwrite<Level>(fmt, args);
 103     va_end(args);
 104   };
 105 
 106   template <LogLevelType Level>
 107   ATTRIBUTE_PRINTF(1, 0)
 108   static void vwrite(const char* fmt, va_list args) {
 109     char buf[LogBufferSize];
 110     size_t prefix_len = LogPrefix<T0, T1, T2, T3, T4>::prefix(buf, sizeof(buf));
 111     // Check that string fits in buffer; resize buffer if necessary
 112     int ret = os::log_vsnprintf(buf + prefix_len, sizeof(buf) - prefix_len, fmt, args);
 113     assert(ret >= 0, "Log message buffer issue");
 114     if ((size_t)ret > sizeof(buf)) {
 115       size_t newbuf_len = prefix_len + ret + 1;
 116       char* newbuf = NEW_C_HEAP_ARRAY(char, newbuf_len, mtLogging);
 117       prefix_len = LogPrefix<T0, T1, T2, T3, T4>::prefix(newbuf, newbuf_len);
 118       ret = os::log_vsnprintf(newbuf + prefix_len, newbuf_len - prefix_len, fmt, args);
 119       assert(ret >= 0, "Log message buffer issue");
 120       puts<Level>(newbuf);
 121       FREE_C_HEAP_ARRAY(char, newbuf);
 122     } else {
 123       puts<Level>(buf);
 124     }
 125   }
 126 
 127   template <LogLevelType Level>
 128   static void puts(const char* string) {
 129     LogTagSetMapping<T0, T1, T2, T3, T4>::tagset().log(Level, string);
 130   }
 131 
 132 #define LOG_LEVEL(level, name) ATTRIBUTE_PRINTF(2, 0) \
 133   Log& v##name(const char* fmt, va_list args) { \
 134     vwrite<LogLevel::level>(fmt, args); \
 135     return *this; \
 136   } \
 137   Log& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) { \
 138     va_list args; \
 139     va_start(args, fmt); \
 140     vwrite<LogLevel::level>(fmt, args); \
 141     va_end(args); \
 142     return *this; \
 143   } \
 144   static bool is_##name() { \
 145     return is_level(LogLevel::level); \
 146   } \
 147   static outputStream* name##_stream() { \
 148     return new logStream(write<LogLevel::level>); \
 149   }
 150   LOG_LEVEL_LIST
 151 #undef LOG_LEVEL
 152 };
 153 
 154 #endif // SHARE_VM_LOGGING_LOG_HPP