1 /*
   2  * Copyright (c) 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_LOGHANDLE_HPP
  25 #define SHARE_VM_LOGGING_LOGHANDLE_HPP
  26 
  27 #include "logging/log.hpp"
  28 
  29 // Wraps a Log instance and throws away the template information.
  30 //
  31 // This can be used to pass a Log instance as a parameter without
  32 // polluting the surrounding API with template functions.
  33 class LogHandle {
  34 private:
  35   LogTagSet* _tagset;
  36 
  37 public:
  38   template <LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
  39   LogHandle(const LogImpl<T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
  40       _tagset(&LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
  41 
  42   bool is_level(LogLevelType level) {
  43     return _tagset->is_level(level);
  44   }
  45 
  46 #define LOG_LEVEL(level, name) ATTRIBUTE_PRINTF(2, 0)   \
  47   LogHandle& v##name(const char* fmt, va_list args) { \
  48     _tagset->vwrite(LogLevel::level, fmt, args); \
  49     return *this; \
  50   } \
  51   LogHandle& name(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) { \
  52     va_list args; \
  53     va_start(args, fmt); \
  54     _tagset->vwrite(LogLevel::level, fmt, args); \
  55     va_end(args); \
  56     return *this; \
  57   } \
  58   bool is_##name() { \
  59     return _tagset->is_level(LogLevel::level); \
  60   }
  61   LOG_LEVEL_LIST
  62 #undef LOG_LEVEL
  63 };
  64 
  65 // Wraps a LogTarget instance and throws away the template information.
  66 //
  67 // This can be used to pass a Log instance as a parameter without
  68 // polluting the surrounding API with template functions.
  69 class LogTargetHandle {
  70 private:
  71   const LogLevelType _level;
  72   LogTagSet*         _tagset;
  73 
  74 public:
  75   LogTargetHandle(LogLevelType level, LogTagSet* tagset) : _level(level), _tagset(tagset) {}
  76 
  77   template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
  78   LogTargetHandle(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
  79       _level(level),
  80       _tagset(&LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
  81 
  82   template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
  83   static LogTargetHandle create() {
  84     return LogTargetHandle(LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>());
  85   }
  86 
  87   void print(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3) {
  88     va_list args;
  89     va_start(args, fmt);
  90     _tagset->vwrite(_level, fmt, args);
  91     va_end(args);
  92   }
  93 
  94   bool is_enabled() const {
  95     return _tagset->is_level(_level);
  96   }
  97 
  98 };
  99 
 100 #endif // SHARE_VM_LOGGING_LOGHANDLE_HPP