|
BDE 4.39.x Production Release
|
Provide utilities for creating log record formatters by scheme.
Provide utilities for creating log record formatters by scheme.
This component provides a utility struct, ball::RecordFormatterRegistryUtil, that defines a namespace for functions to create log record formatters based on URI-like scheme identifiers. The primary function, createRecordFormatter, interprets a format string beginning with a scheme (e.g., "text://", "json://", "qjson://") and delegates to the appropriate specialized formatter factory. This allows for flexible configuration of log output formats using a single, scheme-based interface.
The component supports the following schemes:
ball::RecordStringFormatterball::RecordJsonFormatterball::RecordJsonFormatterThe scheme determines which formatter will be used and the syntax of the format specification. The following schemes are currently supported: text, json, qjson. See Scheme-Based Formatters for more details of the supported schemes and their accompanying format specification syntaxes.
Trying to create a formatter with an invalid format string or an unknown scheme will result in a non-zero error code being returned. If the result formatter is not empty the method assumes that it is loaded with a reasonable (default or otherwise) format and will not change it. In case result is an empty bsl::function createRecordFormatter will return a non-zero error code and loads a default/fallback formatter into the result parameter. Said fallback formatter will be a sensible default for the given scheme if the scheme is recognized, or a default/fallback text formatter if the scheme is not one of the supported ones.
An example with an unknown scheme:
fallbackFormatter is still usable as it was loaded with a sensible default text formatter, allowing the application to continue logging even when the configuration is incorrect.
The output of the above code will look something like:
Currently the text formatter just ignores errors in its format string so only the "json://" and "qjson://" schemes perform some form of validation. That means that a meaningless "text://" format will succeed and may not produce any useful log output, while some invalid "json://" or "qjson://" formats will fall back to a reasonable default with a non-zero return code.
This section illustrates intended use of this component.
Suppose we have configuration entries that define log record formatters using URI-like schemes, and we need to create the corresponding formatters. This component supports "text://", "json://", and "qjson://" schemes.
First, we create formatter options and a test record:
Now, we demonstrate creating formatters with different schemes. For text output, we use the "text://" scheme with printf-style format specifiers:
For structured JSON output, we use the "json://" scheme with a JSON array specifying which fields to include:
For simplified JSON using printf-style specifiers, we use the "qjson://" scheme:
Each formatter can then be used to format log records: