BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball_recordstringformatter.h
Go to the documentation of this file.
1/// @file ball_recordstringformatter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_recordstringformatter.h -*-C++-*-
8#ifndef INCLUDED_BALL_RECORDSTRINGFORMATTER
9#define INCLUDED_BALL_RECORDSTRINGFORMATTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_recordstringformatter ball_recordstringformatter
15/// @brief Provide a record formatter that uses a `printf`-style format spec.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_recordstringformatter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_recordstringformatter-purpose"> Purpose</a>
25/// * <a href="#ball_recordstringformatter-classes"> Classes </a>
26/// * <a href="#ball_recordstringformatter-description"> Description </a>
27/// * <a href="#ball_recordstringformatter-record-format-specification"> Record Format Specification </a>
28/// * <a href="#ball_recordstringformatter-log-record-attributes-rendering-details"> Log Record Attributes Rendering Details </a>
29/// * <a href="#ball_recordstringformatter-usage"> Usage </a>
30///
31/// # Purpose {#ball_recordstringformatter-purpose}
32/// Provide a record formatter that uses a `printf`-style format spec.
33///
34/// # Classes {#ball_recordstringformatter-classes}
35///
36/// - ball::RecordStringFormatter: `printf`-style formatter for log records
37///
38/// @see ball_record, ball_recordattributes
39///
40/// # Description {#ball_recordstringformatter-description}
41/// This component provides a value-semantic function-object class,
42/// `ball::RecordStringFormatter`, that is used to format log records according
43/// to a `printf`-style format specification (see "Record Format Specification"
44/// below). A format specification and a timestamp offset (in the form of a
45/// `bdlt::DatetimeInterval`) are optionally supplied upon construction of a
46/// `ball::RecordStringFormatter` object (or simply "record formatter"). If a
47/// format specification is not supplied, a default one (defined below) is used.
48/// If a timestamp offset is not supplied, it defaults to 0. Both the format
49/// specification and timestamp offset of a record formatter can be modified
50/// following construction.
51///
52/// An overloaded `operator()` is defined for `ball::RecordStringFormatter` that
53/// takes a `ball::Record` and an `bsl::ostream` as arguments. This method
54/// formats the given record according to the format specification of the record
55/// formatter and outputs the result to the given stream. Additionally, each
56/// timestamp indicated in the format specification is biased by the timestamp
57/// offset of the record formatter prior to outputting it to the stream. This
58/// facilitates the logging of records in local time, if desired, in the event
59/// that the timestamp attribute of records are in UTC.
60///
61/// ## Record Format Specification {#ball_recordstringformatter-record-format-specification}
62///
63///
64/// The following table lists the `printf`-style (`%`-prefixed) conversion
65/// specifications, including their expansions, that are recognized within the
66/// format specification of a record formatter:
67/// @code
68/// %d - timestamp in 'DDMonYYYY_HH:MM:SS.mmm' format (27AUG2007_16:09:46.161)
69/// %D - timestamp in 'DDMonYYYY_HH:MM:SS.mmmuuu' format
70/// (27AUG2007_16:09:46.161324)
71/// %dtz - timestamp in 'DDMonYYYY_HH:MM:SS.mmm(+|-)HHMM' format
72/// (27AUG2007_16:09:46.161+0000)
73/// %Dtz - timestamp in 'DDMonYYYY_HH:MM:SS.mmmuuu(+|-)HHMM' format
74/// (27AUG2007_16:09:46.161324+0000)
75/// %i - timestamp in ISO 8601 format (without the millisecond or microsecond
76/// fields)
77/// %I - timestamp in ISO 8601 format (*with* the millisecond field)
78/// %O - timestamp in ISO 8601 format (*with* the millisecond and microsecond
79/// fields)
80/// %p - process Id
81/// %t - thread Id
82/// %T - thread Id in hex
83/// %s - severity
84/// %f - filename (as provided by '__FILE__')
85/// %F - filename abbreviated (basename of '__FILE__' only)
86/// %l - line number (as provided by '__LINE__')
87/// %c - category name
88/// %m - log message
89/// %x - log message with non-printable characters in hex
90/// %X - log message entirely in hex
91/// %u - user-defined fields
92/// %% - single '%' character
93/// %A - log all the attributes of the record
94/// %a - log only those attributes not already logged by the %a[name] or
95/// %av[name] specifier(s)
96/// %a[name] - log an attribute with the specified 'name' as "name=value",
97/// log nothing if the attribute with the specified 'name' is not found
98/// %av[name] - log only the value of an attribute with the specified 'name',
99/// log nothing if the attribute with the specified 'name' is not found
100/// @endcode
101/// (Note that `%F` is used to indicate the shortened form of `__FILE__` rather
102/// than `%f` because `%f` was given its current interpretation in an earlier
103/// version of this component.)
104///
105/// In addition, the following '\'-escape sequences are interpolated in the
106/// formatted output as indicated when they occur in the format specification:
107/// @code
108/// \n - newline character
109/// \t - tab character
110/// \\ - single '\' character
111/// @endcode
112/// Any other text included in the format specification of the record formatter
113/// is output verbatim.
114///
115/// When not supplied at construction, the default format specification of a
116/// record formatter is:
117/// @code
118/// "\n%d %p:%t %s %f:%l %c %m %u\n"
119/// @endcode
120/// A default-formatted record having no user-defined fields will have the
121/// following appearance:
122/// @code
123/// 27AUG2007_16:09:46.161 2040:1 WARN subdir/process.cpp:542 FOO.BAR.BAZ <text>
124/// @endcode
125///
126/// ## Log Record Attributes Rendering Details {#ball_recordstringformatter-log-record-attributes-rendering-details}
127///
128///
129/// Log record attributes are rendered as space-separated `name="value"` pairs
130/// (for example: mylibrary.username="mbloomberg"). Note that attribute names
131/// are *not* quoted, whereas attribute values, if they are strings, are
132/// *always* quoted.
133///
134/// ## Usage {#ball_recordstringformatter-usage}
135///
136///
137/// The following snippets of code illustrate how to use an instance of
138/// `ball::RecordStringFormatter` to format log records.
139///
140/// First we instantiate a record formatter with an explicit format
141/// specification (but we accept the default timestamp offset since it will not
142/// be used in this example):
143/// @code
144/// ball::RecordStringFormatter formatter("\n%t: %m\n");
145/// @endcode
146/// The chosen format specification indicates that, when a record is formatted
147/// using `formatter`, the thread Id attribute of the record will be output
148/// followed by the message attribute of the record.
149///
150/// Next we create a default `ball::Record` and set the thread Id and message
151/// attributes of the record to dummy values:
152/// @code
153/// ball::Record record;
154///
155/// record.fixedFields().setThreadID(6);
156/// record.fixedFields().setMessage("Hello, World!");
157/// @endcode
158/// The following "invocation" of the `formatter` function object formats
159/// `record` to `bsl::cout` according to the format specification supplied at
160/// construction:
161/// @code
162/// formatter(bsl::cout, record);
163/// @endcode
164/// As a result of this call, the following is printed to `stdout`:
165/// @code
166/// 6: Hello, World!
167/// @endcode
168/// @}
169/** @} */
170/** @} */
171
172/** @addtogroup bal
173 * @{
174 */
175/** @addtogroup ball
176 * @{
177 */
178/** @addtogroup ball_recordstringformatter
179 * @{
180 */
181
182#include <balscm_version.h>
183
185
186#include <bslma_allocator.h>
188
190
191#include <bsl_functional.h>
192#include <bsl_iosfwd.h>
193#include <bsl_string.h>
194#include <bsl_set.h>
195#include <bsl_vector.h>
196
197#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
198#include <bslalg_typetraits.h>
199#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
200
201
202namespace ball {
203
204class Record;
205
206 // ===========================
207 // class RecordStringFormatter
208 // ===========================
209
210/// This class provides a value-semantic log record formatter that holds a
211/// `printf`-style format specification and a timestamp offset. The
212/// overloaded `operator()` provided by the class formats a given record
213/// according to the format specification and outputs the formatted result
214/// to a given stream. The timestamp offset of the record formatter is
215/// added to each timestamp that is output to the stream.
216///
217/// See @ref ball_recordstringformatter
219
220 // PRIVATE TYPES
221
222 /// `FieldStringFormatter` is an alias for a functional object that
223 /// render fields provided by a `ball::Record` to a string.
224 typedef bsl::function<void(bsl::string *, const Record&)>
226
227 /// `FieldStringFormatters` is an alias for a vector of the
228 /// `FieldStringFormatter` objects.
230
231 /// `SkipAttributes` is an alias for a set of keys of attributes that
232 /// should not be printed as part of a `%a` format specifier.
234
235 public:
236 // TYPES
238
239 private:
240 // DATA
241 bsl::string d_formatSpec; // 'printf'-style format spec.
242 FieldStringFormatters d_fieldFormatters; // field formatter collection
243 SkipAttributes d_skipAttributes; // set of skipped attributes
244 bdlt::DatetimeInterval d_timestampOffset; // offset added to timestamps
245
246 // PRIVATE MANIPULATORS
247
248 /// Parse the format specification.
249 void parseFormatSpecification();
250
251 public:
252 // TRAITS
255
256
257 // PUBLIC CONSTANTS
258
259 /// The default log format specification used by `RecordStringFormatter`.
260 static const char *k_DEFAULT_FORMAT;
261
262 /// A simple standard record format that renders `ball::Attribute` values
263 /// in the formatted output. Note that this format is recommended over the
264 /// default format, `k_DEFAULT_FORMAT`, for most applications (the default
265 /// format is currently maintained for backwards compatibility).
266 static const char *k_BASIC_ATTRIBUTE_FORMAT;
267
268 // CREATORS
269
271 const allocator_type& allocator = allocator_type());
272 /// Create a record formatter having a default format specification and
273 /// a timestamp offset of 0. Optionally specify an `allocator` (e.g.,
274 /// the address of a `bslma::Allocator` object) to supply memory. If
275 /// `basicAllocator` is not supplied or 0, the currently installed
276 /// default allocator is used. The default format specification is:
277 /// @code
278 /// "\n%d %p:%t %s %f:%l %c %m %u\n"
279 /// @endcode
281 bslma::Allocator *basicallocator);
282
284 const char *format,
285 const allocator_type& allocator = allocator_type());
286 /// Create a record formatter having the specified `format`
287 /// specification and a timestamp offset of 0. Optionally specify an
288 /// `allocator` (e.g., the address of a `bslma::Allocator` object) to
289 /// supply memory. If `basicAllocator` is not supplied or 0, the
290 /// currently installed default allocator is used.
292 bslma::Allocator *basicAllocator);
293
294 /// Create a record formatter having a default format specification and
295 /// the specified timestamp `offset`. Optionally specify an `allocator`
296 /// (e.g., the address of a `bslma::Allocator` object) to supply memory;
297 /// otherwise, the default allocator is used. The default format
298 /// specification is:
299 /// @code
300 /// "\n%d %p:%t %s %f:%l %c %m %u\n"
301 /// @endcode
302 ///
303 /// @deprecated Use a constructor taking `publishInLocalTime` instead.
305 const bdlt::DatetimeInterval& offset,
306 const allocator_type& allocator = allocator_type());
307
308 /// Create a record formatter having a default format specification, and
309 /// if the specified `publishInLocalTime` flag is `true`, format the
310 /// timestamp of each logged record in the local time of the current
311 /// task, and format the timestamp in UTC otherwise. Optionally specify
312 /// an `allocator` (e.g., the address of a `bslma::Allocator` object) to
313 /// supply memory; otherwise, the default allocator is used. The
314 /// default format specification is:
315 /// @code
316 /// "\n%d %p:%t %s %f:%l %c %m %u\n"
317 /// @endcode
318 /// Note that local time offsets are calculated for the timestamp of
319 /// each formatted record and so track transitions into and out of
320 /// Daylight Saving Time.
322 bool publishInLocalTime,
323 const allocator_type& allocator = allocator_type());
324
325 /// Create a record formatter having the specified `format`
326 /// specification and the specified timestamp `offset`. Optionally
327 /// specify an `allocator` (e.g., the address of a `bslma::Allocator`
328 /// object) to supply memory; otherwise, the default allocator is used.
329 ///
330 /// @deprecated Use a constructor taking `publishInLocalTime` instead.
332 const char *format,
333 const bdlt::DatetimeInterval& offset,
334 const allocator_type& allocator = allocator_type());
335
336 /// Create a record formatter having the specified `format`
337 /// specification, and if the specified `publishInLocalTime` flag is
338 /// `true`, format the timestamp of each log in the local time of the
339 /// current task, and format the timestamp in UTC otherwise. Optionally
340 /// specify an `allocator` (e.g., the address of a `bslma::Allocator`
341 /// object) to supply memory; otherwise, the default allocator is used.
342 /// Note that local time offsets are calculated for the timestamp of
343 /// each formatted record and so track transitions into and out of
344 /// Daylight Saving Time.
346 bool publishInLocalTime,
347 const allocator_type& allocator = allocator_type());
348
349 /// Create a record formatter initialized to the value of the specified
350 /// `original` record formatter. Optionally specify an `allocator`
351 /// (e.g., the address of a `bslma::Allocator` object) to supply memory;
352 /// otherwise, the default allocator is used.
354 const RecordStringFormatter& original,
355 const allocator_type& allocator = allocator_type());
356
357 /// Destroy this object.
359
360 // MANIPULATORS
361
362 /// Assign to this record formatter the value of the specified `rhs`
363 /// record formatter.
365
366 /// Disable adjust of the timestamp attribute of to the current local
367 /// time by this file observer. This method has no effect if adjustment
368 /// to the current local time is not enabled.
370
371 /// Enable adjustment of the timestamp attribute to the current local
372 /// time. This method has no effect if adjustment to the current local
373 /// time is already enabled.
375
376 /// Set the format specification of this record formatter to the
377 /// specified `format`.
378 void setFormat(const char *format);
379
380 /// Set the timestamp offset of this record formatter to the specified
381 /// `offset`.
382 ///
383 /// @deprecated Use @ref enablePublishInLocalTime instead.
384 void setTimestampOffset(const bdlt::DatetimeInterval& offset);
385
386 // ACCESSORS
387
388 /// Format the specified `record` according to the format specification
389 /// of this record formatter and output the result to the specified
390 /// `stream`. The timestamp offset of this record formatter is added to
391 /// each timestamp that is output to `stream`.
392 void operator()(bsl::ostream& stream, const Record& record) const;
393
394 /// Return the format specification of this record formatter.
395 const char *format() const;
396
397 /// Return `true` if this formatter adjusts the timestamp attribute to
398 /// the current local time, and `false` otherwise.
400
401 /// Return a reference to the non-modifiable timestamp offset of this
402 /// record formatter.
403 ///
404 /// @deprecated Use @ref isPublishInLocalTimeEnabled instead.
406
407 // Aspects
408
409 /// Return the allocator used by this object to supply memory. Note
410 /// that if no allocator was supplied at construction the default
411 /// allocator in effect at construction is used.
413
414};
415
416// FREE OPERATORS
417
418/// Return `true` if the specified `lhs` and `rhs` record formatters have
419/// the same value, and `false` otherwise. Two record formatters have the
420/// same value if they have the same format specification and the same
421/// timestamp offset.
422bool operator==(const RecordStringFormatter& lhs,
423 const RecordStringFormatter& rhs);
424
425/// Return `true` if the specified `lhs` and `rhs` record formatters do not
426/// have the same value, and `false` otherwise. Two record formatters
427/// differ in value if their format specifications differ or their timestamp
428/// offsets differ.
429bool operator!=(const RecordStringFormatter& lhs,
430 const RecordStringFormatter& rhs);
431
432/// Write the specified `rhs` record formatter to the specified `output`
433/// stream in some reasonable (single-line) format and return a reference
434/// to the modifiable `stream`.
435bsl::ostream& operator<<(bsl::ostream& output,
436 const RecordStringFormatter& rhs);
437
438// ============================================================================
439// INLINE DEFINITIONS
440// ============================================================================
441
442 // ---------------------------
443 // class RecordStringFormatter
444 // ---------------------------
445
446// MANIPULATORS
447inline
448void RecordStringFormatter::setFormat(const char *format)
449{
450 d_formatSpec = format;
451 parseFormatSpecification();
452}
453
454inline
456 const bdlt::DatetimeInterval& offset)
457{
458 d_timestampOffset = offset;
459}
460
461// ACCESSORS
462inline
464{
465 return d_formatSpec.c_str();
466}
467
468inline
471{
472 return d_timestampOffset;
473}
474
475 // Aspects
476
477inline
480{
481 return d_formatSpec.get_allocator();
482}
483
484} // close package namespace
485
486// FREE OPERATORS
487inline
488bool ball::operator!=(const RecordStringFormatter& lhs,
489 const RecordStringFormatter& rhs)
490{
491 return !(lhs == rhs);
492}
493
494
495
496#endif
497
498// ----------------------------------------------------------------------------
499// Copyright 2015 Bloomberg Finance L.P.
500//
501// Licensed under the Apache License, Version 2.0 (the "License");
502// you may not use this file except in compliance with the License.
503// You may obtain a copy of the License at
504//
505// http://www.apache.org/licenses/LICENSE-2.0
506//
507// Unless required by applicable law or agreed to in writing, software
508// distributed under the License is distributed on an "AS IS" BASIS,
509// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
510// See the License for the specific language governing permissions and
511// limitations under the License.
512// ----------------------------- END-OF-FILE ----------------------------------
513
514/** @} */
515/** @} */
516/** @} */
Definition ball_recordstringformatter.h:218
bsl::allocator< char > allocator_type
Definition ball_recordstringformatter.h:237
BSLMF_NESTED_TRAIT_DECLARATION(RecordStringFormatter, bslma::UsesBslmaAllocator)
const char * format() const
Return the format specification of this record formatter.
Definition ball_recordstringformatter.h:463
RecordStringFormatter(const char *format, bool publishInLocalTime, const allocator_type &allocator=allocator_type())
RecordStringFormatter(const char *format, const bdlt::DatetimeInterval &offset, const allocator_type &allocator=allocator_type())
void operator()(bsl::ostream &stream, const Record &record) const
RecordStringFormatter(const char *format, const allocator_type &allocator=allocator_type())
void setFormat(const char *format)
Definition ball_recordstringformatter.h:448
RecordStringFormatter & operator=(const RecordStringFormatter &rhs)
RecordStringFormatter(const allocator_type &allocator=allocator_type())
static const char * k_BASIC_ATTRIBUTE_FORMAT
Definition ball_recordstringformatter.h:266
const bdlt::DatetimeInterval & timestampOffset() const
Definition ball_recordstringformatter.h:470
RecordStringFormatter(bool publishInLocalTime, const allocator_type &allocator=allocator_type())
RecordStringFormatter(const bdlt::DatetimeInterval &offset, const allocator_type &allocator=allocator_type())
RecordStringFormatter(const RecordStringFormatter &original, const allocator_type &allocator=allocator_type())
allocator_type get_allocator() const
Definition ball_recordstringformatter.h:479
bool isPublishInLocalTimeEnabled() const
RecordStringFormatter(bslma::Allocator *basicallocator)
~RecordStringFormatter()=default
Destroy this object.
static const char * k_DEFAULT_FORMAT
The default log format specification used by RecordStringFormatter.
Definition ball_recordstringformatter.h:260
void setTimestampOffset(const bdlt::DatetimeInterval &offset)
Definition ball_recordstringformatter.h:455
RecordStringFormatter(const char *format, bslma::Allocator *basicAllocator)
Definition ball_record.h:178
Definition bdlt_datetimeinterval.h:201
Definition bslma_bslallocator.h:580
Definition bslstl_string.h:1281
const CHAR_TYPE * c_str() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:6705
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Return the allocator used by this string to supply memory.
Definition bslstl_string.h:6723
Forward declaration.
Definition bslstl_function.h:934
Definition bslstl_set.h:657
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)
Definition bslma_usesbslmaallocator.h:343