BDE 4.39.x 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/// bsls::Types::Uint64 kernelTid = bslmt::ThreadUtil::selfKernelIdAsUint64();
93///
94/// ball::RecordAttributes attributes(bdlt::CurrentTime::utc(), // time stamp
95/// processId, // process id
96/// threadId, // thread id
97/// kernelTid, // kernel tid
98/// __FILE__, // filename
99/// __LINE__, // line number
100/// "ExampleCategory", // category
101/// ball::Severity::e_WARN, // severity
102/// "Simple Test Message"); // message
103/// record.setFixedFields(attributes);
104///
105/// assert(attributes == record.fixedFields());
106/// @endcode
107/// Next, we add an additional attribute to the log record:
108/// @code
109/// record.addAttribute(ball::Attribute("myLib.name", "John Smith"));
110/// @endcode
111/// Finally, we write the record to a stream:
112/// @code
113/// bsl::ostringstream output;
114/// output << record << bsl::endl;
115/// @endcode
116/// @}
117/** @} */
118/** @} */
119
120/** @addtogroup bal
121 * @{
122 */
123/** @addtogroup ball
124 * @{
125 */
126/** @addtogroup ball_record
127 * @{
128 */
129
130#include <balscm_version.h>
131
135#include <ball_userfields.h>
136
137#include <bslma_allocator.h>
138#include <bslma_default.h>
140
142
143#include <bsls_alignment.h>
144#include <bsls_assert.h>
145
146#include <bsl_iosfwd.h>
147#include <bsl_vector.h>
148
149
150namespace ball {
151
152 // ============
153 // class Record
154 // ============
155
156/// This class provides a container for a set of fields that are appropriate
157/// for a user-configurable log record. The class contains a
158/// `RecordAttributes` object that in turn holds a fixed set of fields, a
159/// `ball::UserFields` object that holds a set of optional, user-defined
160/// fields, and a set of attributes associated with this log record. For
161/// each of these three sub-containers there is an accessor for obtaining
162/// the container value and a manipulator for changing that value.
163///
164/// Additionally, this class supports a complete set of *value* *semantic*
165/// operations, including copy construction, assignment and equality
166/// comparison, and `ostream` printing. A precise operational definition of
167/// when two instances have the same value can be found in the description
168/// of `operator==` for the class. This class is *exception* *neutral* with
169/// no guarantee of rollback: If an exception is thrown during the
170/// invocation of a method on a pre-existing instance, the object is left in
171/// a valid state, but its value is undefined. In no event is memory
172/// leaked. Finally, **aliasing** (e.g., using all or part of an object as
173/// both source and destination) is supported in all cases.
174///
175/// See @ref ball_record
176class Record {
177
178 private:
179 // DATA
180 CountingAllocator d_allocator; // memory allocator
181
182 RecordAttributes d_fixedFields; // bytes used by fixed fields
183
184 UserFields d_userFields; // bytes used by user fields
185
187 d_attributes; // managed attributes
188
189 bslma::Allocator *d_allocator_p; // allocator used to supply memory;
190 // held but not own
191
192 // FRIENDS
193 friend bool operator==(const Record&, const Record&);
194
195 public:
196 // CLASS METHODS
197
198 /// Destroy the specified `*object` and use the allocator held by `*object` to deallocate its memory footprint.
199 ///
200 /// \pre The behavior is undefined unless
201 /// `object` is the address of a valid log record.
202 static void deleteObject(const Record *object);
203
204 // TRAITS
206
207 // CREATORS
208
209 /// Create a log record having default values for its fixed fields and its
210 /// user-defined fields. Optionally specify a `basicAllocator` used to
211 /// supply memory. If `basicAllocator` is 0, the currently installed
212 /// default allocator is used.
213 explicit Record(bslma::Allocator *basicAllocator = 0);
214
215 /// Create a log record with fixed fields having the value of the specified
216 /// `fixedFields` and user-defined fields having the value of the specified
217 /// `userFields`. Optionally specify a `basicAllocator` used to supply
218 /// memory. If `basicAllocator` is 0, the currently installed default
219 /// allocator is used.
221 const UserFields& userFields,
222 bslma::Allocator *basicAllocator = 0);
223
224 /// Create a log record having the value of the specified `original` log
225 /// record. Optionally specify a `basicAllocator` used to supply memory.
226 /// If `basicAllocator` is 0, the currently installed default allocator is
227 /// used.
228 Record(const Record& original, bslma::Allocator *basicAllocator = 0);
229
230 /// Destroy this object.
231 ~Record() = default;
232
233 // MANIPULATORS
234
235 /// Assign to this log record the value of the specified `rhs` log record
236 /// and return the reference to this modifiable record.
237 Record& operator=(const Record& rhs);
238
239 /// Clear this log record by removing the user fields, attributes, and clearing the fixed field's message buffer.
240 ///
241 /// \note 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 this log record object.
284 ///
285 /// \note Note that this value does not include
286 /// `sizeof *this`.
287 int numAllocatedBytes() const;
288
289 /// Format this object to the specified output `stream` at the
290 /// optionally specified indentation `level` and return a reference to
291 /// the modifiable `stream`. If `level` is specified, optionally
292 /// specify `spacesPerLevel`, the number of spaces per indentation
293 /// level for this and all of its nested objects. Each line is
294 /// indented by the absolute value of `level * spacesPerLevel`. If
295 /// `level` is negative, suppress indentation of the first line. If
296 /// `spacesPerLevel` is negative, suppress line breaks and format the
297 /// entire output on one line. If `stream` is initially invalid, this
298 /// operation has no effect.
299 bsl::ostream& print(bsl::ostream& stream,
300 int level = 0,
301 int spacesPerLevel = 4) const;
302};
303
304// FREE OPERATORS
305
306/// Return `true` if the specified `lhs` and `rhs` log records have the same
307/// value, and `false` otherwise. Two log records have the same value if the
308/// respective fixed fields have the same value and the respective user-defined
309/// fields have the same value.
310bool operator==(const Record& lhs, const Record& rhs);
311
312/// Return `true` if the specified `lhs` and `rhs` log records do not have the
313/// same value, and `false` otherwise. Two log records do not have the same
314/// value if either the respective fixed fields or user-defined fields do not
315/// have the same value.
316bool operator!=(const Record& lhs, const Record& rhs);
317
318/// Format the members of the specified `record` to the specified output
319/// `stream` and return a reference to the modifiable `stream`.
320bsl::ostream& operator<<(bsl::ostream& stream, const Record& record);
321
322// ============================================================================
323// INLINE DEFINITIONS
324// ============================================================================
325
326 // ------------
327 // class Record
328 // ------------
329
330// CLASS METHODS
331inline
332void Record::deleteObject(const Record *object)
333{
334 object->d_allocator_p->deleteObjectRaw(object);
335}
336
337// CREATORS
338inline
340: d_allocator(basicAllocator)
341, d_fixedFields(&d_allocator)
342, d_userFields(&d_allocator)
343, d_attributes(&d_allocator)
344, d_allocator_p(bslma::Default::allocator(basicAllocator))
345{
346}
347
348inline
350 const ball::UserFields& userFields,
351 bslma::Allocator *basicAllocator)
352: d_allocator(basicAllocator)
353, d_fixedFields(fixedFields, &d_allocator)
354, d_userFields(userFields, &d_allocator)
355, d_attributes(&d_allocator)
356, d_allocator_p(bslma::Default::allocator(basicAllocator))
357{
358}
359
360inline
361Record::Record(const Record& original, bslma::Allocator *basicAllocator)
362: d_allocator(basicAllocator)
363, d_fixedFields(original.d_fixedFields, &d_allocator)
364, d_userFields(original.d_userFields, &d_allocator)
365, d_attributes(original.d_attributes, &d_allocator)
366, d_allocator_p(bslma::Default::allocator(basicAllocator))
367{
368}
369
370// MANIPULATORS
371inline
373{
374 if (this != &rhs) {
375 d_fixedFields = rhs.d_fixedFields;
376 d_userFields = rhs.d_userFields;
377 d_attributes = rhs.d_attributes;
378 }
379 return *this;
380}
381
382inline
383void Record::addAttribute(const Attribute& attribute)
384{
385 d_attributes.push_back(ManagedAttribute(attribute));
386}
387
388inline
390{
393 d_attributes.clear();
394}
395
396inline
398{
399 return d_fixedFields;
400}
401
402inline
404{
405 d_fixedFields = fixedFields;
406}
407
408inline
410{
411 d_userFields = userFields;
412}
413
414inline
416{
417 return d_userFields;
418}
419
420// ACCESSORS
421inline
423{
424 return d_fixedFields;
425}
426
427inline
429{
430 return d_userFields;
431}
432
433inline
435{
436 return d_attributes;
437}
438
439inline
441{
442 return static_cast<int>(d_allocator.numBytesTotal());
443}
444
445} // close package namespace
446
447// FREE OPERATORS
448inline
449bool ball::operator==(const Record& lhs, const Record& rhs)
450{
451 return lhs.d_fixedFields == rhs.d_fixedFields
452 && lhs.d_userFields == rhs.d_userFields
453 && lhs.d_attributes == rhs.d_attributes;
454}
455
456inline
457bool ball::operator!=(const Record& lhs, const Record& rhs)
458{
459 return !(lhs == rhs);
460}
461
462inline
463bsl::ostream& ball::operator<<(bsl::ostream& stream, const Record& record)
464{
465 return record.print(stream, 0, -1);
466}
467
468
469
470#endif
471
472// ----------------------------------------------------------------------------
473// Copyright 2015 Bloomberg Finance L.P.
474//
475// Licensed under the Apache License, Version 2.0 (the "License");
476// you may not use this file except in compliance with the License.
477// You may obtain a copy of the License at
478//
479// http://www.apache.org/licenses/LICENSE-2.0
480//
481// Unless required by applicable law or agreed to in writing, software
482// distributed under the License is distributed on an "AS IS" BASIS,
483// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
484// See the License for the specific language governing permissions and
485// limitations under the License.
486// ----------------------------- END-OF-FILE ----------------------------------
487
488/** @} */
489/** @} */
490/** @} */
Definition ball_attribute.h:199
Definition ball_countingallocator.h:138
size_type numBytesTotal() const
Definition ball_countingallocator.h:244
Definition ball_managedattribute.h:117
Definition ball_recordattributes.h:275
void clearMessage()
Definition ball_recordattributes.h:534
Definition ball_record.h:176
void setCustomFields(const ball::UserFields &userFields)
Definition ball_record.h:409
void clear()
Definition ball_record.h:389
ball::UserFields & customFields()
Definition ball_record.h:415
void addAttribute(const ball::Attribute &attribute)
Definition ball_record.h:383
Record & operator=(const Record &rhs)
Definition ball_record.h:372
friend bool operator==(const Record &, const Record &)
BSLMF_NESTED_TRAIT_DECLARATION(Record, bslma::UsesBslmaAllocator)
Record(bslma::Allocator *basicAllocator=0)
Definition ball_record.h:339
void setFixedFields(const RecordAttributes &fixedFields)
Definition ball_record.h:403
const bsl::vector< ball::ManagedAttribute > & attributes() const
Definition ball_record.h:434
~Record()=default
Destroy this object.
int numAllocatedBytes() const
Definition ball_record.h:440
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
static void deleteObject(const Record *object)
Definition ball_record.h:332
RecordAttributes & fixedFields()
Return the modifiable fixed fields of this log record.
Definition ball_record.h:397
Definition ball_userfields.h:136
void removeAll()
Definition ball_userfields.h:324
Definition bslstl_vector.h:1120
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
Definition baljsn_encoder_testtypes.h:76
Definition bslma_usesbslmaallocator.h:344