2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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.
31 /* Logging severity levels.
33 * ovs-appctl(8) defines each of the log levels. */
35 VLOG_LEVEL(OFF, LOG_ALERT) \
36 VLOG_LEVEL(EMER, LOG_ALERT) \
37 VLOG_LEVEL(ERR, LOG_ERR) \
38 VLOG_LEVEL(WARN, LOG_WARNING) \
39 VLOG_LEVEL(INFO, LOG_NOTICE) \
40 VLOG_LEVEL(DBG, LOG_DEBUG)
42 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) VLL_##NAME,
48 const char *vlog_get_level_name(enum vlog_level);
49 enum vlog_level vlog_get_level_val(const char *name);
51 /* Facilities that we can log to. */
52 #define VLOG_FACILITIES \
53 VLOG_FACILITY(SYSLOG, "%05N|%c|%p|%m") \
54 VLOG_FACILITY(CONSOLE, "%d{%b %d %H:%M:%S}|%05N|%c|%p|%m") \
55 VLOG_FACILITY(FILE, "%d{%b %d %H:%M:%S}|%05N|%c|%p|%m")
57 #define VLOG_FACILITY(NAME, PATTERN) VLF_##NAME,
64 const char *vlog_get_facility_name(enum vlog_facility);
65 enum vlog_facility vlog_get_facility_val(const char *name);
69 const char *name; /* User-visible name. */
70 int levels[VLF_N_FACILITIES]; /* Minimum log level for each facility. */
71 int min_level; /* Minimum log level for any facility. */
74 /* Creates and initializes a global instance of a module named MODULE. */
75 #if USE_LINKER_SECTIONS
76 #define VLOG_DEFINE_MODULE(MODULE) \
77 VLOG_DEFINE_MODULE__(MODULE) \
78 extern struct vlog_module *vlog_module_ptr_##MODULE; \
79 struct vlog_module *vlog_module_ptr_##MODULE \
80 __attribute__((section("vlog_modules"))) = &VLM_##MODULE
82 #define VLOG_DEFINE_MODULE(MODULE) extern struct vlog_module VLM_##MODULE
85 const char *vlog_get_module_name(const struct vlog_module *);
86 struct vlog_module *vlog_module_from_name(const char *name);
88 /* Rate-limiter for log messages. */
89 struct vlog_rate_limit {
90 /* Configuration settings. */
91 unsigned int rate; /* Tokens per second. */
92 unsigned int burst; /* Max cumulative tokens credit. */
95 unsigned int tokens; /* Current number of tokens. */
96 time_t last_fill; /* Last time tokens added. */
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. */
102 /* Number of tokens to emit a message. We add 'rate' tokens per second, which
103 * is 60 times the unit used for 'rate', thus 60 tokens are required to emit
105 #define VLOG_MSG_TOKENS 60
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) \
112 (MIN(BURST, UINT_MAX / VLOG_MSG_TOKENS) \
113 * VLOG_MSG_TOKENS), /* burst */ \
116 0, /* first_dropped */ \
117 0, /* last_dropped */ \
121 /* Configuring how each module logs messages. */
122 enum vlog_level vlog_get_level(const struct vlog_module *, enum vlog_facility);
123 void vlog_set_levels(struct vlog_module *,
124 enum vlog_facility, enum vlog_level);
125 char *vlog_set_levels_from_string(const char *);
126 char *vlog_get_levels(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 facilities. */
133 void vlog_set_pattern(enum vlog_facility, const char *pattern);
134 const char *vlog_get_log_file(void);
135 int vlog_set_log_file(const char *file_name);
136 int vlog_reopen_log_file(void);
138 /* Initialization. */
139 void vlog_init(void);
140 void vlog_exit(void);
142 /* Functions for actual logging. */
143 void vlog(const struct vlog_module *, enum vlog_level, const char *format, ...)
144 PRINTF_FORMAT (3, 4);
145 void vlog_valist(const struct vlog_module *, enum vlog_level,
146 const char *, va_list)
147 PRINTF_FORMAT (3, 0);
149 void vlog_fatal(const struct vlog_module *, const char *format, ...)
150 PRINTF_FORMAT (2, 3) NO_RETURN;
151 void vlog_fatal_valist(const struct vlog_module *, const char *format, va_list)
152 PRINTF_FORMAT (2, 0) NO_RETURN;
154 void vlog_rate_limit(const struct vlog_module *, enum vlog_level,
155 struct vlog_rate_limit *, const char *, ...)
156 PRINTF_FORMAT (4, 5);
158 /* Creates and initializes a global instance of a module named MODULE, and
159 * defines a static variable named THIS_MODULE that points to it, for use with
160 * the convenience macros below. */
161 #define VLOG_DEFINE_THIS_MODULE(MODULE) \
162 VLOG_DEFINE_MODULE(MODULE); \
163 static struct vlog_module *const THIS_MODULE = &VLM_##MODULE
165 /* Convenience macros. These assume that THIS_MODULE points to a "struct
166 * vlog_module" for the current module, as set up by e.g. the
167 * VLOG_DEFINE_MODULE macro above.
169 * Guaranteed to preserve errno.
171 #define VLOG_FATAL(...) vlog_fatal(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 */ \