BDE 4.39.x Production Release
Loading...
Searching...
No Matches
ball_scopedattribute.h
Go to the documentation of this file.
1/// @file ball_scopedattribute.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_scopedattribute.h -*-C++-*-
8#ifndef INCLUDED_BALL_SCOPEDATTRIBUTE
9#define INCLUDED_BALL_SCOPEDATTRIBUTE
10
11/// @defgroup ball_scopedattribute ball_scopedattribute
12/// @brief Provide a scoped guard for a single BALL attribute.
13/// @addtogroup bal
14/// @{
15/// @addtogroup ball
16/// @{
17/// @addtogroup ball_scopedattribute
18/// @{
19///
20/// <h1> Outline </h1>
21/// * <a href="#ball_scopedattribute-purpose"> Purpose</a>
22/// * <a href="#ball_scopedattribute-classes"> Classes </a>
23/// * <a href="#ball_scopedattribute-description"> Description </a>
24/// * <a href="#ball_scopedattribute-usage"> Usage </a>
25/// * <a href="#ball_scopedattribute-example-1-basic-usage-of-ball-scopedattribute"> Example 1: Basic Usage of ball::ScopedAttribute </a>
26///
27/// # Purpose {#ball_scopedattribute-purpose}
28/// Provide a scoped guard for a single BALL attribute.
29///
30/// # Classes {#ball_scopedattribute-classes}
31///
32/// - ball::ScopedAttribute: single attribute scoped guard
33///
34/// @see ball_attribute
35///
36/// # Description {#ball_scopedattribute-description}
37/// This component defines a type, `ball::ScopedAttribute`, that
38/// serves as a scoped guard for `ball::Attribute` objects. It defines a single
39/// attribute for the current thread while it is in scope.
40///
41/// This component is used to associate an attribute (a name-value pair) with
42/// the current thread context for use when writing log records for the current
43/// thread. This context information can both be written to the log itself, and
44/// used as input when evaluating whether a particular log should be written.
45/// For more information on how to use this feature, please see the package
46/// level documentation and usage examples for "Log Attributes" and "Rule-Based
47/// Logging".
48///
49/// ## Usage {#ball_scopedattribute-usage}
50///
51///
52/// This section illustrates intended use of this component.
53///
54/// ### Example 1: Basic Usage of ball::ScopedAttribute {#ball_scopedattribute-example-1-basic-usage-of-ball-scopedattribute}
55///
56///
57/// Suppose that service requests for a fictional service with id `999` are
58/// handled asynchronously by the function below. Creating an instance of this
59/// class will set BALL attributes for any logging performed while the request
60/// is being processed:
61/// @code
62/// void handleServiceRequest(const Request& request)
63/// {
64/// BALL_LOG_SET_CATEGORY("MY.SERVICE");
65///
66/// ball::ScopedAttribute attribute("request", request.selectionName());
67///
68/// BALL_LOG_TRACE << "Handling request: " << request;
69///
70/// // handle request here
71/// }
72/// @endcode
73/// Attribute "request" will be set in the calling thread and will affect
74/// publication of any BALL messages for the lifetime of `attribute`.
75/// @}
76/** @} */
77/** @} */
78
79/** @addtogroup bal
80 * @{
81 */
82/** @addtogroup ball
83 * @{
84 */
85/** @addtogroup ball_scopedattribute
86 * @{
87 */
88
89#include <balscm_version.h>
90
91#include <ball_attribute.h>
95
96#include <bdlb_guid.h>
97
98#include <bslma_allocator.h>
99#include <bslma_bslallocator.h>
101
103
104#include <bsls_keyword.h>
105#include <bsls_types.h>
106#include <bsls_assert.h>
107
108#include <bsl_iosfwd.h>
109#include <bsl_string.h>
110
111
112namespace ball {
113
114 // ===============================
115 // class ScopedAttribute_Container
116 // ===============================
117
118/// This component-private class is a concrete implementation of the
119/// `AttributeContainer` protocol for a single attribute.
120///
121/// See @ref ball_scopedattribute
123
124 // DATA
125 Attribute d_attribute;
126
127 private:
128 // NOT IMPLEMENTED
131
132 public:
133 // TYPES
135
136 // TRAITS
139
140 // CREATORS
141
142 /// Create a BALL attribute container holding a single rule, associating
143 /// the specified `name` with the specified `value`. Optionally specify
144 /// an `allocator` (e.g., the address of a `bslma::Allocator` object) to
145 /// supply memory; otherwise, the default allocator is used.
147 const char *name,
148 const bsl::string_view& value,
149 const allocator_type& allocator = allocator_type());
151 const char *name,
152 const char *value,
153 const allocator_type& allocator = allocator_type());
155 const char *name,
156 int value,
157 const allocator_type& allocator = allocator_type());
159 const char *name,
160 long value,
161 const allocator_type& allocator = allocator_type());
163 const char *name,
164 long long value,
165 const allocator_type& allocator = allocator_type());
167 const char *name,
168 unsigned int value,
169 const allocator_type& allocator = allocator_type());
171 const char *name,
172 unsigned long value,
173 const allocator_type& allocator = allocator_type());
175 const char *name,
176 unsigned long long value,
177 const allocator_type& allocator = allocator_type());
179 const char *name,
180 bdlb::Guid value,
181 const allocator_type& allocator = allocator_type());
183 const char *name,
184 const void *value,
185 const allocator_type& allocator = allocator_type());
186
187 /// Destroy this object.
189
190 // ACCESSORS
191
192 /// Return `true` if the specified `attribute` is the same as the value
193 /// held in this container, and `false` otherwise.
194 bool hasValue(const Attribute& attribute) const BSLS_KEYWORD_OVERRIDE;
195
196 /// Format this object to the specified output `stream` at the (absolute
197 /// value of) the optionally specified indentation `level` and return a
198 /// reference to `stream`. If `level` is specified, optionally specify
199 /// `spacesPerLevel`, the number of spaces per indentation level for
200 /// this and all of its nested objects. If `level` is negative,
201 /// suppress indentation of the first line. If `spacesPerLevel` is
202 /// negative, format the entire output on one line, suppressing all but
203 /// the initial indentation (as governed by `level`). If `stream` is
204 /// not valid on entry, this operation has no effect.
205 bsl::ostream& print(bsl::ostream& stream,
206 int level = 0,
207 int spacesPerLevel = 4)
209
210 /// Invoke the specified `visitor` function for all attributes in this
211 /// container.
212 void visitAttributes(const bsl::function<void(const Attribute&)>& visitor)
214
215 // Aspects
216
217 /// Return the allocator used by this object to supply memory.
218 ///
219 /// \note Note that if no allocator was supplied at construction the default
220 /// allocator in effect at construction is used.
222};
223
224 // =====================
225 // class ScopedAttribute
226 // =====================
227
228/// This class provides a scoped guard that sets a single BALL attribute in
229/// the current thread.
230///
231/// See @ref ball_scopedattribute
233
234 // DATA
235 ScopedAttribute_Container d_container; // contains the attribute
236
237 const AttributeContext::iterator d_it; // reference to attribute
238 // container
239
240 private:
241 // NOT IMPLEMENTED
243 ScopedAttribute& operator=(const ScopedAttribute&);
244
245 public:
246 // TYPES
248
249 // TRAITS
252
253 // CREATORS
254
255 /// Set BALL logging attributes for the current thread for the scope of
256 /// this object, associating the specified `name` with the specified
257 /// `value`. Optionally specify an `allocator` (e.g., the address of a
258 /// `bslma::Allocator` object) to supply memory; otherwise, the default
259 /// allocator is used.
260 ScopedAttribute(const char *name,
261 const bsl::string_view& value,
262 const allocator_type& allocator = allocator_type());
263 ScopedAttribute(const char *name,
264 const char *value,
265 const allocator_type& allocator = allocator_type());
266 ScopedAttribute(const char *name,
267 int value,
268 const allocator_type& allocator = allocator_type());
269 ScopedAttribute(const char *name,
270 long value,
271 const allocator_type& allocator = allocator_type());
272 ScopedAttribute(const char *name,
273 long long value,
274 const allocator_type& allocator = allocator_type());
275 ScopedAttribute(const char *name,
276 unsigned int value,
277 const allocator_type& allocator = allocator_type());
278 ScopedAttribute(const char *name,
279 unsigned long value,
280 const allocator_type& allocator = allocator_type());
281 ScopedAttribute(const char *name,
282 unsigned long long value,
283 const allocator_type& allocator = allocator_type());
284 ScopedAttribute(const char *name,
285 bdlb::Guid value,
286 const allocator_type& allocator = allocator_type());
287 ScopedAttribute(const char *name,
288 const void *value,
289 const allocator_type& allocator = allocator_type());
290
291 /// Remove the attributes managed by this object from the BALL system,
292 /// and destroy this object.
294
295 // Aspects
296
297 /// Return the allocator used by this object to supply memory.
298 ///
299 /// \note Note that if no allocator was supplied at construction the default
300 /// allocator in effect at construction is used.
302};
303
304// ============================================================================
305// INLINE DEFINITIONS
306// ============================================================================
307
308 // -------------------------------
309 // class ScopedAttribute_Container
310 // -------------------------------
311
312// CREATORS
313inline
314ScopedAttribute_Container::ScopedAttribute_Container(
315 const char *name,
316 const bsl::string_view& value,
317 const allocator_type& allocator)
318: d_attribute(name, value, allocator)
319{
320}
321
322inline
323ScopedAttribute_Container::ScopedAttribute_Container(
324 const char *name,
325 const char *value,
326 const allocator_type& allocator)
327: d_attribute(name, static_cast<bsl::string>(value), allocator)
328{
329}
330
331inline
332ScopedAttribute_Container::ScopedAttribute_Container(
333 const char *name,
334 int value,
335 const allocator_type& allocator)
336: d_attribute(name, value, allocator)
337{
338}
339
340inline
341ScopedAttribute_Container::ScopedAttribute_Container(
342 const char *name,
343 long value,
344 const allocator_type& allocator)
345: d_attribute(name, value, allocator)
346{
347}
348
349inline
350ScopedAttribute_Container::ScopedAttribute_Container(
351 const char *name,
352 long long value,
353 const allocator_type& allocator)
354: d_attribute(name, value, allocator)
355{
356}
357
358inline
359ScopedAttribute_Container::ScopedAttribute_Container(
360 const char *name,
361 unsigned int value,
362 const allocator_type& allocator)
363: d_attribute(name, value, allocator)
364{
365}
366
367inline
368ScopedAttribute_Container::ScopedAttribute_Container(
369 const char *name,
370 unsigned long value,
371 const allocator_type& allocator)
372: d_attribute(name, value, allocator)
373{
374}
375
376inline
377ScopedAttribute_Container::ScopedAttribute_Container(
378 const char *name,
379 unsigned long long value,
380 const allocator_type& allocator)
381: d_attribute(name, value, allocator)
382{
383}
384
385inline
386ScopedAttribute_Container::ScopedAttribute_Container(
387 const char *name,
388 bdlb::Guid value,
389 const allocator_type& allocator)
390: d_attribute(name, value, allocator)
391{
392}
393
394inline
395ScopedAttribute_Container::ScopedAttribute_Container(
396 const char *name,
397 const void *value,
398 const allocator_type& allocator)
399: d_attribute(name, value, allocator)
400{
401}
402
403// ACCESSORS
404inline
406{
407 return d_attribute == attribute;
408}
409
410inline
412 const bsl::function<void(const ball::Attribute&)>& visitor) const
413{
414 visitor(d_attribute);
415}
416
417 // Aspects
418
419inline
422{
423 return d_attribute.get_allocator();
424}
425
426 // ---------------------
427 // class ScopedAttribute
428 // ---------------------
429
430// CREATORS
431inline
432ScopedAttribute::ScopedAttribute(const char *name,
433 const bsl::string_view& value,
434 const allocator_type& allocator)
435: d_container(name, value, allocator)
436, d_it(AttributeContext::getContext()->addAttributes(&d_container))
437{
438}
439
440inline
441ScopedAttribute::ScopedAttribute(const char *name,
442 const char *value,
443 const allocator_type& allocator)
444: d_container(name, value, allocator)
445, d_it(AttributeContext::getContext()->addAttributes(&d_container))
446{
447}
448
449inline
450ScopedAttribute::ScopedAttribute(const char *name,
451 int value,
452 const allocator_type& allocator)
453: d_container(name, value, allocator)
454, d_it(AttributeContext::getContext()->addAttributes(&d_container))
455{
456}
457
458inline
459ScopedAttribute::ScopedAttribute(const char *name,
460 long value,
461 const allocator_type& allocator)
462: d_container(name, value, allocator)
463, d_it(AttributeContext::getContext()->addAttributes(&d_container))
464{
465}
466
467inline
468ScopedAttribute::ScopedAttribute(const char *name,
469 long long value,
470 const allocator_type& allocator)
471: d_container(name, value, allocator)
472, d_it(AttributeContext::getContext()->addAttributes(&d_container))
473{
474}
475
476inline
477ScopedAttribute::ScopedAttribute(const char *name,
478 unsigned int value,
479 const allocator_type& allocator)
480: d_container(name, value, allocator)
481, d_it(AttributeContext::getContext()->addAttributes(&d_container))
482{
483}
484
485inline
486ScopedAttribute::ScopedAttribute(const char *name,
487 unsigned long value,
488 const allocator_type& allocator)
489: d_container(name, value, allocator)
490, d_it(AttributeContext::getContext()->addAttributes(&d_container))
491{
492}
493
494inline
495ScopedAttribute::ScopedAttribute(const char *name,
496 unsigned long long value,
497 const allocator_type& allocator)
498: d_container(name, value, allocator)
499, d_it(AttributeContext::getContext()->addAttributes(&d_container))
500{
501}
502
503inline
504ScopedAttribute::ScopedAttribute(const char *name,
505 bdlb::Guid value,
506 const allocator_type& allocator)
507: d_container(name, value, allocator)
508, d_it(AttributeContext::getContext()->addAttributes(&d_container))
509{
510}
511
512inline
513ScopedAttribute::ScopedAttribute(const char *name,
514 const void *value,
515 const allocator_type& allocator)
516: d_container(name, value, allocator)
517, d_it(AttributeContext::getContext()->addAttributes(&d_container))
518{
519}
520
521inline
526
527// ACCESSORS
528
529 // Aspects
530
531inline
534{
535 return d_container.get_allocator();
536}
537
538} // close package namespace
539
540
541#endif
542
543// ----------------------------------------------------------------------------
544// Copyright 2015 Bloomberg Finance L.P.
545//
546// Licensed under the Apache License, Version 2.0 (the "License");
547// you may not use this file except in compliance with the License.
548// You may obtain a copy of the License at
549//
550// http://www.apache.org/licenses/LICENSE-2.0
551//
552// Unless required by applicable law or agreed to in writing, software
553// distributed under the License is distributed on an "AS IS" BASIS,
554// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
555// See the License for the specific language governing permissions and
556// limitations under the License.
557// ----------------------------- END-OF-FILE ----------------------------------
558
559/** @} */
560/** @} */
561/** @} */
Definition ball_attributecontainerlist.h:168
Definition ball_attributecontainer.h:426
Definition ball_attributecontext.h:526
static AttributeContext * getContext()
void removeAttributes(iterator element)
Definition ball_attributecontext.h:834
Definition ball_attribute.h:199
allocator_type get_allocator() const
Definition ball_attribute.h:659
Definition ball_scopedattribute.h:122
~ScopedAttribute_Container() BSLS_KEYWORD_OVERRIDE
Destroy this object.
bsl::allocator< char > allocator_type
Definition ball_scopedattribute.h:134
void visitAttributes(const bsl::function< void(const Attribute &)> &visitor) const BSLS_KEYWORD_OVERRIDE
Definition ball_scopedattribute.h:411
BSLMF_NESTED_TRAIT_DECLARATION(ScopedAttribute_Container, bslma::UsesBslmaAllocator)
bool hasValue(const Attribute &attribute) const BSLS_KEYWORD_OVERRIDE
Definition ball_scopedattribute.h:405
allocator_type get_allocator() const
Definition ball_scopedattribute.h:421
Definition ball_scopedattribute.h:232
~ScopedAttribute()
Definition ball_scopedattribute.h:522
allocator_type get_allocator() const
Definition ball_scopedattribute.h:533
bsl::allocator< char > allocator_type
Definition ball_scopedattribute.h:247
BSLMF_NESTED_TRAIT_DECLARATION(ScopedAttribute, bslma::UsesBslmaAllocator)
Definition bdlb_guid.h:201
Definition bslma_bslallocator.h:588
Definition bslstl_stringview.h:471
Forward declaration.
Definition bslstl_function.h:946
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:695
Definition ball_administration.h:214
Definition bdlat_valuetypefunctions.h:939
Definition bslma_usesbslmaallocator.h:344