BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball_record.h
Go to the documentation of this file.
1/// @file ball_record.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_record.h -*-C++-*-
8#ifndef INCLUDED_BALL_RECORD
9#define INCLUDED_BALL_RECORD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_record ball_record
15/// @brief Provide a container for the fields and attributes of a log record.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_record
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_record-purpose"> Purpose</a>
25/// * <a href="#ball_record-classes"> Classes </a>
26/// * <a href="#ball_record-description"> Description </a>
27/// * <a href="#ball_record-attributes"> Attributes </a>
28/// * <a href="#ball_record-usage"> Usage </a>
29/// * <a href="#ball_record-example-1-basic-use-of-ball-record"> Example 1: Basic Use of ball::Record </a>
30///
31/// # Purpose {#ball_record-purpose}
32/// Provide a container for the fields and attributes of a log record.
33///
34/// # Classes {#ball_record-classes}
35///
36/// - ball::Record: container for a log record's fields and attributes
37///
38/// @see ball_recordattributes, ball_logger
39///
40/// # Description {#ball_record-description}
41/// This component provides a single, unconstrained
42/// (value-semantic) attribute class, `ball::Record`, that is used to describe
43/// the properties of a logged message.
44///
45/// ## Attributes {#ball_record-attributes}
46///
47///
48/// @code
49/// Name Type
50/// ------------------ -----------------------------------
51/// fixedFields ball::RecordAttributes
52/// userFields ball::UserFields
53/// attributes bsl::vector<ball::ManagedAttribute>
54/// @endcode
55/// * `fixedFields`: mandatory log fields including timestamp, location,
56/// severity, process id, and the log message.
57/// * `userFields`: user-managed fields associated with a log record. Note
58/// that use of these fields is deprecated and superseded by `attributes`.
59/// * `attributes`: user-managed name/value pairs associated with a log record.
60///
61/// `ball::Record` aggregates a set of fixed fields and various user-defined
62/// fields and attributes into one record type, useful for transmitting a
63/// customized log record as a single instance rather than passing around
64/// individual attributes separately. Note that this class is a pure attribute
65/// class with no constraints, other than the total memory required for the
66/// class. Also note that this class is not thread-safe.
67///
68/// ## Usage {#ball_record-usage}
69///
70///
71/// This section illustrates intended use of this component.
72///
73/// ### Example 1: Basic Use of ball::Record {#ball_record-example-1-basic-use-of-ball-record}
74///
75///
76/// The following example demonstrates how to create and set the properties of
77/// a `ball::Record`. Note that users of the `ball` logging subsystem are not
78/// expected to create records directly.
79///
80/// First we default create a `ball::Record`, `record`, and verify it has a
81/// default set of attributes:
82/// @code
83/// ball::Record record;
84///
85/// assert(ball::RecordAttributes() == record.fixedFields());
86/// assert(0 == record.customFields().length());
87/// @endcode
88/// Then, we set the fixed fields of the record to contain a simple message:
89/// @code
90/// int processId = bdls::ProcessUtil::getProcessId();
91/// bsls::Types::Uint64 threadId = bslmt::ThreadUtil::selfIdAsUint64();
92///
93/// ball::RecordAttributes attributes(bdlt::CurrentTime::utc(), // time stamp
94/// processId, // process id
95/// threadId, // thread id
96/// __FILE__, // filename
97/// __LINE__, // line number
98/// "ExampleCategory", // category
99/// ball::Severity::e_WARN, // severity
100/// "Simple Test Message"); // message
101/// record.setFixedFields(attributes);
102///
103/// assert(attributes == record.fixedFields());
104/// @endcode
105/// Next, we add an additional attribute to the log record:
106/// @code
107/// record.addAttribute(ball::Attribute("myLib.name", "John Smith"));
108/// @endcode
109/// Finally, we write the record to a stream:
110/// @code
111/// bsl::ostringstream output;
112/// output << record << bsl::endl;
113/// @endcode
114/// @}
115/** @} */
116/** @} */
117
118/** @addtogroup bal
119 * @{
120 */
121/** @addtogroup ball
122 * @{
123 */
124/** @addtogroup ball_record
125 * @{
126 */
127
128#include <balscm_version.h>
129
133#include <ball_userfields.h>
134
135#include <bslma_allocator.h>
136#include <bslma_default.h>
138
140
141#include <bsls_alignment.h>
142#include <bsls_assert.h>
143
144#include <bsl_iosfwd.h>
145#include <bsl_vector.h>
146
147#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
148#include <bslalg_typetraits.h>
149#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
150
151
152namespace ball {
153
154 // ============
155 // class Record
156 // ============
157
158/// This class provides a container for a set of fields that are appropriate
159/// for a user-configurable log record. The class contains a
160/// `RecordAttributes` object that in turn holds a fixed set of fields, a
161/// `ball::UserFields` object that holds a set of optional, user-defined
162/// fields, and a set of attributes associated with this log record. For
163/// each of these three sub-containers there is an accessor for obtaining
164/// the container value and a manipulator for changing that value.
165///
166/// Additionally, this class supports a complete set of *value* *semantic*
167/// operations, including copy construction, assignment and equality
168/// comparison, and `ostream` printing. A precise operational definition of
169/// when two instances have the same value can be found in the description
170/// of `operator==` for the class. This class is *exception* *neutral* with
171/// no guarantee of rollback: If an exception is thrown during the
172/// invocation of a method on a pre-existing instance, the object is left in
173/// a valid state, but its value is undefined. In no event is memory
174/// leaked. Finally, **aliasing** (e.g., using all or part of an object as
175/// both source and destination) is supported in all cases.
176///
177/// See @ref ball_record
178class Record {
179
180 private:
181 // DATA
182 CountingAllocator d_allocator; // memory allocator
183
184 RecordAttributes d_fixedFields; // bytes used by fixed fields
185
186 UserFields d_userFields; // bytes used by user fields
187
189 d_attributes; // managed attributes
190
191 bslma::Allocator *d_allocator_p; // allocator used to supply memory;
192 // held but not own
193
194 // FRIENDS
195 friend bool operator==(const Record&, const Record&);
196
197 public:
198 // CLASS METHODS
199
200 /// Destroy the specified `*object` and use the allocator held by `*object`
201 /// to deallocate its memory footprint. The behavior is undefined unless
202 /// `object` is the address of a valid log record.
203 static void deleteObject(const Record *object);
204
205 // TRAITS
207
208 // CREATORS
209
210 /// Create a log record having default values for its fixed fields and its
211 /// user-defined fields. Optionally specify a `basicAllocator` used to
212 /// supply memory. If `basicAllocator` is 0, the currently installed
213 /// default allocator is used.
214 explicit Record(bslma::Allocator *basicAllocator = 0);
215
216 /// Create a log record with fixed fields having the value of the specified
217 /// `fixedFields` and user-defined fields having the value of the specified
218 /// `userFields`. Optionally specify a `basicAllocator` used to supply
219 /// memory. If `basicAllocator` is 0, the currently installed default
220 /// allocator is used.
222 const UserFields& userFields,
223 bslma::Allocator *basicAllocator = 0);
224
225 /// Create a log record having the value of the specified `original` log
226 /// record. Optionally specify a `basicAllocator` used to supply memory.
227 /// If `basicAllocator` is 0, the currently installed default allocator is
228 /// used.
229 Record(const Record& original, bslma::Allocator *basicAllocator = 0);
230
231 /// Destroy this object.
232 ~Record() = default;
233
234 // MANIPULATORS
235
236 /// Assign to this log record the value of the specified `rhs` log record
237 /// and return the reference to this modifiable record.
238 Record& operator=(const Record& rhs);
239
240 /// Clear this log record by removing the user fields, attributes, and
241 /// clearing the fixed field's message buffer. Note that this method is
242 /// tailored for efficient memory use within the `ball` logging system.
243 void clear();
244
245 /// Add a managed copy of the specified `attribute` to the container of
246 /// attributes maintained by this log record.
247 void addAttribute(const ball::Attribute& attribute);
248
249 /// Return the modifiable fixed fields of this log record.
251
252 /// Set the fixed fields of this log record to the value of the
253 /// specified `fixedFields`.
255
256 /// Set the custom user-defined fields of this log record to the value
257 /// of the specified `userFields`.
258 ///
259 /// @deprecated Use log record attributes.
260 void setCustomFields(const ball::UserFields& userFields);
261
262 /// Return a reference providing modifiable access to the custom
263 /// user-defined fields of this log record.
264 ///
265 /// @deprecated Use log record attributes.
267
268 // ACCESSORS
269
270 /// Return the non-modifiable fixed fields of this log record.
271 const RecordAttributes& fixedFields() const;
272
273 /// Return a reference providing non-modifiable access to the custom
274 /// user-defined fields of this log record.
275 ///
276 /// @deprecated Use log record attributes.
277 const ball::UserFields& customFields() const;
278
279 /// Return a reference providing non-modifiable access to the attributes
280 /// of this log record.
282
283 /// Return the total number of bytes of dynamic memory allocated by
284 /// this log record object. Note that this value does not include
285 /// `sizeof *this`.
286 int numAllocatedBytes() const;
287
288 /// Format this object to the specified output `stream` at the
289 /// optionally specified indentation `level` and return a reference to
290 /// the modifiable `stream`. If `level` is specified, optionally
291 /// specify `spacesPerLevel`, the number of spaces per indentation
292 /// level for this and all of its nested objects. Each line is
293 /// indented by the absolute value of `level * spacesPerLevel`. If
294 /// `level` is negative, suppress indentation of the first line. If
295 /// `spacesPerLevel` is negative, suppress line breaks and format the
296 /// entire output on one line. If `stream` is initially invalid, this
297 /// operation has no effect.
298 bsl::ostream& print(bsl::ostream& stream,
299 int level = 0,
300 int spacesPerLevel = 4) const;
301};
302
303// FREE OPERATORS
304
305/// Return `true` if the specified `lhs` and `rhs` log records have the same
306/// value, and `false` otherwise. Two log records have the same value if the
307/// respective fixed fields have the same value and the respective user-defined
308/// fields have the same value.
309bool operator==(const Record& lhs, const Record& rhs);
310
311/// Return `true` if the specified `lhs` and `rhs` log records do not have the
312/// same value, and `false` otherwise. Two log records do not have the same
313/// value if either the respective fixed fields or user-defined fields do not
314/// have the same value.
315bool operator!=(const Record& lhs, const Record& rhs);
316
317/// Format the members of the specified `record` to the specified output
318/// `stream` and return a reference to the modifiable `stream`.
319bsl::ostream& operator<<(bsl::ostream& stream, const Record& record);
320
321// ============================================================================
322// INLINE DEFINITIONS
323// ============================================================================
324
325 // ------------
326 // class Record
327 // ------------
328
329// CLASS METHODS
330inline
331void Record::deleteObject(const Record *object)
332{
333 object->d_allocator_p->deleteObjectRaw(object);
334}
335
336// CREATORS
337inline
339: d_allocator(basicAllocator)
340, d_fixedFields(&d_allocator)
341, d_userFields(&d_allocator)
342, d_attributes(&d_allocator)
343, d_allocator_p(bslma::Default::allocator(basicAllocator))
344{
345}
346
347inline
349 const ball::UserFields& userFields,
350 bslma::Allocator *basicAllocator)
351: d_allocator(basicAllocator)
352, d_fixedFields(fixedFields, &d_allocator)
353, d_userFields(userFields, &d_allocator)
354, d_attributes(&d_allocator)
355, d_allocator_p(bslma::Default::allocator(basicAllocator))
356{
357}
358
359inline
360Record::Record(const Record& original, bslma::Allocator *basicAllocator)
361: d_allocator(basicAllocator)
362, d_fixedFields(original.d_fixedFields, &d_allocator)
363, d_userFields(original.d_userFields, &d_allocator)
364, d_attributes(original.d_attributes, &d_allocator)
365, d_allocator_p(bslma::Default::allocator(basicAllocator))
366{
367}
368
369// MANIPULATORS
370inline
372{
373 if (this != &rhs) {
374 d_fixedFields = rhs.d_fixedFields;
375 d_userFields = rhs.d_userFields;
376 d_attributes = rhs.d_attributes;
377 }
378 return *this;
379}
380
381inline
382void Record::addAttribute(const Attribute& attribute)
383{
384 d_attributes.push_back(ManagedAttribute(attribute));
385}
386
387inline
389{
392 d_attributes.clear();
393}
394
395inline
397{
398 return d_fixedFields;
399}
400
401inline
403{
404 d_fixedFields = fixedFields;
405}
406
407inline
409{
410 d_userFields = userFields;
411}
412
413inline
415{
416 return d_userFields;
417}
418
419// ACCESSORS
420inline
422{
423 return d_fixedFields;
424}
425
426inline
428{
429 return d_userFields;
430}
431
432inline
434{
435 return d_attributes;
436}
437
438inline
440{
441 return static_cast<int>(d_allocator.numBytesTotal());
442}
443
444} // close package namespace
445
446// FREE OPERATORS
447inline
448bool ball::operator==(const Record& lhs, const Record& rhs)
449{
450 return lhs.d_fixedFields == rhs.d_fixedFields
451 && lhs.d_userFields == rhs.d_userFields
452 && lhs.d_attributes == rhs.d_attributes;
453}
454
455inline
456bool ball::operator!=(const Record& lhs, const Record& rhs)
457{
458 return !(lhs == rhs);
459}
460
461inline
462bsl::ostream& ball::operator<<(bsl::ostream& stream, const Record& record)
463{
464 return record.print(stream, 0, -1);
465}
466
467
468
469#endif
470
471// ----------------------------------------------------------------------------
472// Copyright 2015 Bloomberg Finance L.P.
473//
474// Licensed under the Apache License, Version 2.0 (the "License");
475// you may not use this file except in compliance with the License.
476// You may obtain a copy of the License at
477//
478// http://www.apache.org/licenses/LICENSE-2.0
479//
480// Unless required by applicable law or agreed to in writing, software
481// distributed under the License is distributed on an "AS IS" BASIS,
482// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
483// See the License for the specific language governing permissions and
484// limitations under the License.
485// ----------------------------- END-OF-FILE ----------------------------------
486
487/** @} */
488/** @} */
489/** @} */
Definition ball_attribute.h:198
Definition ball_countingallocator.h:138
size_type numBytesTotal() const
Definition ball_countingallocator.h:241
Definition ball_managedattribute.h:117
Definition ball_recordattributes.h:274
void clearMessage()
Definition ball_recordattributes.h:514
Definition ball_record.h:178
void setCustomFields(const ball::UserFields &userFields)
Definition ball_record.h:408
void clear()
Definition ball_record.h:388
ball::UserFields & customFields()
Definition ball_record.h:414
void addAttribute(const ball::Attribute &attribute)
Definition ball_record.h:382
Record & operator=(const Record &rhs)
Definition ball_record.h:371
friend bool operator==(const Record &, const Record &)
BSLMF_NESTED_TRAIT_DECLARATION(Record, bslma::UsesBslmaAllocator)
Record(bslma::Allocator *basicAllocator=0)
Definition ball_record.h:338
void setFixedFields(const RecordAttributes &fixedFields)
Definition ball_record.h:402
const bsl::vector< ball::ManagedAttribute > & attributes() const
Definition ball_record.h:433
~Record()=default
Destroy this object.
int numAllocatedBytes() const
Definition ball_record.h:439
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
static void deleteObject(const Record *object)
Definition ball_record.h:331
RecordAttributes & fixedFields()
Return the modifiable fixed fields of this log record.
Definition ball_record.h:396
Definition ball_userfields.h:136
void removeAll()
Definition ball_userfields.h:318
Definition bslstl_vector.h:1025
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)
Definition balxml_encoderoptions.h:68
Definition bslma_usesbslmaallocator.h:343