vlog: Make the most common module reference more direct.
[cascardo/ovs.git] / include / openvswitch / vlog.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2016 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 void vlog_insert_module(struct ovs_list *);
90
91 const char *vlog_get_module_name(const struct vlog_module *);
92 struct vlog_module *vlog_module_from_name(const char *name);
93
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. */
101 };
102
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)
106
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)                                 \
110         {                                                                 \
111             TOKEN_BUCKET_INIT(RATE, OVS_SAT_MUL(BURST, VLOG_MSG_TOKENS)), \
112             0,                              /* first_dropped */           \
113             0,                              /* last_dropped */            \
114             0,                              /* n_dropped */               \
115             OVS_MUTEX_INITIALIZER           /* mutex */                   \
116         }
117
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);
131
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);
136 #ifndef _WIN32
137 void vlog_change_owner_unix(uid_t, gid_t);
138 #endif
139
140 /* Configure method how vlog should send messages to syslog server. */
141 void vlog_set_syslog_method(const char *method);
142
143 /* Configure syslog target. */
144 void vlog_set_syslog_target(const char *target);
145
146 /* Initialization. */
147 void vlog_init(void);
148 void vlog_enable_async(void);
149
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);
156
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);
162
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);
168
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);
172
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 */  \
183         };                                                              \
184         OVS_CONSTRUCTOR(init_this_module) {                             \
185             vlog_insert_module(&this_module.list);                      \
186         }                                                               \
187                                                                         \
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;
192
193 /* Macros for the current module as set up by VLOG_DEFINE_THIS_MODULE.
194  * These are usually what you want to use.
195  *
196  * Guaranteed to preserve errno.
197  */
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__)
205
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)
212
213 /* Convenience macros for rate-limiting.
214  * Guaranteed to preserve errno.
215  */
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__)
220
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__)
225
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)
230
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__)
236
237 /* Command line processing. */
238 #define VLOG_OPTION_ENUMS                       \
239         OPT_LOG_FILE,                           \
240         OPT_SYSLOG_IMPL,                        \
241         OPT_SYSLOG_TARGET
242
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}
248
249 #define VLOG_OPTION_HANDLERS                    \
250         case 'v':                               \
251             vlog_set_verbosity(optarg);         \
252             break;                              \
253         case OPT_LOG_FILE:                      \
254             vlog_set_log_file(optarg);          \
255             break;                              \
256         case OPT_SYSLOG_IMPL:                   \
257             vlog_set_syslog_method(optarg);     \
258             break;                              \
259         case OPT_SYSLOG_TARGET:                 \
260             vlog_set_syslog_target(optarg);     \
261             break;
262
263 void vlog_usage(void);
264
265 /* Implementation details. */
266 #define VLOG(LEVEL, ...)                                \
267     do {                                                \
268         enum vlog_level level__ = LEVEL;                \
269         if (this_module.min_level >= level__) {         \
270             vlog(&this_module, level__, __VA_ARGS__);   \
271         }                                               \
272     } while (0)
273 #define VLOG_RL(RL, LEVEL, ...)                                         \
274     do {                                                                \
275         enum vlog_level level__ = LEVEL;                                \
276         if (this_module.min_level >= level__) {                         \
277             vlog_rate_limit(&this_module, level__, RL, __VA_ARGS__);    \
278         }                                                               \
279     } while (0)
280 #define VLOG_ONCE(LEVEL, ...)                                           \
281     do {                                                                \
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);                                 \
286         }                                                               \
287     } while (0)
288 #define VLOG_ERRP(ERRP, LEVEL, ...)                                     \
289     do {                                                                \
290         VLOG(LEVEL, __VA_ARGS__);                                       \
291         if (ERRP) {                                                     \
292             *(ERRP) = xasprintf(__VA_ARGS__);                           \
293         }                                                               \
294     } while (0)
295
296 #ifdef  __cplusplus
297 }
298 #endif
299
300 #endif /* vlog.h */