1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
43 /* Logging importance levels. */
52 const char *vlog_get_level_name(enum vlog_level);
53 enum vlog_level vlog_get_level_val(const char *name);
55 /* Facilities that we can log to. */
63 const char *vlog_get_facility_name(enum vlog_facility);
64 enum vlog_facility vlog_get_facility_val(const char *name);
66 /* Modules that can emit log messages. */
67 #define VLOG_MODULES \
69 VLOG_MODULE(controller) \
70 VLOG_MODULE(ctlpath) \
72 VLOG_MODULE(datapath) \
74 VLOG_MODULE(dhcp_client) \
79 VLOG_MODULE(learning_switch) \
80 VLOG_MODULE(mac_learning) \
82 VLOG_MODULE(netlink) \
83 VLOG_MODULE(ofp_discover) \
84 VLOG_MODULE(poll_loop) \
85 VLOG_MODULE(secchan) \
88 VLOG_MODULE(socket_util) \
89 VLOG_MODULE(vconn_netlink) \
90 VLOG_MODULE(vconn_tcp) \
91 VLOG_MODULE(vconn_ssl) \
92 VLOG_MODULE(vconn_stream) \
93 VLOG_MODULE(vconn_unix) \
97 /* VLM_ constant for each vlog module. */
99 #define VLOG_MODULE(NAME) VLM_##NAME,
106 const char *vlog_get_module_name(enum vlog_module);
107 enum vlog_module vlog_get_module_val(const char *name);
109 /* Rate-limiter for log messages. */
110 struct vlog_rate_limit {
111 /* Configuration settings. */
112 unsigned int rate; /* Tokens per second. */
113 unsigned int burst; /* Max cumulative tokens credit. */
115 /* Current status. */
116 unsigned int tokens; /* Current number of tokens. */
117 time_t last_fill; /* Last time tokens added. */
118 time_t first_dropped; /* Time first message was dropped. */
119 unsigned int n_dropped; /* Number of messages dropped. */
122 /* Number of tokens to emit a message. We add 'rate' token per second, which
123 * is 60 times the unit used for 'rate', thus 60 tokens are required to emit
125 #define VLOG_MSG_TOKENS 60
127 /* Initializer for a struct vlog_rate_limit, to set up a maximum rate of RATE
128 * messages per minute and a maximum burst size of BURST messages. */
129 #define VLOG_RATE_LIMIT_INIT(RATE, BURST) \
132 (MIN(BURST, UINT_MAX / VLOG_MSG_TOKENS) \
133 * VLOG_MSG_TOKENS), /* burst */ \
139 /* Configuring how each module logs messages. */
140 enum vlog_level vlog_get_level(enum vlog_module, enum vlog_facility);
141 void vlog_set_levels(enum vlog_module, enum vlog_facility, enum vlog_level);
142 char *vlog_set_levels_from_string(const char *);
143 char *vlog_get_levels(void);
144 bool vlog_is_enabled(enum vlog_module, enum vlog_level);
145 void vlog_set_verbosity(const char *arg);
147 /* Function for actual logging. */
148 void vlog_init(void);
149 void vlog_exit(void);
150 void vlog(enum vlog_module, enum vlog_level, const char *format, ...)
151 __attribute__((format(printf, 3, 4)));
152 void vlog_valist(enum vlog_module, enum vlog_level, const char *, va_list)
153 __attribute__((format(printf, 3, 0)));
154 void vlog_rate_limit(enum vlog_module, enum vlog_level,
155 struct vlog_rate_limit *, const char *, ...)
156 __attribute__((format(printf, 4, 5)));
158 /* Convenience macros. To use these, define THIS_MODULE as a macro that
159 * expands to the module used by the current source file, e.g.
161 * #define THIS_MODULE VLM_netlink
162 * Guaranteed to preserve errno.
164 #define VLOG_EMER(...) vlog(THIS_MODULE, VLL_EMER, __VA_ARGS__)
165 #define VLOG_ERR(...) vlog(THIS_MODULE, VLL_ERR, __VA_ARGS__)
166 #define VLOG_WARN(...) vlog(THIS_MODULE, VLL_WARN, __VA_ARGS__)
167 #define VLOG_DBG(...) vlog(THIS_MODULE, VLL_DBG, __VA_ARGS__)
169 /* More convenience macros, for testing whether a given level is enabled in
170 * THIS_MODULE. When constructing a log message is expensive, this enables it
172 #define VLOG_IS_EMER_ENABLED() true
173 #define VLOG_IS_ERR_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_EMER)
174 #define VLOG_IS_WARN_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_WARN)
175 #define VLOG_IS_DBG_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_DBG)
177 /* Convenience macros. To use these, define THIS_MODULE as a macro that
178 * expands to the module used by the current source file, e.g.
180 * #define THIS_MODULE VLM_netlink
181 * Guaranteed to preserve errno.
183 #define VLOG_ERR_RL(RL, ...) \
184 vlog_rate_limit(THIS_MODULE, VLL_ERR, RL, __VA_ARGS__)
185 #define VLOG_WARN_RL(RL, ...) \
186 vlog_rate_limit(THIS_MODULE, VLL_WARN, RL, __VA_ARGS__)
187 #define VLOG_DBG_RL(RL, ...) \
188 vlog_rate_limit(THIS_MODULE, VLL_DBG, RL, __VA_ARGS__)