899ad4664514af7365ab2b883907c2035cc29a16
[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 /* Creates and initializes a global instance of a module named MODULE, and
174  * defines a static variable named THIS_MODULE that points to it, for use with
175  * the convenience macros below. */
176 #define VLOG_DEFINE_THIS_MODULE(MODULE)                                 \
177         /* This extra "extern" declaration makes sparse happy. */       \
178         extern struct vlog_module VLM_##MODULE;                         \
179         struct vlog_module VLM_##MODULE =                               \
180         {                                                               \
181             OVS_LIST_INITIALIZER(&VLM_##MODULE.list),                   \
182             #MODULE,                                        /* name */  \
183             { VLL_INFO, VLL_INFO, VLL_INFO },             /* levels */  \
184             VLL_INFO,                                  /* min_level */  \
185             true                               /* honor_rate_limits */  \
186         };                                                              \
187         OVS_CONSTRUCTOR(init_##MODULE) {                                \
188                 vlog_insert_module(&VLM_##MODULE.list);                 \
189         }                                                               \
190         static struct vlog_module *const THIS_MODULE = &VLM_##MODULE
191
192 /* Convenience macros.  These assume that THIS_MODULE points to a "struct
193  * vlog_module" for the current module, as set up by e.g. the
194  * VLOG_DEFINE_THIS_MODULE macro above.
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 in
207  * THIS_MODULE.  When constructing a log message is expensive, this enables it
208  * to be skipped. */
209 #define VLOG_IS_ERR_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_ERR)
210 #define VLOG_IS_WARN_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_WARN)
211 #define VLOG_IS_INFO_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_INFO)
212 #define VLOG_IS_DBG_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_DBG)
213
214 /* Convenience macros for rate-limiting.
215  * Guaranteed to preserve errno.
216  */
217 #define VLOG_ERR_RL(RL, ...) VLOG_RL(RL, VLL_ERR, __VA_ARGS__)
218 #define VLOG_WARN_RL(RL, ...) VLOG_RL(RL, VLL_WARN, __VA_ARGS__)
219 #define VLOG_INFO_RL(RL, ...) VLOG_RL(RL, VLL_INFO, __VA_ARGS__)
220 #define VLOG_DBG_RL(RL, ...) VLOG_RL(RL, VLL_DBG, __VA_ARGS__)
221
222 /* Convenience macros to additionally store log message in buffer
223  * Caller is responsible for freeing *ERRP afterwards */
224 #define VLOG_ERR_BUF(ERRP, ...) VLOG_ERRP(ERRP, VLL_ERR, __VA_ARGS__)
225 #define VLOG_WARN_BUF(ERRP, ...) VLOG_ERRP(ERRP, VLL_WARN, __VA_ARGS__)
226
227 #define VLOG_DROP_ERR(RL) vlog_should_drop(THIS_MODULE, VLL_ERR, RL)
228 #define VLOG_DROP_WARN(RL) vlog_should_drop(THIS_MODULE, VLL_WARN, RL)
229 #define VLOG_DROP_INFO(RL) vlog_should_drop(THIS_MODULE, VLL_INFO, RL)
230 #define VLOG_DROP_DBG(RL) vlog_should_drop(THIS_MODULE, VLL_DBG, RL)
231
232 /* Macros for logging at most once per execution. */
233 #define VLOG_ERR_ONCE(...) VLOG_ONCE(VLL_ERR, __VA_ARGS__)
234 #define VLOG_WARN_ONCE(...) VLOG_ONCE(VLL_WARN, __VA_ARGS__)
235 #define VLOG_INFO_ONCE(...) VLOG_ONCE(VLL_INFO, __VA_ARGS__)
236 #define VLOG_DBG_ONCE(...) VLOG_ONCE(VLL_DBG, __VA_ARGS__)
237
238 /* Command line processing. */
239 #define VLOG_OPTION_ENUMS                       \
240         OPT_LOG_FILE,                           \
241         OPT_SYSLOG_IMPL,                        \
242         OPT_SYSLOG_TARGET
243
244 #define VLOG_LONG_OPTIONS                                               \
245         {"verbose",       optional_argument, NULL, 'v'},                \
246         {"log-file",      optional_argument, NULL, OPT_LOG_FILE},       \
247         {"syslog-method", optional_argument, NULL, OPT_SYSLOG_IMPL},    \
248         {"syslog-target", required_argument, NULL, OPT_SYSLOG_TARGET}
249
250 #define VLOG_OPTION_HANDLERS                    \
251         case 'v':                               \
252             vlog_set_verbosity(optarg);         \
253             break;                              \
254         case OPT_LOG_FILE:                      \
255             vlog_set_log_file(optarg);          \
256             break;                              \
257         case OPT_SYSLOG_IMPL:                   \
258             vlog_set_syslog_method(optarg);     \
259             break;                              \
260         case OPT_SYSLOG_TARGET:                 \
261             vlog_set_syslog_target(optarg);     \
262             break;
263
264 void vlog_usage(void);
265
266 /* Implementation details. */
267 #define VLOG(LEVEL, ...)                                \
268     do {                                                \
269         enum vlog_level level__ = LEVEL;                \
270         if (THIS_MODULE->min_level >= level__) {        \
271             vlog(THIS_MODULE, level__, __VA_ARGS__);    \
272         }                                               \
273     } while (0)
274 #define VLOG_RL(RL, LEVEL, ...)                                     \
275     do {                                                            \
276         enum vlog_level level__ = LEVEL;                            \
277         if (THIS_MODULE->min_level >= level__) {                    \
278             vlog_rate_limit(THIS_MODULE, level__, RL, __VA_ARGS__); \
279         }                                                           \
280     } while (0)
281 #define VLOG_ONCE(LEVEL, ...)                                           \
282     do {                                                                \
283         static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; \
284         if (ovsthread_once_start(&once)) {                              \
285             vlog(THIS_MODULE, LEVEL, __VA_ARGS__);                      \
286             ovsthread_once_done(&once);                                 \
287         }                                                               \
288     } while (0)
289 #define VLOG_ERRP(ERRP, LEVEL, ...)                                     \
290     do {                                                                \
291         VLOG(LEVEL, __VA_ARGS__);                                       \
292         if (ERRP) {                                                     \
293             *(ERRP) = xasprintf(__VA_ARGS__);                           \
294         }                                                               \
295     } while (0)
296
297 #ifdef  __cplusplus
298 }
299 #endif
300
301 #endif /* vlog.h */