1 /*
   2  * Copyright (c) 2020, 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_LOGGING_ASYNCLOG_HPP
  25 #define SHARE_LOGGING_ASYNCLOG_HPP
  26 #include "logging/logLevel.hpp"
  27 #include "logging/logPrefix.hpp"
  28 #include "logging/logTagSet.hpp"
  29 #include "logging/logTag.hpp"
  30 #include "logging/logAsyncFlusher.hpp"
  31 #include "logging/logMessageBuffer.hpp"
  32 
  33 #define asynclog_error(...)   (!asynclog_is_enabled(Error, __VA_ARGS__))   ? (void)0 : AsyncLogImpl<LOG_TAGS(__VA_ARGS__)>().write<LogLevel::Error>
  34 #define asynclog_warning(...) (!asynclog_is_enabled(Warning, __VA_ARGS__)) ? (void)0 : AsyncLogImpl<LOG_TAGS(__VA_ARGS__)>().write<LogLevel::Warning>
  35 #define asynclog_info(...)    (!asynclog_is_enabled(Info, __VA_ARGS__))    ? (void)0 : AsyncLogImpl<LOG_TAGS(__VA_ARGS__)>().write<LogLevel::Info>
  36 #define asynclog_debug(...)   (!asynclog_is_enabled(Debug, __VA_ARGS__))   ? (void)0 : AsyncLogImpl<LOG_TAGS(__VA_ARGS__)>().write<LogLevel::Debug>
  37 #define asynclog_trace(...)   (!asynclog_is_enabled(Trace, __VA_ARGS__))   ? (void)0 : AsyncLogImpl<LOG_TAGS(__VA_ARGS__)>().write<LogLevel::Trace>
  38 
  39 // Macros for logging that should be excluded in product builds.
  40 // Available for levels Info, Debug and Trace. Includes test macro that
  41 // evaluates to false in product builds.
  42 #ifndef PRODUCT
  43 #define asynclog_develop_info(...)  (!asynclog_is_enabled(Info, __VA_ARGS__))   ? (void)0 : AsyncLogImpl<LOG_TAGS(__VA_ARGS__)>().write<LogLevel::Info>
  44 #define asynclog_develop_debug(...) (!asynclog_is_enabled(Debug, __VA_ARGS__))  ? (void)0 : AsyncLogImpl<LOG_TAGS(__VA_ARGS__)>().write<LogLevel::Debug>
  45 #define asynclog_develop_trace(...) (!asynclog_is_enabled(Trace, __VA_ARGS__))  ? (void)0 : AsyncLogImpl<LOG_TAGS(__VA_ARGS__)>().write<LogLevel::Trace>
  46 #define asynclog_develop_is_enabled(level, ...)  asynclog_is_enabled(level, __VA_ARGS__)
  47 #else
  48 #define DUMMY_ARGUMENT_CONSUMER(...)
  49 #define asynclog_develop_info(...)  DUMMY_ARGUMENT_CONSUMER
  50 #define asynclog_develop_debug(...) DUMMY_ARGUMENT_CONSUMER
  51 #define asynclog_develop_trace(...) DUMMY_ARGUMENT_CONSUMER
  52 #define asynclog_develop_is_enabled(...)  false
  53 #endif
  54 
  55 // Convenience macro to test if the logging is enabled on the specified level for given tags.
  56 #define asynclog_is_enabled(level, ...) (AsyncLogImpl<LOG_TAGS(__VA_ARGS__)>::is_level(LogLevel::level))
  57 
  58 #define AsyncLog(...)  AsyncLogImpl<LOG_TAGS(__VA_ARGS__)>
  59 #define AsyncLogTarget(level, ...) AsyncLogTargetImpl<LogLevel::level, LOG_TAGS(__VA_ARGS__)>
  60 
  61 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
  62 class AsyncLogTargetImpl;
  63 
  64 class Arena;
  65 
  66 template <LogTagType T0, LogTagType T1 = LogTag::__NO_TAG, LogTagType T2 = LogTag::__NO_TAG, LogTagType T3 = LogTag::__NO_TAG,
  67           LogTagType T4 = LogTag::__NO_TAG, LogTagType GuardTag = LogTag::__NO_TAG>
  68 class AsyncLogImpl {
  69  private:
  70   Arena* _arena;
  71   LogMessageBuffer* _buf;
  72   void create_async_buffer() {
  73       Arena* arena = new (mtLogging)Arena(mtLogging, LogMessageBuffer::initial_buffer_size());
  74       _buf = new (mtLogging)LogMessageBuffer(arena);
  75       _buf->set_prefix(LogPrefix<T0, T1, T2, T3, T4>::prefix);
  76   }
  77 
  78  public:
  79   // Make sure no more than the maximum number of tags have been given.
  80   // The GuardTag allows this to be detected if/when it happens. If the GuardTag
  81   // is not __NO_TAG, the number of tags given exceeds the maximum allowed.
  82   STATIC_ASSERT(GuardTag == LogTag::__NO_TAG); // Number of logging tags exceeds maximum supported!
  83 
  84   AsyncLogImpl() : _arena(NULL), _buf(NULL) {
  85   }
  86 
  87   ~AsyncLogImpl() {
  88       if (_buf != NULL) {
  89         LogAsyncFlusher* flusher = LogAsyncFlusher::instance();
  90         flusher->enqueue(&LogTagSetMapping<T0, T1, T2, T3, T4>::tagset(), _buf);
  91       }
  92   }
  93 
  94   static bool is_level(LogLevelType level) {
  95     return LogTagSetMapping<T0, T1, T2, T3, T4>::tagset().is_level(level);
  96   }
  97 
  98   ATTRIBUTE_PRINTF(3, 4)
  99   void write(LogLevelType level, const char* fmt, ...) {
 100     va_list args;
 101     va_start(args, fmt);
 102     vwrite(level, fmt, args);
 103     va_end(args);
 104   }
 105 
 106   // to be implemented or deleted.
 107   void write(const LogMessageBuffer& msg);
 108 
 109   template <LogLevelType Level>
 110   ATTRIBUTE_PRINTF(2, 3)
 111   void write(const char* fmt, ...) {
 112     va_list args;
 113     va_start(args, fmt);
 114     vwrite(Level, fmt, args);
 115     va_end(args);
 116   }
 117 
 118   ATTRIBUTE_PRINTF(3, 0)
 119   void vwrite(LogLevelType level, const char* fmt, va_list args) {
 120     if (!_buf) create_async_buffer();
 121     _buf->vwrite(level, fmt, args);
 122   }
 123 
 124 #define LOG_LEVEL(level, name) ATTRIBUTE_PRINTF(2, 0) \
 125   AsyncLogImpl& v##name(const char* fmt, va_list args) { \
 126     vwrite(LogLevel::level, fmt, args); \
 127     return *this; \
 128   } \
 129   AsyncLogImpl& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) { \
 130     va_list args; \
 131     va_start(args, fmt); \
 132     vwrite(LogLevel::level, fmt, args); \
 133     va_end(args); \
 134     return *this; \
 135   } \
 136   static bool is_##name() { \
 137     return is_level(LogLevel::level); \
 138   } \
 139   static AsyncLogTargetImpl<LogLevel::level, T0, T1, T2, T3, T4, GuardTag>* name() { \
 140     return (AsyncLogTargetImpl<LogLevel::level, T0, T1, T2, T3, T4, GuardTag>*)NULL; \
 141   }
 142   LOG_LEVEL_LIST
 143 #undef LOG_LEVEL
 144 };
 145 
 146 // Combines logging tags and a logging level.
 147 template <LogLevelType level, LogTagType T0, LogTagType T1 = LogTag::__NO_TAG, LogTagType T2 = LogTag::__NO_TAG,
 148           LogTagType T3 = LogTag::__NO_TAG, LogTagType T4 = LogTag::__NO_TAG, LogTagType GuardTag = LogTag::__NO_TAG>
 149 class AsyncLogTargetImpl {
 150 public:
 151   // Empty constructor to avoid warnings on MSVC about unused variables
 152   // when the log instance is only used for static functions.
 153   AsyncLogTargetImpl() {
 154   }
 155 
 156   static bool is_enabled() {
 157     return AsyncLogImpl<T0, T1, T2, T3, T4, GuardTag>::is_level(level);
 158   }
 159 
 160   static void print(const char* fmt, ...) ATTRIBUTE_PRINTF(1, 2) {
 161     AsyncLogImpl<T0, T1, T2, T3, T4, GuardTag> impl;
 162     va_list args;
 163 
 164     va_start(args, fmt);
 165     impl.vwrite(level, fmt, args);
 166     va_end(args);
 167   }
 168 
 169 };
 170 
 171 #endif // SHARE_LOGGING_ASYNCLOG_HPP