BDE 4.14.0 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 // NOT IMPLEMENTED
130
131 public:
132 // TYPES
134
135 // TRAITS
138
139 // CREATORS
140
142 const char *name,
143 const bsl::string_view& value,
144 const allocator_type& allocator = allocator_type());
146 const char *name,
147 const char *value,
148 const allocator_type& allocator = allocator_type());
150 const char *name,
151 int value,
152 const allocator_type& allocator = allocator_type());
154 const char *name,
155 long value,
156 const allocator_type& allocator = allocator_type());
158 const char *name,
159 long long value,
160 const allocator_type& allocator = allocator_type());
162 const char *name,
163 unsigned int value,
164 const allocator_type& allocator = allocator_type());
166 const char *name,
167 unsigned long value,
168 const allocator_type& allocator = allocator_type());
170 const char *name,
171 unsigned long long value,
172 const allocator_type& allocator = allocator_type());
174 const char *name,
175 bdlb::Guid value,
176 const allocator_type& allocator = allocator_type());
177 /// Create a BALL attribute container holding a single rule, associating
178 /// the specified `name` with the specified `value`. Optionally specify
179 /// an `allocator` (e.g., the address of a `bslma::Allocator` object) to
180 /// supply memory; otherwise, the default allocator is used.
182 const char *name,
183 const void *value,
184 const allocator_type& allocator = allocator_type());
185
186 /// Destroy this object.
188
189 // ACCESSORS
190
191 /// Return `true` if the specified `attribute` is the same as the value
192 /// held in this container, and `false` otherwise.
193 bool hasValue(const Attribute& attribute) const BSLS_KEYWORD_OVERRIDE;
194
195 /// Format this object to the specified output `stream` at the (absolute
196 /// value of) the optionally specified indentation `level` and return a
197 /// reference to `stream`. If `level` is specified, optionally specify
198 /// `spacesPerLevel`, the number of spaces per indentation level for
199 /// this and all of its nested objects. If `level` is negative,
200 /// suppress indentation of the first line. If `spacesPerLevel` is
201 /// negative, format the entire output on one line, suppressing all but
202 /// the initial indentation (as governed by `level`). If `stream` is
203 /// not valid on entry, this operation has no effect.
204 bsl::ostream& print(bsl::ostream& stream,
205 int level = 0,
206 int spacesPerLevel = 4)
208
209 /// Invoke the specified `visitor` function for all attributes in this
210 /// container.
211 void visitAttributes(const bsl::function<void(const Attribute&)>& visitor)
213
214 // Aspects
215
216 /// Return the allocator used by this object to supply memory. Note
217 /// that if no allocator was supplied at construction the default
218 /// allocator in effect at construction is used.
220};
221
222 // =====================
223 // class ScopedAttribute
224 // =====================
225
226/// This class provides a scoped guard that sets a single BALL attribute in
227/// the current thread.
228///
229/// See @ref ball_scopedattribute
231
232 // DATA
233 ScopedAttribute_Container d_container; // contains the attribute
234
235 const AttributeContext::iterator d_it; // reference to attribute
236 // container
237
238 // NOT IMPLEMENTED
240 ScopedAttribute& operator=(const ScopedAttribute&);
241
242 public:
243 // TYPES
245
246 // TRAITS
249
250 // CREATORS
251
252 ScopedAttribute(const char *name,
253 const bsl::string_view& value,
254 const allocator_type& allocator = allocator_type());
255 ScopedAttribute(const char *name,
256 const char *value,
257 const allocator_type& allocator = allocator_type());
258 ScopedAttribute(const char *name,
259 int value,
260 const allocator_type& allocator = allocator_type());
261 ScopedAttribute(const char *name,
262 long value,
263 const allocator_type& allocator = allocator_type());
264 ScopedAttribute(const char *name,
265 long long value,
266 const allocator_type& allocator = allocator_type());
267 ScopedAttribute(const char *name,
268 unsigned int value,
269 const allocator_type& allocator = allocator_type());
270 ScopedAttribute(const char *name,
271 unsigned long value,
272 const allocator_type& allocator = allocator_type());
273 ScopedAttribute(const char *name,
274 unsigned long long value,
275 const allocator_type& allocator = allocator_type());
276 ScopedAttribute(const char *name,
277 bdlb::Guid value,
278 const allocator_type& allocator = allocator_type());
279 /// Set BALL logging attributes for the current thread for the scope of
280 /// this object, associating the specified `name` with the specified
281 /// `value`. Optionally specify an `allocator` (e.g., the address of a
282 /// `bslma::Allocator` object) to supply memory; otherwise, the default
283 /// allocator is used.
284 ScopedAttribute(const char *name,
285 const void *value,
286 const allocator_type& allocator = allocator_type());
287
288 /// Remove the attributes managed by this object from the BALL system,
289 /// and destroy this object.
291
292 // Aspects
293
294 /// Return the allocator used by this object to supply memory. Note
295 /// that if no allocator was supplied at construction the default
296 /// allocator in effect at construction is used.
298};
299
300// ============================================================================
301// INLINE DEFINITIONS
302// ============================================================================
303
304 // -------------------------------
305 // class ScopedAttribute_Container
306 // -------------------------------
307
308// CREATORS
309inline
310ScopedAttribute_Container::ScopedAttribute_Container(
311 const char *name,
312 const bsl::string_view& value,
313 const allocator_type& allocator)
314: d_attribute(name, value, allocator)
315{
316}
317
318inline
319ScopedAttribute_Container::ScopedAttribute_Container(
320 const char *name,
321 const char *value,
322 const allocator_type& allocator)
323: d_attribute(name, static_cast<bsl::string>(value), allocator)
324{
325}
326
327inline
328ScopedAttribute_Container::ScopedAttribute_Container(
329 const char *name,
330 int value,
331 const allocator_type& allocator)
332: d_attribute(name, value, allocator)
333{
334}
335
336inline
337ScopedAttribute_Container::ScopedAttribute_Container(
338 const char *name,
339 long value,
340 const allocator_type& allocator)
341: d_attribute(name, value, allocator)
342{
343}
344
345inline
346ScopedAttribute_Container::ScopedAttribute_Container(
347 const char *name,
348 long long value,
349 const allocator_type& allocator)
350: d_attribute(name, value, allocator)
351{
352}
353
354inline
355ScopedAttribute_Container::ScopedAttribute_Container(
356 const char *name,
357 unsigned int value,
358 const allocator_type& allocator)
359: d_attribute(name, value, allocator)
360{
361}
362
363inline
364ScopedAttribute_Container::ScopedAttribute_Container(
365 const char *name,
366 unsigned long value,
367 const allocator_type& allocator)
368: d_attribute(name, value, allocator)
369{
370}
371
372inline
373ScopedAttribute_Container::ScopedAttribute_Container(
374 const char *name,
375 unsigned long long value,
376 const allocator_type& allocator)
377: d_attribute(name, value, allocator)
378{
379}
380
381inline
382ScopedAttribute_Container::ScopedAttribute_Container(
383 const char *name,
384 bdlb::Guid value,
385 const allocator_type& allocator)
386: d_attribute(name, value, allocator)
387{
388}
389
390inline
391ScopedAttribute_Container::ScopedAttribute_Container(
392 const char *name,
393 const void *value,
394 const allocator_type& allocator)
395: d_attribute(name, value, allocator)
396{
397}
398
399// ACCESSORS
400inline
402{
403 return d_attribute == attribute;
404}
405
406inline
408 const bsl::function<void(const ball::Attribute&)>& visitor) const
409{
410 visitor(d_attribute);
411}
412
413 // Aspects
414
415inline
418{
419 return d_attribute.get_allocator();
420}
421
422 // ---------------------
423 // class ScopedAttribute
424 // ---------------------
425
426// CREATORS
427inline
428ScopedAttribute::ScopedAttribute(const char *name,
429 const bsl::string_view& value,
430 const allocator_type& allocator)
431: d_container(name, value, allocator)
432, d_it(AttributeContext::getContext()->addAttributes(&d_container))
433{
434}
435
436inline
437ScopedAttribute::ScopedAttribute(const char *name,
438 const char *value,
439 const allocator_type& allocator)
440: d_container(name, value, allocator)
441, d_it(AttributeContext::getContext()->addAttributes(&d_container))
442{
443}
444
445inline
446ScopedAttribute::ScopedAttribute(const char *name,
447 int value,
448 const allocator_type& allocator)
449: d_container(name, value, allocator)
450, d_it(AttributeContext::getContext()->addAttributes(&d_container))
451{
452}
453
454inline
455ScopedAttribute::ScopedAttribute(const char *name,
456 long value,
457 const allocator_type& allocator)
458: d_container(name, value, allocator)
459, d_it(AttributeContext::getContext()->addAttributes(&d_container))
460{
461}
462
463inline
464ScopedAttribute::ScopedAttribute(const char *name,
465 long long value,
466 const allocator_type& allocator)
467: d_container(name, value, allocator)
468, d_it(AttributeContext::getContext()->addAttributes(&d_container))
469{
470}
471
472inline
473ScopedAttribute::ScopedAttribute(const char *name,
474 unsigned int value,
475 const allocator_type& allocator)
476: d_container(name, value, allocator)
477, d_it(AttributeContext::getContext()->addAttributes(&d_container))
478{
479}
480
481inline
482ScopedAttribute::ScopedAttribute(const char *name,
483 unsigned long value,
484 const allocator_type& allocator)
485: d_container(name, value, allocator)
486, d_it(AttributeContext::getContext()->addAttributes(&d_container))
487{
488}
489
490inline
491ScopedAttribute::ScopedAttribute(const char *name,
492 unsigned long long value,
493 const allocator_type& allocator)
494: d_container(name, value, allocator)
495, d_it(AttributeContext::getContext()->addAttributes(&d_container))
496{
497}
498
499inline
500ScopedAttribute::ScopedAttribute(const char *name,
501 bdlb::Guid value,
502 const allocator_type& allocator)
503: d_container(name, value, allocator)
504, d_it(AttributeContext::getContext()->addAttributes(&d_container))
505{
506}
507
508inline
509ScopedAttribute::ScopedAttribute(const char *name,
510 const void *value,
511 const allocator_type& allocator)
512: d_container(name, value, allocator)
513, d_it(AttributeContext::getContext()->addAttributes(&d_container))
514{
515}
516
517inline
522
523// ACCESSORS
524
525 // Aspects
526
527inline
530{
531 return d_container.get_allocator();
532}
533
534} // close package namespace
535
536
537#endif
538
539// ----------------------------------------------------------------------------
540// Copyright 2015 Bloomberg Finance L.P.
541//
542// Licensed under the Apache License, Version 2.0 (the "License");
543// you may not use this file except in compliance with the License.
544// You may obtain a copy of the License at
545//
546// http://www.apache.org/licenses/LICENSE-2.0
547//
548// Unless required by applicable law or agreed to in writing, software
549// distributed under the License is distributed on an "AS IS" BASIS,
550// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
551// See the License for the specific language governing permissions and
552// limitations under the License.
553// ----------------------------- END-OF-FILE ----------------------------------
554
555/** @} */
556/** @} */
557/** @} */
Definition ball_attributecontainerlist.h:168
Definition ball_attributecontainer.h:426
Definition ball_attributecontext.h:520
static AttributeContext * getContext()
void removeAttributes(iterator element)
Definition ball_attributecontext.h:811
Definition ball_attribute.h:198
allocator_type get_allocator() const
Definition ball_attribute.h:657
Definition ball_scopedattribute.h:122
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const BSLS_KEYWORD_OVERRIDE
~ScopedAttribute_Container() BSLS_KEYWORD_OVERRIDE
Destroy this object.
bsl::allocator< char > allocator_type
Definition ball_scopedattribute.h:133
void visitAttributes(const bsl::function< void(const Attribute &)> &visitor) const BSLS_KEYWORD_OVERRIDE
Definition ball_scopedattribute.h:407
BSLMF_NESTED_TRAIT_DECLARATION(ScopedAttribute_Container, bslma::UsesBslmaAllocator)
bool hasValue(const Attribute &attribute) const BSLS_KEYWORD_OVERRIDE
Definition ball_scopedattribute.h:401
allocator_type get_allocator() const
Definition ball_scopedattribute.h:417
Definition ball_scopedattribute.h:230
~ScopedAttribute()
Definition ball_scopedattribute.h:518
allocator_type get_allocator() const
Definition ball_scopedattribute.h:529
bsl::allocator< char > allocator_type
Definition ball_scopedattribute.h:244
BSLMF_NESTED_TRAIT_DECLARATION(ScopedAttribute, bslma::UsesBslmaAllocator)
Definition bdlb_guid.h:201
Definition bslma_bslallocator.h:580
Definition bslstl_stringview.h:441
Forward declaration.
Definition bslstl_function.h:934
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:653
Definition ball_administration.h:214
Definition bdlb_printmethods.h:283
Definition bslma_usesbslmaallocator.h:343