2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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 /* Global list of all logging modules */
90 extern struct ovs_list vlog_modules;
92 void vlog_insert_module(struct ovs_list *);
94 /* Creates and initializes a global instance of a module named MODULE. */
95 #define VLOG_DEFINE_MODULE(MODULE) \
96 VLOG_DEFINE_MODULE__(MODULE) \
97 OVS_CONSTRUCTOR(init_##MODULE) { \
98 vlog_insert_module(&VLM_##MODULE.list); \
101 const char *vlog_get_module_name(const struct vlog_module *);
102 struct vlog_module *vlog_module_from_name(const char *name);
104 /* Rate-limiter for log messages. */
105 struct vlog_rate_limit {
106 struct token_bucket token_bucket;
107 time_t first_dropped; /* Time first message was dropped. */
108 time_t last_dropped; /* Time of most recent message drop. */
109 unsigned int n_dropped; /* Number of messages dropped. */
110 struct ovs_mutex mutex; /* Mutual exclusion for rate limit. */
113 /* Number of tokens to emit a message. We add 'rate' tokens per millisecond,
114 * thus 60,000 tokens are required to emit one message per minute. */
115 #define VLOG_MSG_TOKENS (60 * 1000)
117 /* Initializer for a struct vlog_rate_limit, to set up a maximum rate of RATE
118 * messages per minute and a maximum burst size of BURST messages. */
119 #define VLOG_RATE_LIMIT_INIT(RATE, BURST) \
121 TOKEN_BUCKET_INIT(RATE, OVS_SAT_MUL(BURST, VLOG_MSG_TOKENS)), \
122 0, /* first_dropped */ \
123 0, /* last_dropped */ \
125 OVS_MUTEX_INITIALIZER /* mutex */ \
128 /* Configuring how each module logs messages. */
129 enum vlog_level vlog_get_level(const struct vlog_module *,
130 enum vlog_destination);
131 void vlog_set_levels(struct vlog_module *,
132 enum vlog_destination, enum vlog_level);
133 char *vlog_set_levels_from_string(const char *) OVS_WARN_UNUSED_RESULT;
134 void vlog_set_levels_from_string_assert(const char *);
135 char *vlog_get_levels(void);
136 char *vlog_get_patterns(void);
137 bool vlog_is_enabled(const struct vlog_module *, enum vlog_level);
138 bool vlog_should_drop(const struct vlog_module *, enum vlog_level,
139 struct vlog_rate_limit *);
140 void vlog_set_verbosity(const char *arg);
142 /* Configuring log destinations. */
143 void vlog_set_pattern(enum vlog_destination, const char *pattern);
144 int vlog_set_log_file(const char *file_name);
145 int vlog_reopen_log_file(void);
146 void vlog_change_owner(uid_t, gid_t);
148 /* Configure method how vlog should send messages to syslog server. */
149 void vlog_set_syslog_method(const char *method);
151 /* Configure syslog target. */
152 void vlog_set_syslog_target(const char *target);
154 /* Initialization. */
155 void vlog_init(void);
156 void vlog_enable_async(void);
158 /* Functions for actual logging. */
159 void vlog(const struct vlog_module *, enum vlog_level, const char *format, ...)
160 OVS_PRINTF_FORMAT (3, 4);
161 void vlog_valist(const struct vlog_module *, enum vlog_level,
162 const char *, va_list)
163 OVS_PRINTF_FORMAT (3, 0);
165 OVS_NO_RETURN void vlog_fatal(const struct vlog_module *, const char *format, ...)
166 OVS_PRINTF_FORMAT (2, 3);
167 OVS_NO_RETURN void vlog_fatal_valist(const struct vlog_module *,
168 const char *format, va_list)
169 OVS_PRINTF_FORMAT (2, 0);
171 OVS_NO_RETURN void vlog_abort(const struct vlog_module *, const char *format, ...)
172 OVS_PRINTF_FORMAT (2, 3);
173 OVS_NO_RETURN void vlog_abort_valist(const struct vlog_module *,
174 const char *format, va_list)
175 OVS_PRINTF_FORMAT (2, 0);
177 void vlog_rate_limit(const struct vlog_module *, enum vlog_level,
178 struct vlog_rate_limit *, const char *, ...)
179 OVS_PRINTF_FORMAT (4, 5);
181 /* Creates and initializes a global instance of a module named MODULE, and
182 * defines a static variable named THIS_MODULE that points to it, for use with
183 * the convenience macros below. */
184 #define VLOG_DEFINE_THIS_MODULE(MODULE) \
185 VLOG_DEFINE_MODULE(MODULE); \
186 static struct vlog_module *const THIS_MODULE = &VLM_##MODULE
188 /* Convenience macros. These assume that THIS_MODULE points to a "struct
189 * vlog_module" for the current module, as set up by e.g. the
190 * VLOG_DEFINE_MODULE macro above.
192 * Guaranteed to preserve errno.
194 #define VLOG_FATAL(...) vlog_fatal(THIS_MODULE, __VA_ARGS__)
195 #define VLOG_ABORT(...) vlog_abort(THIS_MODULE, __VA_ARGS__)
196 #define VLOG_EMER(...) VLOG(VLL_EMER, __VA_ARGS__)
197 #define VLOG_ERR(...) VLOG(VLL_ERR, __VA_ARGS__)
198 #define VLOG_WARN(...) VLOG(VLL_WARN, __VA_ARGS__)
199 #define VLOG_INFO(...) VLOG(VLL_INFO, __VA_ARGS__)
200 #define VLOG_DBG(...) VLOG(VLL_DBG, __VA_ARGS__)
202 /* More convenience macros, for testing whether a given level is enabled in
203 * THIS_MODULE. When constructing a log message is expensive, this enables it
205 #define VLOG_IS_ERR_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_ERR)
206 #define VLOG_IS_WARN_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_WARN)
207 #define VLOG_IS_INFO_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_INFO)
208 #define VLOG_IS_DBG_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_DBG)
210 /* Convenience macros for rate-limiting.
211 * Guaranteed to preserve errno.
213 #define VLOG_ERR_RL(RL, ...) VLOG_RL(RL, VLL_ERR, __VA_ARGS__)
214 #define VLOG_WARN_RL(RL, ...) VLOG_RL(RL, VLL_WARN, __VA_ARGS__)
215 #define VLOG_INFO_RL(RL, ...) VLOG_RL(RL, VLL_INFO, __VA_ARGS__)
216 #define VLOG_DBG_RL(RL, ...) VLOG_RL(RL, VLL_DBG, __VA_ARGS__)
218 /* Convenience macros to additionally store log message in buffer
219 * Caller is responsible for freeing *ERRP afterwards */
220 #define VLOG_ERR_BUF(ERRP, ...) VLOG_ERRP(ERRP, VLL_ERR, __VA_ARGS__)
221 #define VLOG_WARN_BUF(ERRP, ...) VLOG_ERRP(ERRP, VLL_WARN, __VA_ARGS__)
223 #define VLOG_DROP_ERR(RL) vlog_should_drop(THIS_MODULE, VLL_ERR, RL)
224 #define VLOG_DROP_WARN(RL) vlog_should_drop(THIS_MODULE, VLL_WARN, RL)
225 #define VLOG_DROP_INFO(RL) vlog_should_drop(THIS_MODULE, VLL_INFO, RL)
226 #define VLOG_DROP_DBG(RL) vlog_should_drop(THIS_MODULE, VLL_DBG, RL)
228 /* Macros for logging at most once per execution. */
229 #define VLOG_ERR_ONCE(...) VLOG_ONCE(VLL_ERR, __VA_ARGS__)
230 #define VLOG_WARN_ONCE(...) VLOG_ONCE(VLL_WARN, __VA_ARGS__)
231 #define VLOG_INFO_ONCE(...) VLOG_ONCE(VLL_INFO, __VA_ARGS__)
232 #define VLOG_DBG_ONCE(...) VLOG_ONCE(VLL_DBG, __VA_ARGS__)
234 /* Command line processing. */
235 #define VLOG_OPTION_ENUMS \
240 #define VLOG_LONG_OPTIONS \
241 {"verbose", optional_argument, NULL, 'v'}, \
242 {"log-file", optional_argument, NULL, OPT_LOG_FILE}, \
243 {"syslog-method", optional_argument, NULL, OPT_SYSLOG_IMPL}, \
244 {"syslog-target", required_argument, NULL, OPT_SYSLOG_TARGET}
246 #define VLOG_OPTION_HANDLERS \
248 vlog_set_verbosity(optarg); \
251 vlog_set_log_file(optarg); \
253 case OPT_SYSLOG_IMPL: \
254 vlog_set_syslog_method(optarg); \
256 case OPT_SYSLOG_TARGET: \
257 vlog_set_syslog_target(optarg); \
260 void vlog_usage(void);
262 /* Implementation details. */
263 #define VLOG(LEVEL, ...) \
265 enum vlog_level level__ = LEVEL; \
266 if (THIS_MODULE->min_level >= level__) { \
267 vlog(THIS_MODULE, level__, __VA_ARGS__); \
270 #define VLOG_RL(RL, LEVEL, ...) \
272 enum vlog_level level__ = LEVEL; \
273 if (THIS_MODULE->min_level >= level__) { \
274 vlog_rate_limit(THIS_MODULE, level__, RL, __VA_ARGS__); \
277 #define VLOG_ONCE(LEVEL, ...) \
279 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; \
280 if (ovsthread_once_start(&once)) { \
281 vlog(THIS_MODULE, LEVEL, __VA_ARGS__); \
282 ovsthread_once_done(&once); \
285 #define VLOG_ERRP(ERRP, LEVEL, ...) \
287 VLOG(LEVEL, __VA_ARGS__); \
289 *(ERRP) = xasprintf(__VA_ARGS__); \
293 #define VLOG_DEFINE_MODULE__(MODULE) \
294 extern struct vlog_module VLM_##MODULE; \
295 struct vlog_module VLM_##MODULE = \
297 OVS_LIST_INITIALIZER(&VLM_##MODULE.list), \
298 #MODULE, /* name */ \
299 { VLL_INFO, VLL_INFO, VLL_INFO }, /* levels */ \
300 VLL_INFO, /* min_level */ \
301 true /* honor_rate_limits */ \