BDE 4.39.x 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]`.
207 ///
208 /// \pre The behavior is undefined unless `0 < size`.
209 static int hash(const Rule& rule, int size);
210
211 // TRAITS
213
214 // CREATORS
215
216 /// Create a `Rule` object whose pattern is an empty string and whose
217 /// thresholds levels are all 0. Optionally specify a `basicAllocator`
218 /// used to supply memory. If `basicAllocator` is 0, the currently installed default allocator will be used.
219 ///
220 /// \note Note that a newly created
221 /// `Rule` object does not have any attributes.
222 explicit Rule(bslma::Allocator *basicAllocator = 0);
223
224 /// Create a `Rule` object whose pattern is the specified `pattern` and
225 /// whose thresholds levels are the specified `recordLevel`,
226 /// `passLevel`, `triggerLevel`, and `triggerAllLevel` respectively.
227 /// Optionally specify a `basicAllocator` used to supply memory. If
228 /// `basicAllocator` is 0, the currently installed default allocator will be used.
229 ///
230 /// \pre The behavior is undefined unless each of the four threshold level values is not in the range [0 .. 255].
231 ///
232 /// \note Note that a
233 /// newly created `Rule` object does not have any attributes.
235 int recordLevel,
236 int passLevel,
237 int triggerLevel,
238 int triggerAllLevel,
239 bslma::Allocator *basicAllocator = 0);
240
241 /// Create a `Rule` object that has the same value as that of the
242 /// specified `original` object. Optionally specify a `basicAllocator`
243 /// used to supply memory. If `basicAllocator` is 0, the currently
244 /// installed default allocator will be used.
245 Rule(const Rule& original, bslma::Allocator *basicAllocator = 0);
246
247 /// Destroy this object.
248 ~Rule() = default;
249
250 // MANIPULATORS
251
252 /// Assign to this object the value of the specified `rhs` object.
253 Rule& operator=(const Rule& rhs);
254
255 /// Add an attribute having the specified `value` to this object.
256 /// Return 1 on success and 0 if an attribute having the same value
257 /// already exists in this object.
258 int addAttribute(const ManagedAttribute& value);
259
260 /// @deprecated Use @ref addAttribute instead.
261 int addPredicate(const ManagedAttribute& value);
262
263 /// Remove the attribute having the specified `value` from this object.
264 /// Return the number of attributes being removed (i.e., 1 on success
265 /// and 0 if no attribute having `value` exists in this object).
266 int removeAttribute(const ManagedAttribute& value);
267
268 /// @deprecated Use @ref removeAttribute instead.
269 int removePredicate(const ManagedAttribute& value);
270
271 /// Remove all attributes from this rule.
272 void removeAll();
273
274 /// @deprecated Use @ref removeAll instead.
275 void removeAllPredicates();
276
277 /// Set the threshold levels of this object to the specified
278 /// `recordLevel`, `passLevel`, `triggerLevel`, and `triggerAllLevel`
279 /// values, respectively, if each of the specified values is in the
280 /// range [0 .. 255]. Return 0 on success, and a non-zero value
281 /// otherwise (with no effect on the threshold levels of this object).
282 int setLevels(int recordLevel,
283 int passLevel,
284 int triggerLevel,
285 int triggerAllLevel);
286
287 /// Set the pattern of this object to the specified `value`.
288 void setPattern(const bsl::string_view& value);
289
290 // ACCESSORS
291
292 /// Return `true` if for every attribute maintained by this object, an
293 /// attribute with the same name and value exists in the specified
294 /// `containerList`; and `false` otherwise.
295 bool evaluate(const AttributeContainerList& containerList) const;
296
297 /// Return the number of attributes managed by this object.
298 int numAttributes() const;
299
300 /// @deprecated Use @ref numAttributes instead.
301 int numPredicates() const;
302
303 /// Return `true` if an attribute having specified `value` exists in
304 /// this object, and `false` otherwise.
305 bool hasAttribute(const ManagedAttribute& value) const;
306
307 /// @deprecated Use @ref hasAttribute instead.
308 bool hasPredicate(const Predicate& value) const;
309
310 /// Return an iterator referring to the first member of the attribute set
311 /// maintained by this object.
313
314 /// Return an iterator referring to one past the last member of the
315 /// attribute set maintained by this object.
317
318 /// Return the record level of this object.
319 int recordLevel() const;
320
321 /// Return the pass level of this object.
322 int passLevel() const;
323
324 /// Return the trigger level of this object.
325 int triggerLevel() const;
326
327 /// Return the trigger-all level of this object.
328 int triggerAllLevel() const;
329
330 /// Return the pattern of this object.
331 const char *pattern() const;
332
333 /// Return `true` if the specified `inputString` matches the pattern of
334 /// this rule, and `false` otherwise. (For the definition of a string
335 /// matching the pattern of a rule, please refer to the function-level
336 /// documentation associated with the `PatternUtil::isMatch` function).
337 bool isMatch(const char *inputString) const;
338
339 /// Format this object to the specified output `stream` at the
340 /// (absolute value of) the optionally specified indentation `level`
341 /// and return a reference to `stream`. If `level` is specified,
342 /// optionally specify `spacesPerLevel`, the number of spaces per
343 /// indentation level for this and all of its nested objects. If
344 /// `level` is negative, suppress indentation of the first line. If
345 /// `spacesPerLevel` is negative, format the entire output on one line,
346 /// suppressing all but the initial indentation (as governed by
347 /// `level`). If `stream` is not valid on entry, this operation has no
348 /// effect.
349 bsl::ostream& print(bsl::ostream& stream,
350 int level = 0,
351 int spacesPerLevel = 4) const;
352};
353
354// FREE OPERATORS
355
356/// Return `true` if the specified `lhs` and `rhs` objects have the same
357/// value, and `false` otherwise. Two `Rule` objects have the same value if
358/// they have the same attributes, the same four respective threshold
359/// levels, and the same pattern.
360bool operator==(const Rule& lhs, const Rule& rhs);
361
362/// Return `true` if the specified `lhs` and `rhs` objects do not have the
363/// same value, and `false` otherwise. Two `Rule` objects do not have the
364/// same value if they have different attributes, different values for any
365/// of the four respective threshold levels, or different patterns.
366bool operator!=(const Rule& lhs, const Rule& rhs);
367
368/// Write the value of the specified `rule` to the specified `output` stream.
369/// Return the specified `output` stream.
370bsl::ostream& operator<<(bsl::ostream& output, const Rule& rule);
371
372// ============================================================================
373// INLINE DEFINITIONS
374// ============================================================================
375
376 // ----------
377 // class Rule
378 // ----------
379
380// CREATORS
381inline
383: d_pattern("", basicAllocator)
384, d_thresholds(0, 0, 0, 0)
385, d_attributeSet(basicAllocator)
386, d_hashValue(-1)
387, d_hashSize(0)
388{
389}
390
391inline
393 int recordLevel,
394 int passLevel,
395 int triggerLevel,
396 int triggerAllLevel,
397 bslma::Allocator *basicAllocator)
398: d_pattern(pattern.data(), pattern.length(), basicAllocator)
399, d_thresholds(recordLevel, passLevel, triggerLevel, triggerAllLevel)
400, d_attributeSet(basicAllocator)
401, d_hashValue(-1)
402, d_hashSize(0)
403{
404}
405
406inline
407Rule::Rule(const Rule& original, bslma::Allocator *basicAllocator)
408: d_pattern(original.d_pattern, basicAllocator)
409, d_thresholds(original.d_thresholds)
410, d_attributeSet(original.d_attributeSet, basicAllocator)
411, d_hashValue(original.d_hashValue)
412, d_hashSize(original.d_hashSize)
413{
414}
415
416// MANIPULATORS
417inline
419{
420 d_hashValue = -1;
421 return d_attributeSet.addAttribute(value);
422}
423
424inline
426{
427 return addAttribute(value);
428}
429
430inline
432{
433 d_hashValue = -1;
434 return d_attributeSet.removeAttribute(value);
435}
436
437inline
439{
440 return removeAttribute(value);
441}
442
443inline
445{
446 d_hashValue = -1;
447 d_attributeSet.removeAll();
448}
449
450inline
452{
453 removeAll();
454}
455
456inline
457int Rule::setLevels(int recordLevel,
458 int passLevel,
459 int triggerLevel,
460 int triggerAllLevel)
461{
462 d_hashValue = -1;
463 return d_thresholds.setLevels(recordLevel,
464 passLevel,
467}
468
469inline
471{
472 d_hashValue = -1;
473 d_pattern.assign(value);
474}
475
476// ACCESSORS
477inline
478bool Rule::evaluate(const AttributeContainerList& containerList) const
479{
480 return d_attributeSet.evaluate(containerList);
481}
482
483inline
485{
486 return d_attributeSet.numAttributes();
487}
488
489inline
491{
492 return numAttributes();
493}
494
495inline
496bool Rule::hasAttribute(const ManagedAttribute& value) const
497{
498 return d_attributeSet.isMember(value);
499}
500
501inline
502bool Rule::hasPredicate(const ManagedAttribute& value) const
503{
504 return hasAttribute(value);
505}
506
507inline
509{
510 return d_attributeSet.begin();
511}
512
513inline
515{
516 return d_attributeSet.end();
517}
518
519inline
521{
522 return d_thresholds.recordLevel();
523}
524
525inline
527{
528 return d_thresholds.passLevel();
529}
530
531inline
533{
534 return d_thresholds.triggerLevel();
535}
536
537inline
539{
540 return d_thresholds.triggerAllLevel();
541}
542
543inline
544const char *Rule::pattern() const
545{
546 return d_pattern.c_str();
547}
548
549inline
550bool Rule::isMatch(const char *inputString) const
551{
552 return PatternUtil::isMatch(inputString, d_pattern.c_str());
553}
554
555} // close package namespace
556
557// FREE OPERATORS
558inline
559bool ball::operator==(const Rule& lhs, const Rule& rhs)
560{
561 if (lhs.d_hashValue > 0
562 && rhs.d_hashValue > 0
563 && lhs.d_hashSize == rhs.d_hashSize
564 && lhs.d_hashValue != rhs.d_hashValue) {
565 return false; // RETURN
566 }
567
568 return lhs.d_pattern == rhs.d_pattern
569 && lhs.d_thresholds == rhs.d_thresholds
570 && lhs.d_attributeSet == rhs.d_attributeSet;
571}
572
573inline
574bool ball::operator!=(const Rule& lhs, const Rule& rhs)
575{
576 return !(lhs == rhs);
577}
578
579inline
580bsl::ostream& ball::operator<<(bsl::ostream& output, const Rule& rule)
581{
582 return rule.print(output, 0, -1);
583}
584
585
586
587#endif
588
589// ----------------------------------------------------------------------------
590// Copyright 2015 Bloomberg Finance L.P.
591//
592// Licensed under the Apache License, Version 2.0 (the "License");
593// you may not use this file except in compliance with the License.
594// You may obtain a copy of the License at
595//
596// http://www.apache.org/licenses/LICENSE-2.0
597//
598// Unless required by applicable law or agreed to in writing, software
599// distributed under the License is distributed on an "AS IS" BASIS,
600// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
601// See the License for the specific language governing permissions and
602// limitations under the License.
603// ----------------------------- END-OF-FILE ----------------------------------
604
605/** @} */
606/** @} */
607/** @} */
Definition ball_attributecontainerlist.h:271
Definition ball_managedattributeset.h:128
int numAttributes() const
Return the number of attributes managed by this object.
Definition ball_managedattributeset.h:394
bool removeAttribute(const ManagedAttribute &value)
Definition ball_managedattributeset.h:356
const_iterator end() const
Definition ball_managedattributeset.h:412
const_iterator begin() const
Return an iterator referring to the first member of this attribute set.
Definition ball_managedattributeset.h:406
SetType::const_iterator const_iterator
Definition ball_managedattributeset.h:172
bool addAttribute(const ManagedAttribute &value)
Definition ball_managedattributeset.h:344
void removeAll()
Remove all attributes from this attribute set.
Definition ball_managedattributeset.h:368
bool isMember(const ManagedAttribute &value) const
Definition ball_managedattributeset.h:388
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:538
bool evaluate(const AttributeContainerList &containerList) const
Definition ball_rule.h:478
ManagedAttributeSet::const_iterator end() const
Definition ball_rule.h:514
void removeAllPredicates()
Definition ball_rule.h:451
friend bool operator!=(const Rule &, const Rule &)
int addAttribute(const ManagedAttribute &value)
Definition ball_rule.h:418
int numAttributes() const
Return the number of attributes managed by this object.
Definition ball_rule.h:484
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
bool hasAttribute(const ManagedAttribute &value) const
Definition ball_rule.h:496
int removeAttribute(const ManagedAttribute &value)
Definition ball_rule.h:431
bool hasPredicate(const Predicate &value) const
Definition ball_rule.h:502
friend bsl::ostream & operator<<(bsl::ostream &, const Rule &)
int recordLevel() const
Return the record level of this object.
Definition ball_rule.h:520
void setPattern(const bsl::string_view &value)
Set the pattern of this object to the specified value.
Definition ball_rule.h:470
int setLevels(int recordLevel, int passLevel, int triggerLevel, int triggerAllLevel)
Definition ball_rule.h:457
const char * pattern() const
Return the pattern of this object.
Definition ball_rule.h:544
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:490
ManagedAttributeSet::const_iterator begin() const
Definition ball_rule.h:508
BSLMF_NESTED_TRAIT_DECLARATION(Rule, bslma::UsesBslmaAllocator)
int passLevel() const
Return the pass level of this object.
Definition ball_rule.h:526
void removeAll()
Remove all attributes from this rule.
Definition ball_rule.h:444
int triggerLevel() const
Return the trigger level of this object.
Definition ball_rule.h:532
Rule(bslma::Allocator *basicAllocator=0)
Definition ball_rule.h:382
friend bool operator==(const Rule &, const Rule &)
static int hash(const Rule &rule, int size)
int addPredicate(const ManagedAttribute &value)
Definition ball_rule.h:425
int removePredicate(const ManagedAttribute &value)
Definition ball_rule.h:438
bool isMatch(const char *inputString) const
Definition ball_rule.h:550
Definition ball_thresholdaggregate.h:101
int triggerLevel() const
Return the trigger level of this threshold aggregate.
Definition ball_thresholdaggregate.h:290
int recordLevel() const
Return the record level of this threshold aggregate.
Definition ball_thresholdaggregate.h:278
int passLevel() const
Return the pass level of this threshold aggregate.
Definition ball_thresholdaggregate.h:284
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:296
Definition bslstl_stringview.h:471
Definition bslstl_string.h:1252
const CHAR_TYPE * c_str() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:7405
basic_string & assign(const basic_string &replacement)
Definition bslstl_string.h:6347
Definition bslma_allocator.h:545
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
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)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
static bool isMatch(const char *inputString, const char *pattern)
Definition bslma_usesbslmaallocator.h:344