1 /* 2 * Copyright (c) 2015, 2019, 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_LOGTAGSET_HPP 25 #define SHARE_LOGGING_LOGTAGSET_HPP 26 27 #include "logging/logDecorators.hpp" 28 #include "logging/logLevel.hpp" 29 #include "logging/logOutputList.hpp" 30 #include "logging/logPrefix.hpp" 31 #include "logging/logTag.hpp" 32 #include "utilities/globalDefinitions.hpp" 33 34 class LogMessageBuffer; 35 36 // The tagset represents a combination of tags that occur in a log call somewhere. 37 // Tagsets are created automatically by the LogTagSetMappings and should never be 38 // instantiated directly somewhere else. 39 class LogTagSet { 40 private: 41 static LogTagSet* _list; 42 static size_t _ntagsets; 43 44 LogTagSet* const _next; 45 size_t _ntags; 46 LogTagType _tag[LogTag::MaxTags]; 47 48 LogOutputList _output_list; 49 LogDecorators _decorators; 50 51 typedef size_t (*PrefixWriter)(char* buf, size_t size); 52 PrefixWriter _write_prefix; 53 54 // Keep constructor private to prevent incorrect instantiations of this class. 55 // Only LogTagSetMappings can create/contain instances of this class. 56 // The constructor links all tagsets together in a global list of tagsets. 57 // This list is used during configuration to be able to update all tagsets 58 // and their configurations to reflect the new global log configuration. 59 LogTagSet(PrefixWriter prefix_writer, LogTagType t0, LogTagType t1, LogTagType t2, LogTagType t3, LogTagType t4); 60 61 template <LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag> 62 friend class LogTagSetMapping; 63 64 public: 65 static void describe_tagsets(outputStream* out); 66 static void list_all_tagsets(outputStream* out); 67 first()68 static LogTagSet* first() { 69 return _list; 70 } 71 ntagsets()72 static size_t ntagsets() { 73 return _ntagsets; 74 } 75 next()76 LogTagSet* next() { 77 return _next; 78 } 79 ntags() const80 size_t ntags() const { 81 return _ntags; 82 } 83 tag(size_t idx) const84 LogTagType tag(size_t idx) const { 85 return _tag[idx]; 86 } 87 contains(LogTagType tag) const88 bool contains(LogTagType tag) const { 89 for (size_t i = 0; i < LogTag::MaxTags && _tag[i] != LogTag::__NO_TAG; i++) { 90 if (tag == _tag[i]) { 91 return true; 92 } 93 } 94 return false; 95 } 96 level_for(const LogOutput * output) const97 LogLevelType level_for(const LogOutput* output) const { 98 return _output_list.level_for(output); 99 } 100 disable_outputs()101 void disable_outputs() { 102 _output_list.clear(); 103 } 104 set_output_level(LogOutput * output,LogLevelType level)105 void set_output_level(LogOutput* output, LogLevelType level) { 106 _output_list.set_output_level(output, level); 107 } 108 109 // Refresh the decorators for this tagset to contain the decorators for all 110 // of its current outputs combined with the given decorators. 111 void update_decorators(const LogDecorators& decorator = LogDecorators::None); 112 113 int label(char *buf, size_t len, const char* separator = ",") const; 114 bool has_output(const LogOutput* output); 115 116 // The implementation of this function is put here to ensure 117 // that it is inline:able by the log_is_enabled(level, ...) macro. is_level(LogLevelType level) const118 bool is_level(LogLevelType level) const { 119 return _output_list.is_level(level); 120 } 121 void log(LogLevelType level, const char* msg); 122 void log(const LogMessageBuffer& msg); 123 124 ATTRIBUTE_PRINTF(3, 4) 125 void write(LogLevelType level, const char* fmt, ...); 126 127 template <LogLevelType Level> 128 ATTRIBUTE_PRINTF(2, 3) write(const char * fmt,...)129 void write(const char* fmt, ...) { 130 va_list args; 131 va_start(args, fmt); 132 vwrite(Level, fmt, args); 133 va_end(args); 134 } 135 136 ATTRIBUTE_PRINTF(3, 0) 137 void vwrite(LogLevelType level, const char* fmt, va_list args); 138 }; 139 140 template <LogTagType T0, LogTagType T1 = LogTag::__NO_TAG, LogTagType T2 = LogTag::__NO_TAG, 141 LogTagType T3 = LogTag::__NO_TAG, LogTagType T4 = LogTag::__NO_TAG, 142 LogTagType GuardTag = LogTag::__NO_TAG> 143 class LogTagSetMapping : public AllStatic { 144 private: 145 // Verify number of logging tags does not exceed maximum supported. 146 STATIC_ASSERT(GuardTag == LogTag::__NO_TAG); 147 static LogTagSet _tagset; 148 149 public: tagset()150 static LogTagSet& tagset() { 151 return _tagset; 152 } 153 }; 154 155 // Instantiate the static field _tagset for all tagsets that are used for logging somewhere. 156 // (This must be done here rather than the .cpp file because it's a template.) 157 // Each combination of tags used as template arguments to the Log class somewhere (via macro or not) 158 // will instantiate the LogTagSetMapping template, which in turn creates the static field for that 159 // tagset. This _tagset contains the configuration for those tags. 160 template <LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag> 161 LogTagSet LogTagSetMapping<T0, T1, T2, T3, T4, GuardTag>::_tagset(&LogPrefix<T0, T1, T2, T3, T4>::prefix, T0, T1, T2, T3, T4); 162 163 #endif // SHARE_LOGGING_LOGTAGSET_HPP 164