vlog: add "vlog/list-pattern" command
[cascardo/ovs.git] / include / openvswitch / vlog.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef OPENVSWITCH_VLOG_H
18 #define OPENVSWITCH_VLOG_H 1
19
20 /* Logging.
21  *
22  *
23  * Thread-safety
24  * =============
25  *
26  * Fully thread safe.
27  */
28
29 #include <limits.h>
30 #include <stdarg.h>
31 #include <stdbool.h>
32 #include <time.h>
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>
38
39 #ifdef  __cplusplus
40 extern "C" {
41 #endif
42
43 /* Logging severity levels.
44  *
45  * ovs-appctl(8) defines each of the log levels. */
46 #define VLOG_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)
53 enum vlog_level {
54 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424_LEVEL) VLL_##NAME,
55     VLOG_LEVELS
56 #undef VLOG_LEVEL
57     VLL_N_LEVELS
58 };
59
60 const char *vlog_get_level_name(enum vlog_level);
61 enum vlog_level vlog_get_level_val(const char *name);
62
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,
70     VLOG_DESTINATIONS
71 #undef VLOG_DESTINATION
72     VLF_N_DESTINATIONS,
73     VLF_ANY_DESTINATION = -1
74 };
75
76 const char *vlog_get_destination_name(enum vlog_destination);
77 enum vlog_destination vlog_get_destination_val(const char *name);
78
79 /* A log module. */
80 struct vlog_module {
81     struct ovs_list list;
82     const char *name;             /* User-visible name. */
83     int levels[VLF_N_DESTINATIONS]; /* Minimum log level for each
84                                        destination. */
85     int min_level;                /* Minimum log level for any destination. */
86     bool honor_rate_limits;       /* Set false to ignore rate limits. */
87 };
88
89 /* Global list of all logging modules */
90 extern struct ovs_list vlog_modules;
91
92 void vlog_insert_module(struct ovs_list *);
93
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);                 \
99         }                                                               \
100
101 const char *vlog_get_module_name(const struct vlog_module *);
102 struct vlog_module *vlog_module_from_name(const char *name);
103
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. */
111 };
112
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)
116
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)                                 \
120         {                                                                 \
121             TOKEN_BUCKET_INIT(RATE, OVS_SAT_MUL(BURST, VLOG_MSG_TOKENS)), \
122             0,                              /* first_dropped */           \
123             0,                              /* last_dropped */            \
124             0,                              /* n_dropped */               \
125             OVS_MUTEX_INITIALIZER           /* mutex */                   \
126         }
127
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);
141
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
147 /* Configure method how vlog should send messages to syslog server. */
148 void vlog_set_syslog_method(const char *method);
149
150 /* Configure syslog target. */
151 void vlog_set_syslog_target(const char *target);
152
153 /* Initialization. */
154 void vlog_init(void);
155 void vlog_enable_async(void);
156
157 /* Functions for actual logging. */
158 void vlog(const struct vlog_module *, enum vlog_level, const char *format, ...)
159     OVS_PRINTF_FORMAT (3, 4);
160 void vlog_valist(const struct vlog_module *, enum vlog_level,
161                  const char *, va_list)
162     OVS_PRINTF_FORMAT (3, 0);
163
164 OVS_NO_RETURN void vlog_fatal(const struct vlog_module *, const char *format, ...)
165     OVS_PRINTF_FORMAT (2, 3);
166 OVS_NO_RETURN void vlog_fatal_valist(const struct vlog_module *,
167                                  const char *format, va_list)
168     OVS_PRINTF_FORMAT (2, 0);
169
170 OVS_NO_RETURN void vlog_abort(const struct vlog_module *, const char *format, ...)
171     OVS_PRINTF_FORMAT (2, 3);
172 OVS_NO_RETURN void vlog_abort_valist(const struct vlog_module *,
173                                  const char *format, va_list)
174     OVS_PRINTF_FORMAT (2, 0);
175
176 void vlog_rate_limit(const struct vlog_module *, enum vlog_level,
177                      struct vlog_rate_limit *, const char *, ...)
178     OVS_PRINTF_FORMAT (4, 5);
179
180 /* Creates and initializes a global instance of a module named MODULE, and
181  * defines a static variable named THIS_MODULE that points to it, for use with
182  * the convenience macros below. */
183 #define VLOG_DEFINE_THIS_MODULE(MODULE)                                 \
184         VLOG_DEFINE_MODULE(MODULE);                                     \
185         static struct vlog_module *const THIS_MODULE = &VLM_##MODULE
186
187 /* Convenience macros.  These assume that THIS_MODULE points to a "struct
188  * vlog_module" for the current module, as set up by e.g. the
189  * VLOG_DEFINE_MODULE macro above.
190  *
191  * Guaranteed to preserve errno.
192  */
193 #define VLOG_FATAL(...) vlog_fatal(THIS_MODULE, __VA_ARGS__)
194 #define VLOG_ABORT(...) vlog_abort(THIS_MODULE, __VA_ARGS__)
195 #define VLOG_EMER(...) VLOG(VLL_EMER, __VA_ARGS__)
196 #define VLOG_ERR(...) VLOG(VLL_ERR, __VA_ARGS__)
197 #define VLOG_WARN(...) VLOG(VLL_WARN, __VA_ARGS__)
198 #define VLOG_INFO(...) VLOG(VLL_INFO, __VA_ARGS__)
199 #define VLOG_DBG(...) VLOG(VLL_DBG, __VA_ARGS__)
200
201 /* More convenience macros, for testing whether a given level is enabled in
202  * THIS_MODULE.  When constructing a log message is expensive, this enables it
203  * to be skipped. */
204 #define VLOG_IS_ERR_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_ERR)
205 #define VLOG_IS_WARN_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_WARN)
206 #define VLOG_IS_INFO_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_INFO)
207 #define VLOG_IS_DBG_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_DBG)
208
209 /* Convenience macros for rate-limiting.
210  * Guaranteed to preserve errno.
211  */
212 #define VLOG_ERR_RL(RL, ...) VLOG_RL(RL, VLL_ERR, __VA_ARGS__)
213 #define VLOG_WARN_RL(RL, ...) VLOG_RL(RL, VLL_WARN, __VA_ARGS__)
214 #define VLOG_INFO_RL(RL, ...) VLOG_RL(RL, VLL_INFO, __VA_ARGS__)
215 #define VLOG_DBG_RL(RL, ...) VLOG_RL(RL, VLL_DBG, __VA_ARGS__)
216
217 /* Convenience macros to additionally store log message in buffer
218  * Caller is responsible for freeing *ERRP afterwards */
219 #define VLOG_ERR_BUF(ERRP, ...) VLOG_ERRP(ERRP, VLL_ERR, __VA_ARGS__)
220 #define VLOG_WARN_BUF(ERRP, ...) VLOG_ERRP(ERRP, VLL_WARN, __VA_ARGS__)
221
222 #define VLOG_DROP_ERR(RL) vlog_should_drop(THIS_MODULE, VLL_ERR, RL)
223 #define VLOG_DROP_WARN(RL) vlog_should_drop(THIS_MODULE, VLL_WARN, RL)
224 #define VLOG_DROP_INFO(RL) vlog_should_drop(THIS_MODULE, VLL_INFO, RL)
225 #define VLOG_DROP_DBG(RL) vlog_should_drop(THIS_MODULE, VLL_DBG, RL)
226
227 /* Macros for logging at most once per execution. */
228 #define VLOG_ERR_ONCE(...) VLOG_ONCE(VLL_ERR, __VA_ARGS__)
229 #define VLOG_WARN_ONCE(...) VLOG_ONCE(VLL_WARN, __VA_ARGS__)
230 #define VLOG_INFO_ONCE(...) VLOG_ONCE(VLL_INFO, __VA_ARGS__)
231 #define VLOG_DBG_ONCE(...) VLOG_ONCE(VLL_DBG, __VA_ARGS__)
232
233 /* Command line processing. */
234 #define VLOG_OPTION_ENUMS                       \
235         OPT_LOG_FILE,                           \
236         OPT_SYSLOG_IMPL,                        \
237         OPT_SYSLOG_TARGET
238
239 #define VLOG_LONG_OPTIONS                                               \
240         {"verbose",       optional_argument, NULL, 'v'},                \
241         {"log-file",      optional_argument, NULL, OPT_LOG_FILE},       \
242         {"syslog-method", optional_argument, NULL, OPT_SYSLOG_IMPL},    \
243         {"syslog-target", required_argument, NULL, OPT_SYSLOG_TARGET}
244
245 #define VLOG_OPTION_HANDLERS                    \
246         case 'v':                               \
247             vlog_set_verbosity(optarg);         \
248             break;                              \
249         case OPT_LOG_FILE:                      \
250             vlog_set_log_file(optarg);          \
251             break;                              \
252         case OPT_SYSLOG_IMPL:                   \
253             vlog_set_syslog_method(optarg);     \
254             break;                              \
255         case OPT_SYSLOG_TARGET:                 \
256             vlog_set_syslog_target(optarg);     \
257             break;
258
259 void vlog_usage(void);
260
261 /* Implementation details. */
262 #define VLOG(LEVEL, ...)                                \
263     do {                                                \
264         enum vlog_level level__ = LEVEL;                \
265         if (THIS_MODULE->min_level >= level__) {        \
266             vlog(THIS_MODULE, level__, __VA_ARGS__);    \
267         }                                               \
268     } while (0)
269 #define VLOG_RL(RL, LEVEL, ...)                                     \
270     do {                                                            \
271         enum vlog_level level__ = LEVEL;                            \
272         if (THIS_MODULE->min_level >= level__) {                    \
273             vlog_rate_limit(THIS_MODULE, level__, RL, __VA_ARGS__); \
274         }                                                           \
275     } while (0)
276 #define VLOG_ONCE(LEVEL, ...)                                           \
277     do {                                                                \
278         static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; \
279         if (ovsthread_once_start(&once)) {                              \
280             vlog(THIS_MODULE, LEVEL, __VA_ARGS__);                      \
281             ovsthread_once_done(&once);                                 \
282         }                                                               \
283     } while (0)
284 #define VLOG_ERRP(ERRP, LEVEL, ...)                                     \
285     do {                                                                \
286         VLOG(LEVEL, __VA_ARGS__);                                       \
287         if (ERRP) {                                                     \
288             *(ERRP) = xasprintf(__VA_ARGS__);                           \
289         }                                                               \
290     } while (0)
291
292 #define VLOG_DEFINE_MODULE__(MODULE)                                    \
293         extern struct vlog_module VLM_##MODULE;                         \
294         struct vlog_module VLM_##MODULE =                               \
295         {                                                               \
296             OVS_LIST_INITIALIZER(&VLM_##MODULE.list),                       \
297             #MODULE,                                        /* name */  \
298             { VLL_INFO, VLL_INFO, VLL_INFO },             /* levels */  \
299             VLL_INFO,                                  /* min_level */  \
300             true                               /* honor_rate_limits */  \
301         };
302
303 #ifdef  __cplusplus
304 }
305 #endif
306
307 #endif /* vlog.h */