2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2016 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef OPENVSWITCH_VLOG_H
18 #define OPENVSWITCH_VLOG_H 1
33 #include <openvswitch/compiler.h>
34 #include <openvswitch/list.h>
35 #include <openvswitch/thread.h>
36 #include <openvswitch/token-bucket.h>
37 #include <openvswitch/util.h>
43 /* Logging severity levels.
45 * ovs-appctl(8) defines each of the log levels. */
47 VLOG_LEVEL(OFF, LOG_ALERT, 1) \
48 VLOG_LEVEL(EMER, LOG_ALERT, 1) \
49 VLOG_LEVEL(ERR, LOG_ERR, 3) \
50 VLOG_LEVEL(WARN, LOG_WARNING, 4) \
51 VLOG_LEVEL(INFO, LOG_NOTICE, 5) \
52 VLOG_LEVEL(DBG, LOG_DEBUG, 7)
54 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424_LEVEL) VLL_##NAME,
60 const char *vlog_get_level_name(enum vlog_level
);
61 enum vlog_level
vlog_get_level_val(const char *name
);
63 /* Destinations that we can log to. */
64 #define VLOG_DESTINATIONS \
65 VLOG_DESTINATION(SYSLOG, "ovs|%05N|%c%T|%p|%m") \
66 VLOG_DESTINATION(CONSOLE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c%T|%p|%m") \
67 VLOG_DESTINATION(FILE, "%D{%Y-%m-%dT%H:%M:%S.###Z}|%05N|%c%T|%p|%m")
68 enum vlog_destination
{
69 #define VLOG_DESTINATION(NAME, PATTERN) VLF_##NAME,
71 #undef VLOG_DESTINATION
73 VLF_ANY_DESTINATION
= -1
76 const char *vlog_get_destination_name(enum vlog_destination
);
77 enum vlog_destination
vlog_get_destination_val(const char *name
);
82 const char *name
; /* User-visible name. */
83 int levels
[VLF_N_DESTINATIONS
]; /* Minimum log level for each
85 int min_level
; /* Minimum log level for any destination. */
86 bool honor_rate_limits
; /* Set false to ignore rate limits. */
89 void vlog_insert_module(struct ovs_list
*);
91 const char *vlog_get_module_name(const struct vlog_module
*);
92 struct vlog_module
*vlog_module_from_name(const char *name
);
94 /* Rate-limiter for log messages. */
95 struct vlog_rate_limit
{
96 struct token_bucket token_bucket
;
97 time_t first_dropped
; /* Time first message was dropped. */
98 time_t last_dropped
; /* Time of most recent message drop. */
99 unsigned int n_dropped
; /* Number of messages dropped. */
100 struct ovs_mutex mutex
; /* Mutual exclusion for rate limit. */
103 /* Number of tokens to emit a message. We add 'rate' tokens per millisecond,
104 * thus 60,000 tokens are required to emit one message per minute. */
105 #define VLOG_MSG_TOKENS (60 * 1000)
107 /* Initializer for a struct vlog_rate_limit, to set up a maximum rate of RATE
108 * messages per minute and a maximum burst size of BURST messages. */
109 #define VLOG_RATE_LIMIT_INIT(RATE, BURST) \
111 TOKEN_BUCKET_INIT(RATE, OVS_SAT_MUL(BURST, VLOG_MSG_TOKENS)), \
112 0, /* first_dropped */ \
113 0, /* last_dropped */ \
115 OVS_MUTEX_INITIALIZER /* mutex */ \
118 /* Configuring how each module logs messages. */
119 enum vlog_level
vlog_get_level(const struct vlog_module
*,
120 enum vlog_destination
);
121 void vlog_set_levels(struct vlog_module
*,
122 enum vlog_destination
, enum vlog_level
);
123 char *vlog_set_levels_from_string(const char *) OVS_WARN_UNUSED_RESULT
;
124 void vlog_set_levels_from_string_assert(const char *);
125 char *vlog_get_levels(void);
126 char *vlog_get_patterns(void);
127 bool vlog_is_enabled(const struct vlog_module
*, enum vlog_level
);
128 bool vlog_should_drop(const struct vlog_module
*, enum vlog_level
,
129 struct vlog_rate_limit
*);
130 void vlog_set_verbosity(const char *arg
);
132 /* Configuring log destinations. */
133 void vlog_set_pattern(enum vlog_destination
, const char *pattern
);
134 int vlog_set_log_file(const char *file_name
);
135 int vlog_reopen_log_file(void);
137 void vlog_change_owner_unix(uid_t
, gid_t
);
140 /* Configure method how vlog should send messages to syslog server. */
141 void vlog_set_syslog_method(const char *method
);
143 /* Configure syslog target. */
144 void vlog_set_syslog_target(const char *target
);
146 /* Initialization. */
147 void vlog_init(void);
148 void vlog_enable_async(void);
150 /* Functions for actual logging. */
151 void vlog(const struct vlog_module
*, enum vlog_level
, const char *format
, ...)
152 OVS_PRINTF_FORMAT (3, 4);
153 void vlog_valist(const struct vlog_module
*, enum vlog_level
,
154 const char *, va_list)
155 OVS_PRINTF_FORMAT (3, 0);
157 OVS_NO_RETURN
void vlog_fatal(const struct vlog_module
*, const char *format
, ...)
158 OVS_PRINTF_FORMAT (2, 3);
159 OVS_NO_RETURN
void vlog_fatal_valist(const struct vlog_module
*,
160 const char *format
, va_list)
161 OVS_PRINTF_FORMAT (2, 0);
163 OVS_NO_RETURN
void vlog_abort(const struct vlog_module
*, const char *format
, ...)
164 OVS_PRINTF_FORMAT (2, 3);
165 OVS_NO_RETURN
void vlog_abort_valist(const struct vlog_module
*,
166 const char *format
, va_list)
167 OVS_PRINTF_FORMAT (2, 0);
169 void vlog_rate_limit(const struct vlog_module
*, enum vlog_level
,
170 struct vlog_rate_limit
*, const char *, ...)
171 OVS_PRINTF_FORMAT (4, 5);
173 /* Defines a logging module whose name is MODULE, which should generally be
174 * roughly the name of the source file, and makes it the module used by the
175 * logging convenience macros defined below. */
176 #define VLOG_DEFINE_THIS_MODULE(MODULE) \
177 static struct vlog_module this_module = { \
178 OVS_LIST_INITIALIZER(&this_module.list), \
179 #MODULE, /* name */ \
180 { VLL_INFO, VLL_INFO, VLL_INFO }, /* levels */ \
181 VLL_INFO, /* min_level */ \
182 true /* honor_rate_limits */ \
184 OVS_CONSTRUCTOR(init_this_module_##MODULE) { \
185 vlog_insert_module(&this_module.list); \
188 /* Prevent duplicate module names, via linker error. \
189 * The extra "extern" declaration makes sparse happy. */ \
190 extern struct vlog_module *VLM_##MODULE; \
191 struct vlog_module *VLM_##MODULE = &this_module;
193 /* Macros for the current module as set up by VLOG_DEFINE_THIS_MODULE.
194 * These are usually what you want to use.
196 * Guaranteed to preserve errno.
198 #define VLOG_FATAL(...) vlog_fatal(&this_module, __VA_ARGS__)
199 #define VLOG_ABORT(...) vlog_abort(&this_module, __VA_ARGS__)
200 #define VLOG_EMER(...) VLOG(VLL_EMER, __VA_ARGS__)
201 #define VLOG_ERR(...) VLOG(VLL_ERR, __VA_ARGS__)
202 #define VLOG_WARN(...) VLOG(VLL_WARN, __VA_ARGS__)
203 #define VLOG_INFO(...) VLOG(VLL_INFO, __VA_ARGS__)
204 #define VLOG_DBG(...) VLOG(VLL_DBG, __VA_ARGS__)
206 /* More convenience macros, for testing whether a given level is enabled. When
207 * constructing a log message is expensive, this enables it to be skipped. */
208 #define VLOG_IS_ERR_ENABLED() vlog_is_enabled(&this_module, VLL_ERR)
209 #define VLOG_IS_WARN_ENABLED() vlog_is_enabled(&this_module, VLL_WARN)
210 #define VLOG_IS_INFO_ENABLED() vlog_is_enabled(&this_module, VLL_INFO)
211 #define VLOG_IS_DBG_ENABLED() vlog_is_enabled(&this_module, VLL_DBG)
213 /* Convenience macros for rate-limiting.
214 * Guaranteed to preserve errno.
216 #define VLOG_ERR_RL(RL, ...) VLOG_RL(RL, VLL_ERR, __VA_ARGS__)
217 #define VLOG_WARN_RL(RL, ...) VLOG_RL(RL, VLL_WARN, __VA_ARGS__)
218 #define VLOG_INFO_RL(RL, ...) VLOG_RL(RL, VLL_INFO, __VA_ARGS__)
219 #define VLOG_DBG_RL(RL, ...) VLOG_RL(RL, VLL_DBG, __VA_ARGS__)
221 /* Convenience macros to additionally store log message in buffer
222 * Caller is responsible for freeing *ERRP afterwards */
223 #define VLOG_ERR_BUF(ERRP, ...) VLOG_ERRP(ERRP, VLL_ERR, __VA_ARGS__)
224 #define VLOG_WARN_BUF(ERRP, ...) VLOG_ERRP(ERRP, VLL_WARN, __VA_ARGS__)
226 #define VLOG_DROP_ERR(RL) vlog_should_drop(&this_module, VLL_ERR, RL)
227 #define VLOG_DROP_WARN(RL) vlog_should_drop(&this_module, VLL_WARN, RL)
228 #define VLOG_DROP_INFO(RL) vlog_should_drop(&this_module, VLL_INFO, RL)
229 #define VLOG_DROP_DBG(RL) vlog_should_drop(&this_module, VLL_DBG, RL)
231 /* Macros for logging at most once per execution. */
232 #define VLOG_ERR_ONCE(...) VLOG_ONCE(VLL_ERR, __VA_ARGS__)
233 #define VLOG_WARN_ONCE(...) VLOG_ONCE(VLL_WARN, __VA_ARGS__)
234 #define VLOG_INFO_ONCE(...) VLOG_ONCE(VLL_INFO, __VA_ARGS__)
235 #define VLOG_DBG_ONCE(...) VLOG_ONCE(VLL_DBG, __VA_ARGS__)
237 /* Command line processing. */
238 #define VLOG_OPTION_ENUMS \
243 #define VLOG_LONG_OPTIONS \
244 {"verbose", optional_argument, NULL, 'v'}, \
245 {"log-file", optional_argument, NULL, OPT_LOG_FILE}, \
246 {"syslog-method", optional_argument, NULL, OPT_SYSLOG_IMPL}, \
247 {"syslog-target", required_argument, NULL, OPT_SYSLOG_TARGET}
249 #define VLOG_OPTION_HANDLERS \
251 vlog_set_verbosity(optarg); \
254 vlog_set_log_file(optarg); \
256 case OPT_SYSLOG_IMPL: \
257 vlog_set_syslog_method(optarg); \
259 case OPT_SYSLOG_TARGET: \
260 vlog_set_syslog_target(optarg); \
263 void vlog_usage(void);
265 /* Implementation details. */
266 #define VLOG(LEVEL, ...) \
268 enum vlog_level level__ = LEVEL; \
269 if (this_module.min_level >= level__) { \
270 vlog(&this_module, level__, __VA_ARGS__); \
273 #define VLOG_RL(RL, LEVEL, ...) \
275 enum vlog_level level__ = LEVEL; \
276 if (this_module.min_level >= level__) { \
277 vlog_rate_limit(&this_module, level__, RL, __VA_ARGS__); \
280 #define VLOG_ONCE(LEVEL, ...) \
282 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; \
283 if (ovsthread_once_start(&once)) { \
284 vlog(&this_module, LEVEL, __VA_ARGS__); \
285 ovsthread_once_done(&once); \
288 #define VLOG_ERRP(ERRP, LEVEL, ...) \
290 VLOG(LEVEL, __VA_ARGS__); \
292 *(ERRP) = xasprintf(__VA_ARGS__); \