BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball_rule.h
Go to the documentation of this file.
1/// @file ball_rule.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_rule.h -*-C++-*-
8#ifndef INCLUDED_BALL_RULE
9#define INCLUDED_BALL_RULE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_rule ball_rule
15/// @brief Provide an object having a pattern, thresholds, and attributes.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_rule
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_rule-purpose"> Purpose</a>
25/// * <a href="#ball_rule-classes"> Classes </a>
26/// * <a href="#ball_rule-description"> Description </a>
27/// * <a href="#ball_rule-usage"> Usage </a>
28///
29/// # Purpose {#ball_rule-purpose}
30/// Provide an object having a pattern, thresholds, and attributes.
31///
32/// # Classes {#ball_rule-classes}
33///
34/// - ball::Rule: a pattern, thresholds, and attribute sets
35///
36/// @see ball_ruleset
37///
38/// # Description {#ball_rule-description}
39/// This component implements a type, `ball::Rule`, that consists
40/// of a pattern, four threshold levels, and a set of attributes. The pattern
41/// indicates the names of the categories for which the rule will become
42/// relevant. The four threshold levels determine what actions will be
43/// performed on log records when their severity level equals or exceeds any of
44/// these threshold levels. The attribute set is a collection of unique
45/// attribute name/value pairs.
46///
47/// Note that multiple attributes with the same name are permitted so long as
48/// they correspond to different values.
49///
50/// This component participates in the implementation of "Rule-Based Logging".
51/// For more information on how to use that feature, please see the package
52/// level documentation and usage examples for "Rule-Based Logging".
53///
54/// ## Usage {#ball_rule-usage}
55///
56///
57/// The following code fragments illustrate how to create a rule and add
58/// attributes.
59///
60/// We create a rule whose pattern is `WEEKEND*` and whose threshold levels are
61/// all `ball::Severity::e_OFF` except the `pass-through` level. A
62/// `pass-through` level of `ball::Severity::e_INFO` indicates that whenever the
63/// rule is active and the severity is equal to or exceeds
64/// `ball::Severity::e_INFO`, log records will be passed to the observer:
65/// @code
66/// ball::Rule rule("WEEKEND*", // pattern
67/// ball::Severity::e_OFF, // record level
68/// ball::Severity::e_INFO, // pass-through level
69/// ball::Severity::e_OFF, // trigger level
70/// ball::Severity::e_OFF); // triggerAll level
71/// @endcode
72/// Create some attributes and then add one to the rule:
73/// @code
74/// ball::ManagedAttribute p1("myLib.uuid", 4044457);
75/// ball::ManagedAttribute p2("myLib.name", "John Smith");
76/// rule.addAttribute(p1);
77/// @endcode
78/// Attributes can be looked up by the `hasAttribute` method:
79/// @code
80/// assert(true == rule.hasAttribute(p1));
81/// assert(false == rule.hasAttribute(p2));
82/// @endcode
83/// We then add the other attribute:
84/// @code
85/// rule.addAttribute(p2);
86/// assert(true == rule.hasAttribute(p2));
87/// @endcode
88/// Attributes can also be removed from the rule by the `removeAttribute`
89/// method:
90/// @code
91/// rule.removeAttribute(p1);
92/// assert(false == rule.hasAttribute(p1));
93/// assert(true == rule.hasAttribute(p2));
94/// @endcode
95/// The pattern of a rule can be changed by the `setPattern` method:
96/// @code
97/// assert(0 == strcmp(rule.pattern(), "WEEKEND*"));
98///
99/// rule.setPattern("WEEKDAY*");
100/// assert(0 == strcmp(rule.pattern(), "WEEKDAY*"));
101/// @endcode
102/// The threshold levels of a rule can also be modified by the `setLevels`
103/// method:
104/// @code
105/// assert(ball::Severity::e_OFF == rule.recordLevel());
106/// assert(ball::Severity::e_INFO == rule.passLevel());
107/// assert(ball::Severity::e_OFF == rule.triggerLevel());
108/// assert(ball::Severity::e_OFF == rule.triggerAllLevel());
109///
110/// rule.setLevels(ball::Severity::e_INFO,
111/// ball::Severity::e_OFF,
112/// ball::Severity::e_INFO,
113/// ball::Severity::e_INFO);
114///
115/// assert(ball::Severity::e_INFO == rule.recordLevel());
116/// assert(ball::Severity::e_OFF == rule.passLevel());
117/// assert(ball::Severity::e_INFO == rule.triggerLevel());
118/// assert(ball::Severity::e_INFO == rule.triggerAllLevel());
119/// @endcode
120/// @}
121/** @} */
122/** @} */
123
124/** @addtogroup bal
125 * @{
126 */
127/** @addtogroup ball
128 * @{
129 */
130/** @addtogroup ball_rule
131 * @{
132 */
133
134#include <balscm_version.h>
135
138#include <ball_patternutil.h>
139#include <ball_predicate.h>
140#include <ball_predicateset.h>
142
143#include <bslma_allocator.h>
145
147
148#include <bsl_string.h>
149
150
151namespace ball {
152
153class AttributeContainerList;
154
155 // ==========
156 // class Rule
157 // ==========
158
159/// This class defines a value-semantic object that holds a pattern, four
160/// threshold levels, and an attribute set. For each of these fields there
161/// is an accessor for obtaining the field value and a manipulator for
162/// changing that value. There are a few methods as well for directly
163/// adding/removing/searching attributes.
164///
165/// Additionally, this class supports a complete set of *value* *semantic*
166/// operations, including copy construction, assignment and equality
167/// comparison, and `ostream` printing. A precise operational definition of
168/// when two instances have the same value can be found in the description
169/// of `operator==` for the class. This class is *exception* *neutral* with
170/// no guarantee of rollback: If an exception is thrown during the
171/// invocation of a method on a pre-existing instance, the object is left in
172/// a valid state, but its value is undefined. In no event is memory
173/// leaked. Finally, *aliasing* (e.g., using all or part of an object as
174/// both source and destination) is supported in all cases.
175///
176/// See @ref ball_rule
177class Rule {
178
179 // DATA
180 bsl::string d_pattern; // the pattern for the name of
181 // categories to which this rule will
182 // become relevant
183
184 ThresholdAggregate d_thresholds; // an aggregate of four threshold
185 // levels
186
187 ManagedAttributeSet d_attributeSet; // set of attributes
188
189 mutable int d_hashValue; // cached hash value; < 0 indicates it
190 // is invalid
191
192 mutable int d_hashSize; // number of slots from which
193 // 'd_hashValue' was calculated; 0
194 // indicates it is invalid
195
196 // FRIENDS
197 friend bool operator==(const Rule&, const Rule&);
198 friend bool operator!=(const Rule&, const Rule&);
199 friend bsl::ostream& operator<<(bsl::ostream&, const Rule&);
200
201 public:
202 // CLASS METHODS
203
204 /// Return a hash value calculated from the specified `rule` using the
205 /// specified `size` as the number of slots. The value returned is
206 /// guaranteed to be in the range `[0 .. size - 1]`. The behavior is
207 /// undefined unless `0 < size`.
208 static int hash(const Rule& rule, int size);
209
210 // TRAITS
212
213 // CREATORS
214
215 /// Create a `Rule` object whose pattern is an empty string and whose
216 /// thresholds levels are all 0. Optionally specify a `basicAllocator`
217 /// used to supply memory. If `basicAllocator` is 0, the currently
218 /// installed default allocator will be used. Note that a newly created
219 /// `Rule` object does not have any attributes.
220 explicit Rule(bslma::Allocator *basicAllocator = 0);
221
222 /// Create a `Rule` object whose pattern is the specified `pattern` and
223 /// whose thresholds levels are the specified `recordLevel`,
224 /// `passLevel`, `triggerLevel`, and `triggerAllLevel` respectively.
225 /// Optionally specify a `basicAllocator` used to supply memory. If
226 /// `basicAllocator` is 0, the currently installed default allocator
227 /// will be used. The behavior is undefined unless each of the four
228 /// threshold level values is not in the range [0 .. 255]. Note that a
229 /// newly created `Rule` object does not have any attributes.
231 int recordLevel,
232 int passLevel,
233 int triggerLevel,
234 int triggerAllLevel,
235 bslma::Allocator *basicAllocator = 0);
236
237 /// Create a `Rule` object that has the same value as that of the
238 /// specified `original` object. Optionally specify a `basicAllocator`
239 /// used to supply memory. If `basicAllocator` is 0, the currently
240 /// installed default allocator will be used.
241 Rule(const Rule& original, bslma::Allocator *basicAllocator = 0);
242
243 /// Destroy this object.
244 ~Rule() = default;
245
246 // MANIPULATORS
247
248 /// Assign to this object the value of the specified `rhs` object.
249 Rule& operator=(const Rule& rhs);
250
251 /// Add an attribute having the specified `value` to this object.
252 /// Return 1 on success and 0 if an attribute having the same value
253 /// already exists in this object.
254 int addAttribute(const ManagedAttribute& value);
255
256 /// @deprecated Use @ref addAttribute instead.
257 int addPredicate(const ManagedAttribute& value);
258
259 /// Remove the attribute having the specified `value` from this object.
260 /// Return the number of attributes being removed (i.e., 1 on success
261 /// and 0 if no attribute having `value` exists in this object).
262 int removeAttribute(const ManagedAttribute& value);
263
264 /// @deprecated Use @ref removeAttribute instead.
265 int removePredicate(const ManagedAttribute& value);
266
267 /// Remove all attributes from this rule.
268 void removeAll();
269
270 /// @deprecated Use @ref removeAll instead.
271 void removeAllPredicates();
272
273 /// Set the threshold levels of this object to the specified
274 /// `recordLevel`, `passLevel`, `triggerLevel`, and `triggerAllLevel`
275 /// values, respectively, if each of the specified values is in the
276 /// range [0 .. 255]. Return 0 on success, and a non-zero value
277 /// otherwise (with no effect on the threshold levels of this object).
278 int setLevels(int recordLevel,
279 int passLevel,
280 int triggerLevel,
281 int triggerAllLevel);
282
283 /// Set the pattern of this object to the specified `value`.
284 void setPattern(const bsl::string_view& value);
285
286 // ACCESSORS
287
288 /// Return `true` if for every attribute maintained by this object, an
289 /// attribute with the same name and value exists in the specified
290 /// `containerList`; and `false` otherwise.
291 bool evaluate(const AttributeContainerList& containerList) const;
292
293 /// Return the number of attributes managed by this object.
294 int numAttributes() const;
295
296 /// @deprecated Use @ref numAttributes instead.
297 int numPredicates() const;
298
299 /// Return `true` if an attribute having specified `value` exists in
300 /// this object, and `false` otherwise.
301 bool hasAttribute(const ManagedAttribute& value) const;
302
303 /// @deprecated Use @ref hasAttribute instead.
304 bool hasPredicate(const Predicate& value) const;
305
306 /// Return an iterator referring to the first member of the attribute set
307 /// maintained by this object.
309
310 /// Return an iterator referring to one past the last member of the
311 /// attribute set maintained by this object.
313
314 /// Return the record level of this object.
315 int recordLevel() const;
316
317 /// Return the pass level of this object.
318 int passLevel() const;
319
320 /// Return the trigger level of this object.
321 int triggerLevel() const;
322
323 /// Return the trigger-all level of this object.
324 int triggerAllLevel() const;
325
326 /// Return the pattern of this object.
327 const char *pattern() const;
328
329 /// Return `true` if the specified `inputString` matches the pattern of
330 /// this rule, and `false` otherwise. (For the definition of a string
331 /// matching the pattern of a rule, please refer to the function-level
332 /// documentation associated with the `PatternUtil::isMatch` function).
333 bool isMatch(const char *inputString) const;
334
335 /// Format this object to the specified output `stream` at the
336 /// (absolute value of) the optionally specified indentation `level`
337 /// and return a reference to `stream`. If `level` is specified,
338 /// optionally specify `spacesPerLevel`, the number of spaces per
339 /// indentation level for this and all of its nested objects. If
340 /// `level` is negative, suppress indentation of the first line. If
341 /// `spacesPerLevel` is negative, format the entire output on one line,
342 /// suppressing all but the initial indentation (as governed by
343 /// `level`). If `stream` is not valid on entry, this operation has no
344 /// effect.
345 bsl::ostream& print(bsl::ostream& stream,
346 int level = 0,
347 int spacesPerLevel = 4) const;
348};
349
350// FREE OPERATORS
351
352/// Return `true` if the specified `lhs` and `rhs` objects have the same
353/// value, and `false` otherwise. Two `Rule` objects have the same value if
354/// they have the same attributes, the same four respective threshold
355/// levels, and the same pattern.
356bool operator==(const Rule& lhs, const Rule& rhs);
357
358/// Return `true` if the specified `lhs` and `rhs` objects do not have the
359/// same value, and `false` otherwise. Two `Rule` objects do not have the
360/// same value if they have different attributes, different values for any
361/// of the four respective threshold levels, or different patterns.
362bool operator!=(const Rule& lhs, const Rule& rhs);
363
364/// Write the value of the specified `rule` to the specified `output` stream.
365/// Return the specified `output` stream.
366bsl::ostream& operator<<(bsl::ostream& output, const Rule& rule);
367
368// ============================================================================
369// INLINE DEFINITIONS
370// ============================================================================
371
372 // ----------
373 // class Rule
374 // ----------
375
376// CREATORS
377inline
379: d_pattern("", basicAllocator)
380, d_thresholds(0, 0, 0, 0)
381, d_attributeSet(basicAllocator)
382, d_hashValue(-1)
383, d_hashSize(0)
384{
385}
386
387inline
389 int recordLevel,
390 int passLevel,
391 int triggerLevel,
392 int triggerAllLevel,
393 bslma::Allocator *basicAllocator)
394: d_pattern(pattern.data(), pattern.length(), basicAllocator)
395, d_thresholds(recordLevel, passLevel, triggerLevel, triggerAllLevel)
396, d_attributeSet(basicAllocator)
397, d_hashValue(-1)
398, d_hashSize(0)
399{
400}
401
402inline
403Rule::Rule(const Rule& original, bslma::Allocator *basicAllocator)
404: d_pattern(original.d_pattern, basicAllocator)
405, d_thresholds(original.d_thresholds)
406, d_attributeSet(original.d_attributeSet, basicAllocator)
407, d_hashValue(original.d_hashValue)
408, d_hashSize(original.d_hashSize)
409{
410}
411
412// MANIPULATORS
413inline
415{
416 d_hashValue = -1;
417 return d_attributeSet.addAttribute(value);
418}
419
420inline
422{
423 return addAttribute(value);
424}
425
426inline
428{
429 d_hashValue = -1;
430 return d_attributeSet.removeAttribute(value);
431}
432
433inline
435{
436 return removeAttribute(value);
437}
438
439inline
441{
442 d_hashValue = -1;
443 d_attributeSet.removeAll();
444}
445
446inline
448{
449 removeAll();
450}
451
452inline
453int Rule::setLevels(int recordLevel,
454 int passLevel,
455 int triggerLevel,
456 int triggerAllLevel)
457{
458 d_hashValue = -1;
459 return d_thresholds.setLevels(recordLevel,
460 passLevel,
463}
464
465inline
467{
468 d_hashValue = -1;
469 d_pattern.assign(value);
470}
471
472// ACCESSORS
473inline
474bool Rule::evaluate(const AttributeContainerList& containerList) const
475{
476 return d_attributeSet.evaluate(containerList);
477}
478
479inline
481{
482 return d_attributeSet.numAttributes();
483}
484
485inline
487{
488 return numAttributes();
489}
490
491inline
492bool Rule::hasAttribute(const ManagedAttribute& value) const
493{
494 return d_attributeSet.isMember(value);
495}
496
497inline
498bool Rule::hasPredicate(const ManagedAttribute& value) const
499{
500 return hasAttribute(value);
501}
502
503inline
505{
506 return d_attributeSet.begin();
507}
508
509inline
511{
512 return d_attributeSet.end();
513}
514
515inline
517{
518 return d_thresholds.recordLevel();
519}
520
521inline
523{
524 return d_thresholds.passLevel();
525}
526
527inline
529{
530 return d_thresholds.triggerLevel();
531}
532
533inline
535{
536 return d_thresholds.triggerAllLevel();
537}
538
539inline
540const char *Rule::pattern() const
541{
542 return d_pattern.c_str();
543}
544
545inline
546bool Rule::isMatch(const char *inputString) const
547{
548 return PatternUtil::isMatch(inputString, d_pattern.c_str());
549}
550
551} // close package namespace
552
553// FREE OPERATORS
554inline
555bool ball::operator==(const Rule& lhs, const Rule& rhs)
556{
557 if (lhs.d_hashValue > 0
558 && rhs.d_hashValue > 0
559 && lhs.d_hashSize == rhs.d_hashSize
560 && lhs.d_hashValue != rhs.d_hashValue) {
561 return false; // RETURN
562 }
563
564 return lhs.d_pattern == rhs.d_pattern
565 && lhs.d_thresholds == rhs.d_thresholds
566 && lhs.d_attributeSet == rhs.d_attributeSet;
567}
568
569inline
570bool ball::operator!=(const Rule& lhs, const Rule& rhs)
571{
572 return !(lhs == rhs);
573}
574
575inline
576bsl::ostream& ball::operator<<(bsl::ostream& output, const Rule& rule)
577{
578 return rule.print(output, 0, -1);
579}
580
581
582
583#endif
584
585// ----------------------------------------------------------------------------
586// Copyright 2015 Bloomberg Finance L.P.
587//
588// Licensed under the Apache License, Version 2.0 (the "License");
589// you may not use this file except in compliance with the License.
590// You may obtain a copy of the License at
591//
592// http://www.apache.org/licenses/LICENSE-2.0
593//
594// Unless required by applicable law or agreed to in writing, software
595// distributed under the License is distributed on an "AS IS" BASIS,
596// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
597// See the License for the specific language governing permissions and
598// limitations under the License.
599// ----------------------------- END-OF-FILE ----------------------------------
600
601/** @} */
602/** @} */
603/** @} */
Definition ball_attributecontainerlist.h:267
Definition ball_managedattributeset.h:127
int numAttributes() const
Return the number of attributes managed by this object.
Definition ball_managedattributeset.h:388
bool removeAttribute(const ManagedAttribute &value)
Definition ball_managedattributeset.h:350
const_iterator end() const
Definition ball_managedattributeset.h:406
const_iterator begin() const
Definition ball_managedattributeset.h:400
SetType::const_iterator const_iterator
Definition ball_managedattributeset.h:169
bool addAttribute(const ManagedAttribute &value)
Definition ball_managedattributeset.h:338
void removeAll()
Remove all attributes from this attribute set.
Definition ball_managedattributeset.h:362
bool isMember(const ManagedAttribute &value) const
Definition ball_managedattributeset.h:382
bool evaluate(const AttributeContainerList &containerList) const
Definition ball_managedattribute.h:117
Definition ball_rule.h:177
int triggerAllLevel() const
Return the trigger-all level of this object.
Definition ball_rule.h:534
bool evaluate(const AttributeContainerList &containerList) const
Definition ball_rule.h:474
ManagedAttributeSet::const_iterator end() const
Definition ball_rule.h:510
void removeAllPredicates()
Definition ball_rule.h:447
friend bool operator!=(const Rule &, const Rule &)
int addAttribute(const ManagedAttribute &value)
Definition ball_rule.h:414
int numAttributes() const
Return the number of attributes managed by this object.
Definition ball_rule.h:480
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
bool hasAttribute(const ManagedAttribute &value) const
Definition ball_rule.h:492
int removeAttribute(const ManagedAttribute &value)
Definition ball_rule.h:427
bool hasPredicate(const Predicate &value) const
Definition ball_rule.h:498
friend bsl::ostream & operator<<(bsl::ostream &, const Rule &)
int recordLevel() const
Return the record level of this object.
Definition ball_rule.h:516
void setPattern(const bsl::string_view &value)
Set the pattern of this object to the specified value.
Definition ball_rule.h:466
int setLevels(int recordLevel, int passLevel, int triggerLevel, int triggerAllLevel)
Definition ball_rule.h:453
const char * pattern() const
Return the pattern of this object.
Definition ball_rule.h:540
Rule & operator=(const Rule &rhs)
Assign to this object the value of the specified rhs object.
~Rule()=default
Destroy this object.
int numPredicates() const
Definition ball_rule.h:486
ManagedAttributeSet::const_iterator begin() const
Definition ball_rule.h:504
BSLMF_NESTED_TRAIT_DECLARATION(Rule, bslma::UsesBslmaAllocator)
int passLevel() const
Return the pass level of this object.
Definition ball_rule.h:522
void removeAll()
Remove all attributes from this rule.
Definition ball_rule.h:440
int triggerLevel() const
Return the trigger level of this object.
Definition ball_rule.h:528
Rule(bslma::Allocator *basicAllocator=0)
Definition ball_rule.h:378
friend bool operator==(const Rule &, const Rule &)
static int hash(const Rule &rule, int size)
int addPredicate(const ManagedAttribute &value)
Definition ball_rule.h:421
int removePredicate(const ManagedAttribute &value)
Definition ball_rule.h:434
bool isMatch(const char *inputString) const
Definition ball_rule.h:546
Definition ball_thresholdaggregate.h:97
int triggerLevel() const
Return the trigger level of this threshold aggregate.
Definition ball_thresholdaggregate.h:260
int recordLevel() const
Return the record level of this threshold aggregate.
Definition ball_thresholdaggregate.h:248
int passLevel() const
Return the pass level of this threshold aggregate.
Definition ball_thresholdaggregate.h:254
int setLevels(int recordLevel, int passLevel, int triggerLevel, int triggerAllLevel)
int triggerAllLevel() const
Return the trigger-all level of this threshold aggregate.
Definition ball_thresholdaggregate.h:266
Definition bslstl_stringview.h:441
Definition bslstl_string.h:1281
const CHAR_TYPE * c_str() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:6705
basic_string & assign(const basic_string &replacement)
Definition bslstl_string.h:5716
Definition bslma_allocator.h:457
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition ball_administration.h:214
bsl::ostream & operator<<(bsl::ostream &output, const Attribute &attribute)
bool operator!=(const Attribute &lhs, const Attribute &rhs)
bool operator==(const Attribute &lhs, const Attribute &rhs)
static bool isMatch(const char *inputString, const char *pattern)
Definition bslma_usesbslmaallocator.h:343