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.
25 #include "ovs-thread.h"
27 #include "token-bucket.h"
34 /* Logging severity levels.
36 * ovs-appctl(8) defines each of the log levels. */
38 VLOG_LEVEL(OFF, LOG_ALERT) \
39 VLOG_LEVEL(EMER, LOG_ALERT) \
40 VLOG_LEVEL(ERR, LOG_ERR) \
41 VLOG_LEVEL(WARN, LOG_WARNING) \
42 VLOG_LEVEL(INFO, LOG_NOTICE) \
43 VLOG_LEVEL(DBG, LOG_DEBUG)
45 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) VLL_##NAME,
51 const char *vlog_get_level_name(enum vlog_level);
52 enum vlog_level vlog_get_level_val(const char *name);
54 /* Facilities that we can log to. */
55 #define VLOG_FACILITIES \
56 VLOG_FACILITY(SYSLOG, "ovs|%05N|%c%T|%p|%m") \
57 VLOG_FACILITY(CONSOLE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c%T|%p|%m") \
58 VLOG_FACILITY(FILE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c%T|%p|%m")
60 #define VLOG_FACILITY(NAME, PATTERN) VLF_##NAME,
67 const char *vlog_get_facility_name(enum vlog_facility);
68 enum vlog_facility vlog_get_facility_val(const char *name);
72 const char *name; /* User-visible name. */
73 int levels[VLF_N_FACILITIES]; /* Minimum log level for each facility. */
74 int min_level; /* Minimum log level for any facility. */
75 bool honor_rate_limits; /* Set false to ignore rate limits. */
78 /* Creates and initializes a global instance of a module named MODULE. */
79 #if USE_LINKER_SECTIONS
80 #define VLOG_DEFINE_MODULE(MODULE) \
81 VLOG_DEFINE_MODULE__(MODULE) \
82 extern struct vlog_module *vlog_module_ptr_##MODULE; \
83 struct vlog_module *vlog_module_ptr_##MODULE \
84 __attribute__((section("vlog_modules"))) = &VLM_##MODULE
86 #define VLOG_DEFINE_MODULE(MODULE) extern struct vlog_module VLM_##MODULE
89 const char *vlog_get_module_name(const struct vlog_module *);
90 struct vlog_module *vlog_module_from_name(const char *name);
92 /* Rate-limiter for log messages. */
93 struct vlog_rate_limit {
94 struct token_bucket token_bucket;
95 time_t first_dropped; /* Time first message was dropped. */
96 time_t last_dropped; /* Time of most recent message drop. */
97 unsigned int n_dropped; /* Number of messages dropped. */
98 pthread_mutex_t mutex; /* Mutual exclusion for rate limit. */
101 /* Number of tokens to emit a message. We add 'rate' tokens per millisecond,
102 * thus 60,000 tokens are required to emit one message per minute. */
103 #define VLOG_MSG_TOKENS (60 * 1000)
105 /* Initializer for a struct vlog_rate_limit, to set up a maximum rate of RATE
106 * messages per minute and a maximum burst size of BURST messages. */
107 #define VLOG_RATE_LIMIT_INIT(RATE, BURST) \
109 TOKEN_BUCKET_INIT(RATE, SAT_MUL(BURST, VLOG_MSG_TOKENS)), \
110 0, /* first_dropped */ \
111 0, /* last_dropped */ \
113 PTHREAD_ADAPTIVE_MUTEX_INITIALIZER /* mutex */ \
116 /* Configuring how each module logs messages. */
117 enum vlog_level vlog_get_level(const struct vlog_module *, enum vlog_facility);
118 void vlog_set_levels(struct vlog_module *,
119 enum vlog_facility, enum vlog_level);
120 char *vlog_set_levels_from_string(const char *) WARN_UNUSED_RESULT;
121 void vlog_set_levels_from_string_assert(const char *);
122 char *vlog_get_levels(void);
123 bool vlog_is_enabled(const struct vlog_module *, enum vlog_level);
124 bool vlog_should_drop(const struct vlog_module *, enum vlog_level,
125 struct vlog_rate_limit *);
126 void vlog_set_verbosity(const char *arg);
128 /* Configuring log facilities. */
129 void vlog_set_pattern(enum vlog_facility, const char *pattern);
130 int vlog_set_log_file(const char *file_name);
131 int vlog_reopen_log_file(void);
133 /* Initialization. */
134 void vlog_init(void);
136 /* Functions for actual logging. */
137 void vlog(const struct vlog_module *, enum vlog_level, const char *format, ...)
138 PRINTF_FORMAT (3, 4);
139 void vlog_valist(const struct vlog_module *, enum vlog_level,
140 const char *, va_list)
141 PRINTF_FORMAT (3, 0);
143 void vlog_fatal(const struct vlog_module *, const char *format, ...)
144 PRINTF_FORMAT (2, 3) NO_RETURN;
145 void vlog_fatal_valist(const struct vlog_module *, const char *format, va_list)
146 PRINTF_FORMAT (2, 0) NO_RETURN;
148 void vlog_abort(const struct vlog_module *, const char *format, ...)
149 PRINTF_FORMAT (2, 3) NO_RETURN;
150 void vlog_abort_valist(const struct vlog_module *, const char *format, va_list)
151 PRINTF_FORMAT (2, 0) NO_RETURN;
153 void vlog_rate_limit(const struct vlog_module *, enum vlog_level,
154 struct vlog_rate_limit *, const char *, ...)
155 PRINTF_FORMAT (4, 5);
157 /* Creates and initializes a global instance of a module named MODULE, and
158 * defines a static variable named THIS_MODULE that points to it, for use with
159 * the convenience macros below. */
160 #define VLOG_DEFINE_THIS_MODULE(MODULE) \
161 VLOG_DEFINE_MODULE(MODULE); \
162 static struct vlog_module *const THIS_MODULE = &VLM_##MODULE
164 /* Convenience macros. These assume that THIS_MODULE points to a "struct
165 * vlog_module" for the current module, as set up by e.g. the
166 * VLOG_DEFINE_MODULE macro above.
168 * Guaranteed to preserve errno.
170 #define VLOG_FATAL(...) vlog_fatal(THIS_MODULE, __VA_ARGS__)
171 #define VLOG_ABORT(...) vlog_abort(THIS_MODULE, __VA_ARGS__)
172 #define VLOG_EMER(...) VLOG(VLL_EMER, __VA_ARGS__)
173 #define VLOG_ERR(...) VLOG(VLL_ERR, __VA_ARGS__)
174 #define VLOG_WARN(...) VLOG(VLL_WARN, __VA_ARGS__)
175 #define VLOG_INFO(...) VLOG(VLL_INFO, __VA_ARGS__)
176 #define VLOG_DBG(...) VLOG(VLL_DBG, __VA_ARGS__)
178 /* More convenience macros, for testing whether a given level is enabled in
179 * THIS_MODULE. When constructing a log message is expensive, this enables it
181 #define VLOG_IS_ERR_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_ERR)
182 #define VLOG_IS_WARN_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_WARN)
183 #define VLOG_IS_INFO_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_INFO)
184 #define VLOG_IS_DBG_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_DBG)
186 /* Convenience macros for rate-limiting.
187 * Guaranteed to preserve errno.
189 #define VLOG_ERR_RL(RL, ...) VLOG_RL(RL, VLL_ERR, __VA_ARGS__)
190 #define VLOG_WARN_RL(RL, ...) VLOG_RL(RL, VLL_WARN, __VA_ARGS__)
191 #define VLOG_INFO_RL(RL, ...) VLOG_RL(RL, VLL_INFO, __VA_ARGS__)
192 #define VLOG_DBG_RL(RL, ...) VLOG_RL(RL, VLL_DBG, __VA_ARGS__)
194 #define VLOG_DROP_ERR(RL) vlog_should_drop(THIS_MODULE, VLL_ERR, RL)
195 #define VLOG_DROP_WARN(RL) vlog_should_drop(THIS_MODULE, VLL_WARN, RL)
196 #define VLOG_DROP_INFO(RL) vlog_should_drop(THIS_MODULE, VLL_INFO, RL)
197 #define VLOG_DROP_DBG(RL) vlog_should_drop(THIS_MODULE, VLL_DBG, RL)
199 /* Macros for logging at most once per execution. */
200 #define VLOG_ERR_ONCE(...) VLOG_ONCE(VLL_ERR, __VA_ARGS__)
201 #define VLOG_WARN_ONCE(...) VLOG_ONCE(VLL_WARN, __VA_ARGS__)
202 #define VLOG_INFO_ONCE(...) VLOG_ONCE(VLL_INFO, __VA_ARGS__)
203 #define VLOG_DBG_ONCE(...) VLOG_ONCE(VLL_DBG, __VA_ARGS__)
205 /* Command line processing. */
206 #define VLOG_OPTION_ENUMS OPT_LOG_FILE
207 #define VLOG_LONG_OPTIONS \
208 {"verbose", optional_argument, NULL, 'v'}, \
209 {"log-file", optional_argument, NULL, OPT_LOG_FILE}
210 #define VLOG_OPTION_HANDLERS \
212 vlog_set_verbosity(optarg); \
215 vlog_set_log_file(optarg); \
217 void vlog_usage(void);
219 /* Implementation details. */
220 #define VLOG(LEVEL, ...) \
222 enum vlog_level level__ = LEVEL; \
223 if (THIS_MODULE->min_level >= level__) { \
224 vlog(THIS_MODULE, level__, __VA_ARGS__); \
227 #define VLOG_RL(RL, LEVEL, ...) \
229 enum vlog_level level__ = LEVEL; \
230 if (THIS_MODULE->min_level >= level__) { \
231 vlog_rate_limit(THIS_MODULE, level__, RL, __VA_ARGS__); \
234 #define VLOG_ONCE(LEVEL, ...) \
236 static bool already_logged; \
237 if (!already_logged) { \
238 already_logged = true; \
239 vlog(THIS_MODULE, LEVEL, __VA_ARGS__); \
243 #define VLOG_DEFINE_MODULE__(MODULE) \
244 extern struct vlog_module VLM_##MODULE; \
245 struct vlog_module VLM_##MODULE = \
247 #MODULE, /* name */ \
248 { [ 0 ... VLF_N_FACILITIES - 1] = VLL_INFO }, /* levels */ \
249 VLL_INFO, /* min_level */ \
250 true /* honor_rate_limits */ \