Flow 1.0.0
Flow project: Full implementation reference.
verbosity_config.hpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19#pragma once
20
22#include "flow/log/config.hpp"
23#include <utility>
24#include <string>
25#include <vector>
26
27namespace flow::log
28{
29
30// Types.
31
32/**
33 * Optional-use structure encapsulating a full set of verbosity config, such that one can parse it from a config source
34 * (like an options file) in concise form and apply it to a log::Config object.
35 *
36 * Put simply, one can read a string like `"ALL:2;COOL_COMPONENT:4;ANOTHER_COMPONENT:TRACE"` from an `istream` via
37 * `>>`; then `this->apply_to_config(&cfg)`, where `cfg` is a `log::Config`; resulting in the necessary
38 * `Config::configure_default_verbosity()` and `Config::configure_component_verbosity_by_name()` calls that will set
39 * the default verbosity to INFO and those specific components' verbosities to DATA and TRACE respectively in order.
40 */
42{
43public:
44 // Types.
45
46 /// Short-hand for the configuration capable of being encapsulated by Verbosity_config.
47 using Component_sev_pair_seq = std::vector<std::pair<std::string, Sev>>;
48
49 // Constants.
50
51 /// String that Verbosity_config::parse() treats as the default/catch-all verbosity's "component" specifier.
52 static const std::string S_ALL_COMPONENT_NAME_ALIAS;
53
54 /// Separates component/severity pairs in a Verbosity_config specifier string.
55 static const char S_TOKEN_SEPARATOR;
56
57 /// Separates component and severity within each pair in a Verbosity_config specifier string.
58 static const char S_PAIR_SEPARATOR;
59
60 // Constructors/destructor.
61
62 /**
63 * Constructor a Verbosity_config that resets all severity config and sets the default/catch-all to
64 * Config::S_MOST_VERBOSE_SEV_DEFAULT.
65 */
67
68 // Methods.
69
70 /**
71 * Deserializes `*this` from a standard input stream. Reads a single space-delimited token
72 * from the given stream. Separates that into a sequence of #S_TOKEN_SEPARATOR-delimited pairs, each representing an
73 * element of what will be returnable via component_sev_pairs(), in order. Each pair is to be in one of
74 * the following forms:
75 * - No #S_PAIR_SEPARATOR; just a Severity readable via its `istream<<` operator (as of this writing,
76 * a number "1", "2", ... or a string "WARNING", "INFO", etc.). Then this is taken to be the severity, and
77 * the component name for component_sev_pairs() is taken to be as-if "" (the default).
78 * - ":sev" (where the colon stands for #S_PAIR_SEPARATOR): As-if simply "sev" (previous bullet point).
79 * - "ALL:sev" (ditto re. colon): `ALL` is #S_ALL_COMPONENT_NAME_ALIAS: Same meaning as previous bullet point.
80 * - "name:sev" (ditto re. colon): `name` is the component name; `sev` is the Severity readable via its
81 * `istream<<` operator.
82 *
83 * Returns `true` if the token was legal; else `false`. last_result_message() can then be invoked to get
84 * further details suitable for display to the user in the case of `false`.
85 *
86 * If `true` returned, the existing payload is completely overwritten. Otherwise it is untouched.
87 *
88 * @param is
89 * Input stream.
90 * @return `true` if and only if successfully deserialized. See also last_result_message().
91 */
92 bool parse(std::istream& is);
93
94 /**
95 * Applies `*this` to to the given log::Config. Typically one would parse into `*this` first.
96 *
97 * Returns `true` if this succeeded; else `false`. last_result_message() can then be invoked to get
98 * further details suitable for display to the user in the case of `false`. Typically this would only occur, as of
99 * this writing, on an unknown component name according to what has been registered on `*target_config`.
100 *
101 * If `true` returned, the Config verbosity config is completely overwritten; otherwise it is reset and completely
102 * overwritten but may represent only a partial application (valid but typically not advisable to use).
103 *
104 * @param target_config
105 * The log::Config to reset.
106 * @return `true` if and only if successfully applied. See also last_result_message().
107 */
108 bool apply_to_config(Config* target_config);
109
110 /**
111 * To be used after parse() or `operator<<` or apply_to_config(), returns "" on success or a message describing
112 * the problem on failure.
113 *
114 * @return See above.
115 */
116 const std::string& last_result_message() const;
117
118 /**
119 * Read-only access to encapsulated config; specifies the verbosity-setting calls to make on a Config in order,
120 * given as component-name-to-severity pairs, with an empty component name specifying the default severity used
121 * when no component name is given.
122 *
123 * The assumed semantics as applied to a given log::Config `cfg` are as follows:
124 * -# If empty, or the first pair's component name is *not* "", then act as-if there was an additional
125 * front-inserted pair `("", Config::S_MOST_VERBOSE_SEV_DEFAULT)`.
126 * -# For the first pair: `cfg.configure_default_verbosity(v, true);`, where `v` is the log::Sev in that slot.
127 * In other words the config is reset, and the catch-all default verbosity is initialized to that severity.
128 * -# For each subsequent pair `(name, v)`:
129 * - If the `name` is "": `cfg.configure_default_verbosity(v, false);`.
130 * - Otherwise: `cfg.configure_component_verbosity_by_name(v, name);`.
131 *
132 * @return See above.
133 */
135
136private:
137 // Data.
138
139 /// See component_sev_pairs().
141
142 /// See last_result_message().
144}; // struct Verbosity_config
145
146// Free functions: in *_fwd.hpp.
147
148} // namespace flow::log
Class used to configure the filtering and logging behavior of Loggers; its use in your custom Loggers...
Definition: config.hpp:200
Optional-use structure encapsulating a full set of verbosity config, such that one can parse it from ...
const std::string & last_result_message() const
To be used after parse() or operator<< or apply_to_config(), returns "" on success or a message descr...
bool apply_to_config(Config *target_config)
Applies *this to to the given log::Config.
std::string m_last_result_message
See last_result_message().
Component_sev_pair_seq m_component_sev_pairs
See component_sev_pairs().
static const char S_TOKEN_SEPARATOR
Separates component/severity pairs in a Verbosity_config specifier string.
static const std::string S_ALL_COMPONENT_NAME_ALIAS
String that Verbosity_config::parse() treats as the default/catch-all verbosity's "component" specifi...
Verbosity_config()
Constructor a Verbosity_config that resets all severity config and sets the default/catch-all to Conf...
const Component_sev_pair_seq & component_sev_pairs() const
Read-only access to encapsulated config; specifies the verbosity-setting calls to make on a Config in...
std::vector< std::pair< std::string, Sev > > Component_sev_pair_seq
Short-hand for the configuration capable of being encapsulated by Verbosity_config.
bool parse(std::istream &is)
Deserializes *this from a standard input stream.
static const char S_PAIR_SEPARATOR
Separates component and severity within each pair in a Verbosity_config specifier string.
Flow module providing logging functionality.