BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball_streamobserver.h
Go to the documentation of this file.
1/// @file ball_streamobserver.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_streamobserver.h -*-C++-*-
8#ifndef INCLUDED_BALL_STREAMOBSERVER
9#define INCLUDED_BALL_STREAMOBSERVER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_streamobserver ball_streamobserver
15/// @brief Provide an observer that emits log records to a stream.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_streamobserver
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_streamobserver-purpose"> Purpose</a>
25/// * <a href="#ball_streamobserver-classes"> Classes </a>
26/// * <a href="#ball_streamobserver-description"> Description </a>
27/// * <a href="#ball_streamobserver-log-record-formatting"> Log Record Formatting </a>
28/// * <a href="#ball_streamobserver-thread-safety"> Thread Safety </a>
29/// * <a href="#ball_streamobserver-usage"> Usage </a>
30/// * <a href="#ball_streamobserver-example-1-basic-usage"> Example 1: Basic Usage </a>
31///
32/// # Purpose {#ball_streamobserver-purpose}
33/// Provide an observer that emits log records to a stream.
34///
35/// # Classes {#ball_streamobserver-classes}
36///
37/// - ball::StreamObserver: observer that emits log records to a stream
38///
39/// @see ball_record, ball_context, ball_loggermanager
40///
41/// # Description {#ball_streamobserver-description}
42/// This component provides a concrete implementation of the
43/// `ball::Observer` protocol for receiving and processing log records:
44/// @code
45/// ,--------------------.
46/// ( ball::StreamObserver )
47/// `--------------------'
48/// | ctor
49/// | setRecordFormatFunctor
50/// V
51/// ,--------------.
52/// ( ball::Observer )
53/// `--------------'
54/// publish
55/// releaseRecords
56/// dtor
57/// @endcode
58/// `ball::StreamObserver` is a concrete class derived from `ball::Observer`
59/// that processes the log records it receives through its `publish` method by
60/// writing them to an output stream. Given its minimal functionality,
61/// `ball::StreamObserver` should be used with care in a production environment.
62/// It is not recommended to construct this observer with file-based streams due
63/// to lack of any file rotation functionality.
64///
65/// ## Log Record Formatting {#ball_streamobserver-log-record-formatting}
66///
67///
68/// By default, the output format of published log records is:
69/// @code
70/// DATE_TIME PID THREAD-ID SEVERITY FILE LINE CATEGORY MESSAGE USER-FIELDS
71/// @endcode
72/// where `DATE` and `TIME` are of the form `DDMonYYYY` and `HH:MM:SS.mmm`,
73/// respectively (`Mon` being the 3-letter abbreviation for the month). For
74/// example, assuming that no user-defined fields are present, a log record
75/// will have the following appearance when the default format is in effect:
76/// @code
77/// 18MAY2005_18:58:12.076 7959 1 WARN ball_streamobserver2.t.cpp 404 TEST hi!
78/// @endcode
79/// The default format can be overridden by supplying a suitable formatting
80/// functor to `setRecordFormatFunctor`. For example, an instance of
81/// `ball::RecordStringFormatter` conveniently provides such a functor:
82/// @code
83/// streamObserver.setRecordFormatFunctor(
84/// ball::RecordStringFormatter("\n%I %p:%t %s %f:%l %c %m %u\n"));
85/// @endcode
86/// The above statement will cause subsequent records to be logged in a format
87/// that is almost identical to the default format except that the timestamp
88/// attribute will be written in ISO 8601 format. See
89/// @ref ball_recordstringformatter for information on how format specifications
90/// are defined and interpreted.
91///
92/// Note that the observer emits newline characters at the beginning and at the
93/// end of a log record by default, so the user needs to add them explicitly to
94/// the format string to preserve this behavior.
95///
96/// Also note that in the sample message above the timestamp has millisecond
97/// precision (`18MAY2005_18:58:12.076`). If microsecond precision is desired
98/// instead, consider using either the `%D` or `%O` format specification
99/// supported by @ref ball_recordstringformatter .
100///
101/// ## Thread Safety {#ball_streamobserver-thread-safety}
102///
103///
104/// All methods of `ball::StreamObserver` are thread-safe, and can be called
105/// concurrently by multiple threads.
106///
107/// ## Usage {#ball_streamobserver-usage}
108///
109///
110/// This section illustrates intended use of this component.
111///
112/// ### Example 1: Basic Usage {#ball_streamobserver-example-1-basic-usage}
113///
114///
115/// The following snippets of code illustrate the basic usage of
116/// `ball::StreamObserver`.
117///
118/// First create a `ball::Record` object `record` and a `ball::Context` object
119/// `context`. Note that the default values for these objects (or their
120/// contained objects) are perfectly suitable for logging purposes.
121/// @code
122/// ball::RecordAttributes attributes;
123/// ball::UserFields fieldValues;
124/// ball::Context context;
125///
126/// bslma::Allocator *ga = bslma::Default::globalAllocator(0);
127/// const bsl::shared_ptr<const ball::Record>
128/// record(new (*ga) ball::Record(attributes, fieldValues, ga), ga);
129/// @endcode
130/// Next, create a stream observer `observer` with the `bsl::cout` as the output
131/// stream.
132/// @code
133/// ball::StreamObserver observer(&bsl::cout);
134/// @endcode
135/// Finally, publish `record` and `context` to `observer`.
136/// @code
137/// observer.publish(record, context);
138/// @endcode
139/// This will produce the following output on `stdout`:
140/// @code
141/// 01JAN0001_24:00:00.000 0 0 OFF 0
142/// @endcode
143/// @}
144/** @} */
145/** @} */
146
147/** @addtogroup bal
148 * @{
149 */
150/** @addtogroup ball
151 * @{
152 */
153/** @addtogroup ball_streamobserver
154 * @{
155 */
156
157#include <balscm_version.h>
158
159#include <ball_observer.h>
160
161#include <bslma_allocator.h>
162#include <bslma_bslallocator.h>
164
166
167#include <bslmt_mutex.h>
168
169#include <bsls_assert.h>
170#include <bsls_keyword.h>
171#include <bsls_review.h>
172
173#include <bsl_iosfwd.h>
174#include <bsl_functional.h>
175
176
177namespace ball {
178
179class Context;
180class Record;
181
182 // ====================
183 // class StreamObserver
184 // ====================
185
186/// This class provides a concrete implementation of the `Observer`
187/// protocol. The `publish` method of this class outputs the log records
188/// that it receives to an instance of `bsl::ostream` supplied at
189/// construction.
190///
191/// See @ref ball_streamobserver
192class StreamObserver : public Observer {
193
194 public:
195 // TYPES
196
197 /// `RecordFormatFunctor` is an alias for the type of the functor used
198 /// for formatting log records to a stream.
199 typedef bsl::function<void(bsl::ostream&, const Record&)>
201
203
204 private:
205 // DATA
206 bsl::ostream *d_stream_p; // output sink for log records
207
208 bslmt::Mutex d_mutex; // serializes concurrent calls to
209 // 'publish'
210
211 RecordFormatFunctor d_formatter; // formatting functor used when writing
212 // to log file
213
214 // NOT IMPLEMENTED
216 StreamObserver& operator=(const StreamObserver&);
217
218 // CLASS METHODS
219
220 /// Write the specified log `record` to the specified output `stream`
221 /// using the default record format of this stream observer.
222 static
223 void logRecordDefault(bsl::ostream& stream, const Record& record);
224
225 public:
226 // TRAITS
228
229 // CREATORS
230
231 /// Create a stream observer that transmits log records to the specified
232 /// `stream`. Optionally specify an `allocator` (e.g., the address of a
233 /// `bslma::Allocator` object) to supply memory; otherwise, the default
234 /// allocator is used. Note that a default record format is in effect
235 /// for stream logging (see `setLogFileFunctor`).
236 explicit
237 StreamObserver(bsl::ostream *stream,
238 const allocator_type& allocator = allocator_type());
239
240 /// Destroy this stream observer.
242
243 // MANIPULATORS
244 using Observer::publish;
245
246 /// Process the specified log `record` having the specified publishing
247 /// `context`. Print `record` and `context` to the `bsl::ostream`
248 /// supplied at construction. The behavior is undefined if `record` or
249 /// `context` is modified during the execution of this method.
250 void publish(const bsl::shared_ptr<const Record>& record,
251 const Context& context)
253
254 /// Discard any shared reference to a `Record` object that was supplied
255 /// to the `publish` method, and is held by this observer. Note that
256 /// this operation should be called if resources underlying the
257 /// previously provided shared-pointers must be released.
259
260 /// Set the formatting functor used when writing records to the log file
261 /// of this file observer to the specified `formatter` functor. Note
262 /// that a default format ("\n%d %p %t %s %f %l %c %m %u\n") is in
263 /// effect until this method is called (see
264 /// @ref ball_recordstringformatter ). Also note that the observer emits
265 /// newline characters at the beginning and at the end of a log record
266 /// by default, so the user needs to add them explicitly to the format
267 /// string to preserve this behavior.
269};
270
271// ============================================================================
272// INLINE DEFINITIONS
273// ============================================================================
274
275 // --------------------
276 // class StreamObserver
277 // --------------------
278
279// MANIPULATORS
280inline
284
285} // close package namespace
286
287
288#endif
289
290// ----------------------------------------------------------------------------
291// Copyright 2017 Bloomberg Finance L.P.
292//
293// Licensed under the Apache License, Version 2.0 (the "License");
294// you may not use this file except in compliance with the License.
295// You may obtain a copy of the License at
296//
297// http://www.apache.org/licenses/LICENSE-2.0
298//
299// Unless required by applicable law or agreed to in writing, software
300// distributed under the License is distributed on an "AS IS" BASIS,
301// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
302// See the License for the specific language governing permissions and
303// limitations under the License.
304// ----------------------------- END-OF-FILE ----------------------------------
305
306/** @} */
307/** @} */
308/** @} */
Definition ball_context.h:295
Definition ball_observer.h:235
Definition ball_record.h:178
Definition ball_streamobserver.h:192
bsl::function< void(bsl::ostream &, const Record &)> RecordFormatFunctor
Definition ball_streamobserver.h:200
bsl::allocator< char > allocator_type
Definition ball_streamobserver.h:202
BSLMF_NESTED_TRAIT_DECLARATION(StreamObserver, bslma::UsesBslmaAllocator)
~StreamObserver() BSLS_KEYWORD_OVERRIDE
Destroy this stream observer.
void setRecordFormatFunctor(const RecordFormatFunctor &formatter)
void publish(const bsl::shared_ptr< const Record > &record, const Context &context) BSLS_KEYWORD_OVERRIDE
void releaseRecords() BSLS_KEYWORD_OVERRIDE
Definition ball_streamobserver.h:281
StreamObserver(bsl::ostream *stream, const allocator_type &allocator=allocator_type())
Definition bslma_bslallocator.h:580
Forward declaration.
Definition bslstl_function.h:934
Definition bslmt_mutex.h:315
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:653
Definition ball_administration.h:214
Definition bdlb_printmethods.h:283
Definition bslma_usesbslmaallocator.h:343